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

RunnableWithFallbacks.__Getattr__ Crashes With NameError On Unresolvable Forward-Ref Annotations

Calling methods like with_structured_output() on a RunnableWithFallbacks instance raises NameError when the method's return annotation contains a forward reference that cannot be resolved in the defining module's global namespace, breaking fallback chain composition for chat models.

highConfidence 95%LangchainMemorylangchain-coreVector StoresAffected V1.2.16

Origin Analysis

In langchain_core/runnables/fallbacks.py, the _returns_runnable() helper calls typing.get_type_hints(attr) to inspect the return annotation. If the method's annotation contains a string forward reference (e.g., 'Runnable') that is not present in the method's defining module __globals__ (common when a chat model inherits from a base class whose module does not import the referenced name), get_type_hints raises NameError during evaluation. This exception is unhandled inside RunnableWithFallbacks.__getattr__, causing the entire attribute lookup to fail and preventing any use of the method.
1. Instantiate a primary chat model that has a method with an unresolvable forward-ref return annotation (e.g., ChatXAI). 2. Wrap it with a fallback using primary.with_fallbacks([fallback]). 3. Call chain_with_fallbacks.with_structured_output(TestSchema). 4. Observe NameError: name 'Runnable' is not defined inside _returns_runnable.

Fixing Code Block

Edge Case Audit

The change may hide legitimate annotation errors and can cause methods that actually return a Runnable to not be re-wrapped for fallback propagation when their annotations cannot be resolved at that moment. This could lead to subtle behavioral differences in environments where forward references are normally resolvable but temporarily unavailable (e.g., during circular imports or dynamic namespace modifications). Thread safety is not directly affected, but concurrent access to global namespaces might produce inconsistent behavior. Rollback: if this fix causes regressions where fallback wrapping is expected but missing, revert to the previous behavior and instead handle exceptions at a higher level (e.g., in __getattr__) or improve the global namespace passed to get_type_hints by collecting names from the entire class hierarchy. Defensive suggestion: log a debug message when introspection fails to aid diagnosis.

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