AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Langchain-Typesafe: Recursive `State` Alias Leaves Input Schema Unresolvable

The `TypeSafeClassifier` input schema generation fails with `PydanticUserError` because the private recursive `_StateValue` type alias is not resolvable in the dynamically created `TypeSafeClassifierInput` model namespace. This breaks `get_input_jsonschema()`, `as_tool()`, schemaful graph serialization, and Runnable composition schema introspection.

highConfidence 92%Langchain-TypesafeChainslangchain-coreAffected V0.0.1a3

Origin Analysis

`ClassifierRequest` references `State`, which recursively references the private `_StateValue` alias. `_StateValue` is defined in `langchain_typesafe.types`, but LangChain creates `TypeSafeClassifierInput` using the `langchain_typesafe.classifier` module namespace. The private alias is absent there, and because it is an implicit recursive `TypeAlias` without a named schema boundary, Pydantic cannot resolve the forward reference and falls into recursive expansion, causing either `PydanticUserError` or `RecursionError`.
Instantiate `TypeSafeClassifier(api_key="not-used")`, then call `classifier.get_input_jsonschema()`, `classifier.get_input_schema().model_validate(request)`, `classifier.as_tool()`, `classifier.get_graph().to_json(with_schemas=True)`, `RunnableParallel(classify=classifier).get_input_schema()`, or `RunnableBranch((lambda _: True, classifier), classifier).get_input_schema()`. All fail with `PydanticUserError: 'TypeSafeClassifierInput' is not fully defined; you should define '_StateValue', then call 'TypeSafeClassifierInput.model_rebuild()'`.

Fixing Code Block

Edge Case Audit

The fix requires `typing_extensions>=4.12` (already present in the stated environment) and is compatible with Python 3.10+. It uses a string-based alias RHS, so static type checkers may need re-evaluation. No runtime behavior changes are expected for invoke/ainvoke/batch paths, but schema consumers that relied on the previously unreachable expanded form could see a new `$defs` structure. Rollback: revert the `TypeAliasType` definition to the original implicit `TypeAlias` if unexpected schema regressions appear. Ensure `BaseMessage` and `Sequence` are imported in the module where this code lives.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state