Skip to content

Importing langchain.agents breaks BaseLLM class construction on Pydantic main #37835

@alexmojaki

Description

@alexmojaki

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):

rtn.__annotations__ = dict(selected_annotations)

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: pydantic/pydantic#13097 (comment)

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

Metadata

Metadata

Labels

bugRelated to a bug, vulnerability, unexpected error with an existing featurecore`langchain-core` package issues & PRsexternal

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions