Skip to content

[performance]: Use hard coded langchain-core version to avoid importlib import #30744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 10, 2025

Conversation

sydney-runkle
Copy link
Collaborator

@sydney-runkle sydney-runkle commented Apr 9, 2025

This PR aims to reduce import time of langchain-core tools by removing the importlib.metadata import previously used in __init__.py. This is the first in a sequence of PRs to reduce import time delays for langchain-core features and structures 🚀.

Because we're now hard coding the version, we need to make sure version.py and pyproject.toml stay in sync, so I've added a new CI job that runs whenever either of those files are modified. This run demonstrates the failure that occurs whenever the version gets out of sync (thus blocking a PR).

Before, note the ~15% of time spent on the importlib.metadata /related imports

Screenshot 2025-04-09 at 9 06 15 AM

After (note, lack of importlib.metadata time sink):

Screenshot 2025-04-09 at 9 01 23 AM

Copy link

vercel bot commented Apr 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Apr 10, 2025 6:10pm

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. langchain Related to the langchain package labels Apr 9, 2025
@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Apr 9, 2025
@sydney-runkle
Copy link
Collaborator Author

Waiting on merging this until we have benchmarks from #30747 in place so that we can track progress.

Copy link

codspeed-hq bot commented Apr 9, 2025

CodSpeed Performance Report

Merging #30744 will improve performances by 27.77%

Comparing hard-coded-version (02e7498) with master (cd6a831)

Summary

⚡ 1 improvements
✅ 11 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
test_import_time[InMemoryRateLimiter] 273 ms 213.6 ms +27.77%

@@ -0,0 +1,29 @@
name: Check `langchain-core` version equality
Copy link
Collaborator

@eyurtsev eyurtsev Apr 9, 2025

Choose a reason for hiding this comment

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

We need to generalize this I think. How about we do this as part of the release pipeline so it'll apply to all libraries. We can put logic in workflow to only apply it to libraries that we fixed already (or even skip and force all libraries to include version which I would prefer!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I think generalizing this would be good... maybe in a separate PR where we automate some of the manual and redundant release tasks (like version bumping, etc).

Copy link

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea @Viicos, thanks! Will do :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, maybe I should just do the version stuff across all of our libs at once then :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Viicos, explored dynamic versioning via #30771. pdm-backend has relatively limited support, and it breaks our release CI, so going to stick with keeping the version in pyproject.toml for now.

Maybe if we move to hatch, that switch would make sense.

@sydney-runkle sydney-runkle merged commit 8f8fea2 into master Apr 10, 2025
77 checks passed
@sydney-runkle sydney-runkle deleted the hard-coded-version branch April 10, 2025 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
langchain Related to the langchain package lgtm PR looks good. Use to confirm that a PR is ready for merging. size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants