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-TypesafeAffected 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