Skip to content

Commit 8f8fea2

Browse files
[performance]: Use hard coded langchain-core version to avoid importlib import (#30744)
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](https://github.com/langchain-ai/langchain/actions/runs/14358012706/job/40251952044?pr=30744) 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 <img width="1081" alt="Screenshot 2025-04-09 at 9 06 15 AM" src="https://github.com/user-attachments/assets/59f405ec-ee8d-4473-89ff-45dea5befa31" /> After (note, lack of `importlib.metadata` time sink): <img width="1245" alt="Screenshot 2025-04-09 at 9 01 23 AM" src="https://github.com/user-attachments/assets/9c32e77c-27ce-485e-9b88-e365193ed58d" />
1 parent cd6a831 commit 8f8fea2

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Check `langchain-core` version equality
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'libs/core/pyproject.toml'
7+
- 'libs/core/langchain_core/version.py'
8+
9+
jobs:
10+
check_version_equality:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Check version equality
17+
run: |
18+
PYPROJECT_VERSION=$(grep -Po '(?<=^version = ")[^"]*' libs/core/pyproject.toml)
19+
VERSION_PY_VERSION=$(grep -Po '(?<=^VERSION = ")[^"]*' libs/core/langchain_core/version.py)
20+
21+
# Compare the two versions
22+
if [ "$PYPROJECT_VERSION" != "$VERSION_PY_VERSION" ]; then
23+
echo "langchain-core versions in pyproject.toml and version.py do not match!"
24+
echo "pyproject.toml version: $PYPROJECT_VERSION"
25+
echo "version.py version: $VERSION_PY_VERSION"
26+
exit 1
27+
else
28+
echo "Versions match: $PYPROJECT_VERSION"
29+
fi

libs/core/langchain_core/__init__.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,13 @@
88
very lightweight.
99
"""
1010

11-
from importlib import metadata
12-
1311
from langchain_core._api import (
1412
surface_langchain_beta_warnings,
1513
surface_langchain_deprecation_warnings,
1614
)
15+
from langchain_core.version import VERSION
1716

18-
try:
19-
__version__ = metadata.version(__package__)
20-
except metadata.PackageNotFoundError:
21-
# Case where package metadata is not available.
22-
__version__ = ""
17+
__version__ = VERSION
2318

2419
surface_langchain_deprecation_warnings()
2520
surface_langchain_beta_warnings()

libs/core/langchain_core/version.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""langchain-core version information and utilities."""
2+
3+
VERSION = "0.3.51"

0 commit comments

Comments
 (0)