1
0
mirror of https://github.com/hwchase17/langchain.git synced 2025-05-04 22:58:42 +00:00
langchain/libs
Sydney Runkle 4f69094b51
core[performance]: use custom __getattr__ in __init__.py files for lazy imports ()
Most easily reviewed with the "hide whitespace" option toggled.

Seeing 10-50% speed ups in import time for common structures 🚀 

The general purpose of this PR is to lazily import structures within
`langchain_core.XXX_module.__init__.py` so that we're not eagerly
importing expensive dependencies (`pydantic`, `requests`, etc).

Analysis of flamegraphs generated with `importtime` motivated these
changes. For example, the one below demonstrates that importing
`HumanMessage` accidentally triggered imports for `importlib.metadata`,
`requests`, etc.

There's still much more to do on this front, and we can start digging
into our own internal code for optimizations now that we're less
concerned about external imports.

<img width="1210" alt="Screenshot 2025-04-11 at 1 10 54 PM"
src="https://github.com/user-attachments/assets/112a3fe7-24a9-4294-92c1-d5ae64df839e"
/>

I've tracked the improvements with some local benchmarks:

## `pytest-benchmark` results

| Name | Before (s) | After (s) | Delta (s) | % Change |

|-----------------------------|------------|-----------|-----------|----------|
| Document | 2.8683 | 1.2775 | -1.5908 | -55.46% |
| HumanMessage | 2.2358 | 1.1673 | -1.0685 | -47.79% |
| ChatPromptTemplate | 5.5235 | 2.9709 | -2.5526 | -46.22% |
| Runnable | 2.9423 | 1.7793 | -1.163 | -39.53% |
| InMemoryVectorStore | 3.1180 | 1.8417 | -1.2763 | -40.93% |
| RunnableLambda | 2.7385 | 1.8745 | -0.864 | -31.55% |
| tool | 5.1231 | 4.0771 | -1.046 | -20.42% |
| CallbackManager | 4.2263 | 3.4099 | -0.8164 | -19.32% |
| LangChainTracer | 3.8394 | 3.3101 | -0.5293 | -13.79% |
| BaseChatModel | 4.3317 | 3.8806 | -0.4511 | -10.41% |
| PydanticOutputParser | 3.2036 | 3.2995 | 0.0959 | 2.99% |
| InMemoryRateLimiter | 0.5311 | 0.5995 | 0.0684 | 12.88% |

Note the lack of change for `InMemoryRateLimiter` and
`PydanticOutputParser` is just random noise, I'm getting comparable
numbers locally.

## Local CodSpeed results

We're still working on configuring CodSpeed on CI. The local usage
produced similar results.
2025-04-14 08:57:54 -04:00
..
cli docs: streamline LangSmith teasing () 2025-03-28 15:13:22 -04:00
community community: Add ruff rule PGH003 () 2025-04-14 02:32:13 +00:00
core core[performance]: use custom __getattr__ in __init__.py files for lazy imports () 2025-04-14 08:57:54 -04:00
experimental experimental: migrate to external repo () 2024-09-25 19:02:19 -07:00
langchain langchain[lint]: use pyupgrade to get to 3.9 standards () 2025-04-11 10:33:26 -04:00
partners perplexity[patch]: Fix : Handle missing citations attribute in ChatPerplexity () 2025-04-13 09:24:05 -04:00
standard-tests Clean up numpy dependencies and speed up 3.13 CI with numpy>=2.1.0 () 2025-04-08 09:45:07 -04:00
text-splitters text-splitters: release 0.3.8 () 2025-04-04 09:58:45 -04:00
packages.yml docs: YDB Vector Store docs () 2025-04-10 21:33:56 -04:00