Skip to content

Commit f32c72b

Browse files
authored
fix(langchain): fix patching langchain throws an ImportError [backport 3.15] (#14739)
Backport 967ff6d from #14687 to 3.15. ## Description Changes our patching logic to only patch `langchain_core`, as we have not supported operations on `langchain` directly for some time, and is being removed in all langchain 1.0.0 releases - patching this was causing `ImporError`s due to circular imports from how langchain switched some imports from inlined to top-level. The reason we patched `langchain-community`, `langchain-openai`, and `langchain-pinecone` in the first place was because we could not patch the underlying `embed_query`, `embed_documents`, and `similarity_search` for embeddings and vectorstores as the base classes are abstract. However, by patching `__init_subclass__`, we can successfully patch them on each instance, meaning we only need to patch `langchain_core`. This is reflected in the change in `_monkey.py`, and the imported `langchain_core` in the patching file. ## Testing Updated all riot lockfiles and ensured existing tests continued to pass. In order to make tests work (as `__init_submodules__` did not play nicely with cached modules & patching/unpatching every test), the `langchain_core` fixture is scoped to each module. Additionally, running this script: ```python from ddtrace.llmobs import LLMObs LLMObs.enable(ml_app="sam-test") from langchain_openai import ChatOpenAI chat = ChatOpenAI( model = "gpt-3.5-turbo", temperature=0.1, ) resp = chat.invoke("What is the capital of France?") ``` with ```bash $ python -m venv .venv $ source .venv/bin/activate $ pip install langchain_openai $ pip install ddtrace ``` Should not experience any spans being submitted, but with ```bash $ pip install -e /path/to/dd-trace-py ``` checked out to this branch should see spans being submitted. [Trace](https://dd.datad0g.com/llm/traces?query=%40ml_app%3Asam-test%20%40event_type%3Aspan%20%40parent_id%3Aundefined&agg_m=count&agg_m_source=base&agg_t=count&fromUser=false&sp=%5B%7B%22sp%22%3A%7B%22width%22%3A%22min%28100%25%2C%20max%28calc%28100%25%20-%20var%28--ui-page-left-offset%29%20-%2016px%29%2C%20900px%29%29%22%7D%2C%22p%22%3A%7B%22eventId%22%3A%22AwAAAZl4GYUiibFcWwAAABhBWmw0R1lVaUFBQVI1bGtTMlMtaUFBQUEAAAAkZDE5OTc4MTktYTZkYS00NzMxLTk1NTctZmZjNTk0MDc4NjM5AAAAGA%22%7D%2C%22i%22%3A%22llm-obs-panel%22%7D%5D&spanId=202569580851811194&start=1758653023818&end=1758656623818&paused=false) using `ddtrace` without the fix (just OpenAI span, missing LangChain span. Output does not directly include the `ImportError` but `breakpoint`ing in does reveal it). [Trace](https://dd.datad0g.com/llm/traces?query=%40ml_app%3Asam-test%20%40event_type%3Aspan%20%40parent_id%3Aundefined&agg_m=count&agg_m_source=base&agg_t=count&fromUser=false&sp=%5B%7B%22sp%22%3A%7B%22width%22%3A%22min%28100%25%2C%20max%28calc%28100%25%20-%20var%28--ui-page-left-offset%29%20-%2016px%29%2C%20900px%29%29%22%7D%2C%22p%22%3A%7B%22eventId%22%3A%22AwAAAZl4Gcv15UA_aQAAABhBWmw0R2N2MUFBQXlvaEhFVkI0Y0FBQUEAAAAkZjE5OTc4MTktZDc5MS00ZDQyLTlhMWMtZTk2OGM5NTUzODYzAAAAGQ%22%7D%2C%22i%22%3A%22llm-obs-panel%22%7D%5D&spanId=12717218430539325303&start=1758653023818&end=1758656623818&paused=false) using local `ddtrace` with the fix (Includes the LangChain parent span). ## Risks Importing `langchain` only/directly will no longer trigger patching. With that being said, that hasn't been a valid way to use langchain for some time now. ## Additional Notes MLOB-3970 Fixes #14590
1 parent c2df9bc commit f32c72b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2054
-1804
lines changed

.riot/requirements/10d6b85.txt

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.10
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate .riot/requirements/10d6b85.in
6+
#
7+
ai21==4.2.0
8+
aiohappyeyeballs==2.6.1
9+
aiohttp==3.12.15
10+
aiosignal==1.4.0
11+
annotated-types==0.7.0
12+
anthropic==0.68.0
13+
anyio==4.11.0
14+
async-timeout==4.0.3
15+
attrs==25.3.0
16+
boto3==1.40.39
17+
botocore==1.40.39
18+
certifi==2025.8.3
19+
charset-normalizer==3.4.3
20+
cohere==5.18.0
21+
coverage[toml]==7.10.7
22+
dataclasses-json==0.6.7
23+
distro==1.9.0
24+
docstring-parser==0.17.0
25+
exceptiongroup==1.3.0
26+
fastavro==1.12.0
27+
filelock==3.19.1
28+
frozenlist==1.7.0
29+
fsspec==2025.9.0
30+
greenlet==3.0.3
31+
h11==0.16.0
32+
hf-xet==1.1.10
33+
httpcore==1.0.9
34+
httpx==0.28.1
35+
httpx-sse==0.4.0
36+
huggingface-hub==0.35.1
37+
hypothesis==6.45.0
38+
idna==3.10
39+
iniconfig==2.1.0
40+
jiter==0.11.0
41+
jmespath==1.0.1
42+
jsonpatch==1.33
43+
jsonpointer==3.0.0
44+
langchain==0.3.27
45+
langchain-anthropic==0.3.20
46+
langchain-aws==0.2.33
47+
langchain-cohere==0.3.5
48+
langchain-community==0.3.29
49+
langchain-core==0.3.76
50+
langchain-experimental==0.3.4
51+
langchain-openai==0.3.33
52+
langchain-text-splitters==0.3.11
53+
langsmith==0.4.31
54+
marshmallow==3.26.1
55+
mock==5.2.0
56+
multidict==6.6.4
57+
mypy-extensions==1.1.0
58+
numexpr==2.8.5
59+
numpy==1.26.4
60+
openai==1.109.1
61+
opentracing==2.4.0
62+
orjson==3.11.3
63+
packaging==25.0
64+
pandas==2.3.2
65+
pluggy==1.6.0
66+
propcache==0.3.2
67+
psutil==7.1.0
68+
pydantic==2.11.9
69+
pydantic-core==2.33.2
70+
pydantic-settings==2.11.0
71+
pygments==2.19.2
72+
pytest==8.4.2
73+
pytest-asyncio==0.23.7
74+
pytest-cov==7.0.0
75+
pytest-mock==3.15.1
76+
pytest-randomly==3.10.1
77+
python-dateutil==2.9.0.post0
78+
python-dotenv==1.1.1
79+
pytz==2025.2
80+
pyyaml==6.0.2
81+
regex==2025.9.18
82+
requests==2.32.5
83+
requests-toolbelt==1.0.0
84+
respx==0.22.0
85+
s3transfer==0.14.0
86+
six==1.17.0
87+
sniffio==1.3.1
88+
sortedcontainers==2.4.0
89+
sqlalchemy==2.0.43
90+
tabulate==0.9.0
91+
tenacity==8.5.0
92+
tiktoken==0.11.0
93+
tokenizers==0.22.1
94+
tomli==2.2.1
95+
tqdm==4.67.1
96+
types-requests==2.32.4.20250913
97+
typing-extensions==4.15.0
98+
typing-inspect==0.9.0
99+
typing-inspection==0.4.1
100+
tzdata==2025.2
101+
urllib3==2.5.0
102+
yarl==1.20.1
103+
zstandard==0.25.0

.riot/requirements/11b0e12.txt

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.12
3+
# by the following command:
4+
#
5+
# pip-compile --no-annotate .riot/requirements/11b0e12.in
6+
#
7+
ai21==4.2.0
8+
aiohappyeyeballs==2.6.1
9+
aiohttp==3.12.15
10+
aiosignal==1.4.0
11+
annotated-types==0.7.0
12+
anthropic==0.68.0
13+
anyio==4.11.0
14+
attrs==25.3.0
15+
boto3==1.40.39
16+
botocore==1.40.39
17+
certifi==2025.8.3
18+
charset-normalizer==3.4.3
19+
cohere==5.18.0
20+
coverage[toml]==7.10.7
21+
dataclasses-json==0.6.7
22+
distro==1.9.0
23+
docstring-parser==0.17.0
24+
exceptiongroup==1.3.0
25+
fastavro==1.12.0
26+
filelock==3.19.1
27+
frozenlist==1.7.0
28+
fsspec==2025.9.0
29+
greenlet==3.0.3
30+
h11==0.16.0
31+
hf-xet==1.1.10
32+
httpcore==1.0.9
33+
httpx==0.28.1
34+
httpx-sse==0.4.0
35+
huggingface-hub==0.35.1
36+
hypothesis==6.45.0
37+
idna==3.10
38+
iniconfig==2.1.0
39+
jiter==0.11.0
40+
jmespath==1.0.1
41+
jsonpatch==1.33
42+
jsonpointer==3.0.0
43+
langchain==0.3.27
44+
langchain-anthropic==0.3.20
45+
langchain-aws==0.2.33
46+
langchain-cohere==0.4.6
47+
langchain-community==0.3.29
48+
langchain-core==0.3.76
49+
langchain-openai==0.3.33
50+
langchain-text-splitters==0.3.11
51+
langsmith==0.4.31
52+
marshmallow==3.26.1
53+
mock==5.2.0
54+
multidict==6.6.4
55+
mypy-extensions==1.1.0
56+
numexpr==2.8.5
57+
numpy==2.3.3
58+
openai==1.109.1
59+
opentracing==2.4.0
60+
orjson==3.11.3
61+
packaging==25.0
62+
pluggy==1.6.0
63+
propcache==0.3.2
64+
psutil==7.1.0
65+
pydantic==2.11.9
66+
pydantic-core==2.33.2
67+
pydantic-settings==2.11.0
68+
pygments==2.19.2
69+
pytest==8.4.2
70+
pytest-asyncio==0.23.7
71+
pytest-cov==7.0.0
72+
pytest-mock==3.15.1
73+
pytest-randomly==3.10.1
74+
python-dateutil==2.9.0.post0
75+
python-dotenv==1.1.1
76+
pyyaml==6.0.2
77+
regex==2025.9.18
78+
requests==2.32.5
79+
requests-toolbelt==1.0.0
80+
respx==0.22.0
81+
s3transfer==0.14.0
82+
six==1.17.0
83+
sniffio==1.3.1
84+
sortedcontainers==2.4.0
85+
sqlalchemy==2.0.43
86+
tenacity==8.5.0
87+
tiktoken==0.11.0
88+
tokenizers==0.22.1
89+
tqdm==4.67.1
90+
types-pyyaml==6.0.12.20250915
91+
types-requests==2.32.4.20250913
92+
typing-extensions==4.15.0
93+
typing-inspect==0.9.0
94+
typing-inspection==0.4.1
95+
urllib3==2.5.0
96+
yarl==1.20.1
97+
zstandard==0.25.0

.riot/requirements/12135c7.txt

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)