Skip to content

Conversation

cbornet
Copy link
Collaborator

@cbornet cbornet commented Sep 4, 2025

  • Use importlib to load dynamically the classes
  • Removes missing package warnings

Copy link

vercel bot commented Sep 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
langchain Ignored Ignored Preview Sep 11, 2025 2:30pm

Copy link

codspeed-hq bot commented Sep 4, 2025

CodSpeed WallTime Performance Report

Merging #32813 will not alter performance

Comparing cbornet:langchain_v1-refactor-imports (21a3b8f) with master (af17774)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched

Copy link

codspeed-hq bot commented Sep 4, 2025

CodSpeed Instrumentation Performance Report

Merging #32813 will not alter performance

Comparing cbornet:langchain_v1-refactor-imports (21a3b8f) with master (af17774)

Summary

✅ 14 untouched

@mdrxy mdrxy self-assigned this Sep 4, 2025
@mdrxy mdrxy added the langchain Related to the package `langchain` label Sep 4, 2025
@mdrxy mdrxy changed the title refactor(langchain): refactor optional imports logic in langchain_v1 refactor(langchain): refactor optional imports logic in langchain_v1 Sep 4, 2025
@@ -28,6 +28,47 @@
from pydantic import BaseModel


_SUPPORTED_PROVIDERS = {
"openai": ("langchain_openai", "ChatOpenAI"),
Copy link
Collaborator

@eyurtsev eyurtsev Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't want to load things on __init__ -- we want to have as fast of an import of langchain / langchain.chat_models as possible. Users would ideally only pay an import cost for the things they use

Refactor OK, but let's make it lazy and memoize appropriately?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruff rule PLC0415 recommends to do the imports at the module loading.
I think the langchain_* modules are quite fast to import, aren't they ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's been a significant problem which is why we've swapped to lazy imports in many places. I think we saw import time grow to ~0.5 seconds, which is way too slow

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we want as little of the code loaded up front, so that end of the day only the parts of the code that the user needs are loaded

Copy link
Collaborator Author

@cbornet cbornet Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I can switch to a cached lazy load then.
Note that the big load time may come from one package alone. It would be interesting to identify it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@cbornet cbornet force-pushed the langchain_v1-refactor-imports branch 5 times, most recently from 53357e0 to 0a6572c Compare September 11, 2025 14:29
@cbornet cbornet force-pushed the langchain_v1-refactor-imports branch from 0a6572c to 21a3b8f Compare September 11, 2025 14:30
@cbornet cbornet requested a review from eyurtsev September 11, 2025 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
langchain Related to the package `langchain`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants