langchain - 💡(How to fix) Fix Importing langchain.agents breaks BaseLLM class construction on Pydantic main

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

Traceback (most recent call last): File "/Users/alex/Library/Application Support/JetBrains/PyCharm2026.1/scratches/scratch_2970.py", line 2, in <module> from langchain_core.language_models.llms import BaseLLM File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/langchain_core/language_models/llms.py", line 293, in <module> class BaseLLM(BaseLanguageModel[str], ABC): File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 249, in new set_model_fields(cls, config_wrapper=config_wrapper, ns_resolver=ns_resolver) File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 585, in set_model_fields fields, pydantic_extra_info, class_vars = collect_model_fields( ^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 264, in collect_model_fields type_hints = _typing_extra.get_model_type_hints(cls, ns_resolver=ns_resolver) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 354, in get_model_type_hints hints[name] = try_eval_type(value, globalns, localns) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 409, in try_eval_type return eval_type(value, globalns, localns), True ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 433, in eval_type return _eval_type(value, globalns, localns, type_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 510, in _eval_type return typing._eval_type( # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/typing.py", line 415, in _eval_type return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/alex/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/typing.py", line 947, in _evaluate eval(self.forward_code, globalns, localns), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<string>", line 1, in <module> TypeError: 'function' object is not subscriptable Unable to evaluate type annotation 'dict[str, Any] | None'.

Root Cause

Root cause (langchain side)

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Other Dependencies

claude-agent-sdk: 0.2.87 httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.2.2 openai: 2.38.0 openai-agents: 0.17.3 opentelemetry-api: 1.42.1 opentelemetry-exporter-otlp-proto-http: 1.42.1 opentelemetry-sdk: 1.42.1 orjson: 3.11.9 packaging: 26.2 pydantic: 2.14.0a1 pytest: 9.0.3 pyyaml: 6.0.3 requests: 2.34.2 requests-toolbelt: 1.0.0 rich: 15.0.0 tenacity: 9.1.4 tiktoken: 0.13.0 typing-extensions: 4.15.0 uuid-utils: 0.16.0 vcrpy: 8.1.1 websockets: 16.0 wrapt: 2.2.1 xxhash: 3.7.0 zstandard: 0.25.0

Code Example

import langchain.agents
from langchain_core.language_models.llms import BaseLLM

---

Traceback (most recent call last):
  File "/Users/alex/Library/Application Support/JetBrains/PyCharm2026.1/scratches/scratch_2970.py", line 2, in <module>
    from langchain_core.language_models.llms import BaseLLM
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/langchain_core/language_models/llms.py", line 293, in <module>
    class BaseLLM(BaseLanguageModel[str], ABC):
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 249, in __new__
    set_model_fields(cls, config_wrapper=config_wrapper, ns_resolver=ns_resolver)
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 585, in set_model_fields
    fields, pydantic_extra_info, class_vars = collect_model_fields(
                                              ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 264, in collect_model_fields
    type_hints = _typing_extra.get_model_type_hints(cls, ns_resolver=ns_resolver)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 354, in get_model_type_hints
    hints[name] = try_eval_type(value, globalns, localns)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 409, in try_eval_type
    return eval_type(value, globalns, localns), True
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 433, in eval_type
    return _eval_type(value, globalns, localns, type_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 510, in _eval_type
    return typing._eval_type(  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/typing.py", line 415, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/typing.py", line 947, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
TypeError: 'function' object is not subscriptable
Unable to evaluate type annotation 'dict[str, Any] | None'.

---

rtn.__annotations__ = dict(selected_annotations)
RAW_BUFFERClick to expand / collapse

Submission checklist

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

#36896 (auto-closed by triage bot for not using the issue template; refiling under the bug-report template). Also #34537 (a closed-without-merging PR that proposed exactly the fix needed).

Reproduction Steps / Example Code (Python)

import langchain.agents
from langchain_core.language_models.llms import BaseLLM

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/Users/alex/Library/Application Support/JetBrains/PyCharm2026.1/scratches/scratch_2970.py", line 2, in <module>
    from langchain_core.language_models.llms import BaseLLM
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/langchain_core/language_models/llms.py", line 293, in <module>
    class BaseLLM(BaseLanguageModel[str], ABC):
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 249, in __new__
    set_model_fields(cls, config_wrapper=config_wrapper, ns_resolver=ns_resolver)
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 585, in set_model_fields
    fields, pydantic_extra_info, class_vars = collect_model_fields(
                                              ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_fields.py", line 264, in collect_model_fields
    type_hints = _typing_extra.get_model_type_hints(cls, ns_resolver=ns_resolver)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 354, in get_model_type_hints
    hints[name] = try_eval_type(value, globalns, localns)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 409, in try_eval_type
    return eval_type(value, globalns, localns), True
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 433, in eval_type
    return _eval_type(value, globalns, localns, type_params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/work/logfire/.venv/lib/python3.12/site-packages/pydantic/_internal/_typing_extra.py", line 510, in _eval_type
    return typing._eval_type(  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/typing.py", line 415, in _eval_type
    return t._evaluate(globalns, localns, type_params, recursive_guard=recursive_guard)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/alex/.local/share/uv/python/cpython-3.12.11-macos-aarch64-none/lib/python3.12/typing.py", line 947, in _evaluate
    eval(self.__forward_code__, globalns, localns),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
TypeError: 'function' object is not subscriptable
Unable to evaluate type annotation 'dict[str, Any] | None'.

Description

Problem

After importing langchain.agents, subsequent construction of BaseLLM (e.g. via from langchain_core.language_models import BaseLLM) fails with TypeError: 'function' object is not subscriptable when using the current Pydantic main branch / 2.14.0a1 alpha.

Root cause (langchain side)

In libs/core/langchain_core/utils/pydantic.py, _create_subset_model_v2 writes __annotations__ directly on a model class (verified on master bc5f1517cf, line 272):

https://github.com/langchain-ai/langchain/blob/bc5f1517cf7ac27addd4286e388228b8172b93b9/libs/core/langchain_core/utils/pydantic.py#L272

rtn.__annotations__ = dict(selected_annotations)

This breaks the invariant (in Python >= 3.10) that getattr(cls, '__annotations__', {}) and cls.__dict__.get('__annotations__', {}) should return the same thing. Pydantic main switched from the latter to the former in its safe_get_annotations helper (pydantic PR #13070), which exposes the langchain bug.

See Pydantic maintainer's diagnosis: https://github.com/pydantic/pydantic/issues/13097#issuecomment-4282311834

Impact

Any code that imports langchain.agents and then constructs BaseLLM (or any model inheriting from a class with a dict[...] string annotation) will fail once pydantic 2.14 ships. Caught by logfire's weekly CI against pydantic main:

Suggested fix

Avoid mutating __annotations__ directly in _create_subset_model_v2. The existing TODO comment in the code already anticipates this: "Determine if there is a more 'pydantic' way to preserve annotations." Using pydantic.create_model with fields passed normally (so pydantic sets up __annotations__ itself), or typing.get_type_hints(..., include_extras=True) as proposed in the closed PR #34537, would avoid the invariant violation.

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:55 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6031 Python Version: 3.12.11 (main, Aug 8 2025, 16:50:44) [Clang 20.1.4 ]

Package Information

langchain_core: 1.4.0 langchain: 1.3.2 langsmith: 0.8.5 langchain_openai: 1.2.2 langchain_protocol: 0.0.15 langgraph_sdk: 0.3.15

Optional packages not installed

deepagents deepagents-cli

Other Dependencies

claude-agent-sdk: 0.2.87 httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.2.2 openai: 2.38.0 openai-agents: 0.17.3 opentelemetry-api: 1.42.1 opentelemetry-exporter-otlp-proto-http: 1.42.1 opentelemetry-sdk: 1.42.1 orjson: 3.11.9 packaging: 26.2 pydantic: 2.14.0a1 pytest: 9.0.3 pyyaml: 6.0.3 requests: 2.34.2 requests-toolbelt: 1.0.0 rich: 15.0.0 tenacity: 9.1.4 tiktoken: 0.13.0 typing-extensions: 4.15.0 uuid-utils: 0.16.0 vcrpy: 8.1.1 websockets: 16.0 wrapt: 2.2.1 xxhash: 3.7.0 zstandard: 0.25.0

extent analysis

TL;DR

Avoid mutating __annotations__ directly in langchain_core.utils.pydantic by using pydantic.create_model instead.

Guidance

  • Review the langchain_core.utils.pydantic module, specifically the _create_subset_model_v2 function, to understand how __annotations__ is being modified.
  • Consider using pydantic.create_model to create models, allowing Pydantic to manage __annotations__ internally.
  • Verify that the issue is resolved by testing the creation of BaseLLM after importing langchain.agents with the modified code.
  • Investigate other parts of the codebase where __annotations__ might be modified directly to prevent similar issues.

Example

from pydantic import create_model

# Instead of modifying __annotations__ directly
# rtn.__annotations__ = dict(selected_annotations)

# Use create_model to let Pydantic manage annotations
Model = create_model('Model', **selected_annotations)

Notes

The suggested fix assumes that using pydantic.create_model is a viable alternative for creating models in langchain_core. Further investigation may be needed to ensure this approach works for all use cases.

Recommendation

Apply workaround: Use pydantic.create_model to create models instead of modifying __annotations__ directly, as this approach is more Pydantic-native and avoids the invariant violation.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING