Skip to content

fix(core): lazy loading transformers, numpy, simsimd#35466

Open
Won-Kyu Park (wkpark) wants to merge 2 commits into
langchain-ai:masterfrom
wkpark:lazy_load_numpy_transformers
Open

fix(core): lazy loading transformers, numpy, simsimd#35466
Won-Kyu Park (wkpark) wants to merge 2 commits into
langchain-ai:masterfrom
wkpark:lazy_load_numpy_transformers

Conversation

@wkpark

@wkpark Won-Kyu Park (wkpark) commented Feb 27, 2026

Copy link
Copy Markdown

fix(core): lazy loading transformers, numpy, simsimd

Summary

Optimized the initial import speed of langchain_core by converting top-level imports of transformers, numpy, and simsimd into lazy, local imports.

Details

Confirmed with python -X importtime that these dependencies are no longer loaded on initial import, and verified functionality through make format/lint and est.

Disclaimer

I used the Gemini CLI to help with the specific implementation details and to ensure compliance with linting and type checking and drafting this PR description.

before
without transformers / torch env.

$ time uv run python -c "from langchain_core.prompts import BasePromptTemplate"
real    0m0.600s
user    0m0.509s
sys     0m0.074s

with transformers / torch installed env.

$ uv pip install transformers
...
$ uv pip install numpy
...

$ time uv run python -c "from langchain_core.prompts import BasePromptTemplate"
real    0m9.664s
user    0m4.268s
sys     0m0.752s

$ time uv run python -c "from langchain_core.prompts import BasePromptTemplate" # one more time

real    0m4.030s
user    0m3.877s
sys     0m0.468s

after PR applied

$ time uv run python -c "from langchain_core.prompts import BasePromptTemplate"
real    0m0.797s
user    0m0.662s
sys     0m0.110s

$ uv pip show transformers
Name: transformers
Version: 4.55.0
Location: /home/future/repo/linkBrain-server/.venv/lib/python3.13/site-packages
Requires: filelock, huggingface-hub, numpy, packaging, pyyaml, regex, requests, safetensors, tokenizers, tqdm
Required-by: docling-ibm-models, optimum, sentence-transformers

See also #35469

@github-actions github-actions Bot added core `langchain-core` package issues & PRs external labels Feb 27, 2026
@wkpark Won-Kyu Park (wkpark) changed the title Lazy load numpy transformers fix(core): lazy loading transformers, numpy, simsimd Feb 27, 2026
@github-actions github-actions Bot added the fix For PRs that implement a fix label Feb 27, 2026
@codspeed-hq

codspeed-hq Bot commented Feb 27, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 10.65%

⚡ 1 improved benchmark
✅ 12 untouched benchmarks
⏩ 2 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_import_time[InMemoryVectorStore] 1.5 s 1.4 s +10.65%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing wkpark:lazy_load_numpy_transformers (a34d883) with master (aef86c4)2

Open in CodSpeed

Footnotes

  1. 2 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (06e6507) during the generation of this report, so aef86c4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@wkpark Won-Kyu Park (wkpark) force-pushed the lazy_load_numpy_transformers branch 2 times, most recently from a0b778f to b5f28ac Compare February 27, 2026 20:08
@github-actions github-actions Bot added fix For PRs that implement a fix and removed fix For PRs that implement a fix labels Mar 1, 2026
@wkpark Won-Kyu Park (wkpark) force-pushed the lazy_load_numpy_transformers branch 2 times, most recently from 18d65be to 789268d Compare March 5, 2026 11:21
@github-actions github-actions Bot added the size: XS < 50 LOC label Mar 9, 2026
@ajram23

ajram23 commented Mar 24, 2026

Copy link
Copy Markdown

+1 running into the same problem on the Mac for transformers

@wkpark Won-Kyu Park (wkpark) force-pushed the lazy_load_numpy_transformers branch 2 times, most recently from 44d6276 to 2e64f7a Compare April 17, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants