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

GenericFakeChatModel Does Not Implement With_structured_output

GenericFakeChatModel raises NotImplementedError when calling with_structured_output, preventing testing of structured output parsing with fake chat models.

mediumConfidence 95%LangChainEmbeddingsChat ModelsAffected V1.2.22

Origin Analysis

BaseChatModel.with_structured_output raises NotImplementedError because GenericFakeChatModel does not override with_structured_output or provide a custom bind_tools implementation; it inherits the default which is not suitable for structured output.
1. Create a Pydantic model (e.g., Parrot). 2. Instantiate GenericFakeChatModel with messages containing AIMessage with a ToolCall matching the schema. 3. Call model.with_structured_output(Parrot). 4. Invoke the resulting runnable. Observe NotImplementedError at the point of calling with_structured_output.

Fixing Code Block

def with_structured_output(self, schema, include_raw=False, **kwargs): from langchain_core.runnables import RunnableLambda from pydantic import BaseModel if include_raw: raise NotImplementedError('include_raw is not supported for GenericFakeChatModel') if not (isinstance(schema, type) and issubclass(schema, BaseModel)): raise ValueError('schema must be a Pydantic BaseModel subclass') def _parse(ai_message): if not ai_message.tool_calls: raise ValueError('No tool calls found in fake message') tool_call = ai_message.tool_calls[0] return schema.model_validate(tool_call['args']) return self | RunnableLambda(_parse)
Adds a custom with_structured_output method to GenericFakeChatModel that composes the model with a parser. The parser extracts the first tool call from the AIMessage and validates its 'args' into the specified Pydantic schema, bypassing the default bind_tools path that raises NotImplementedError.

Edge Case Audit

This implementation assumes the model's output is an AIMessage with at least one tool call and that the first tool call's args are valid for the schema. It does not support include_raw=True. In concurrent or multi-threaded scenarios where the fake model's message iterator is shared, the iterator may be exhausted unexpectedly, leading to StopIteration errors. If LangChain's ToolCall structure changes in future versions, the key access may fail. Rollback: remove this override; the model will revert to raising NotImplementedError for structured output.

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