Structured output - Docs by LangChain
Key Points
- 1LangChain's structured output enables agents to return data in specific, predictable formats like JSON or Pydantic models, facilitating direct application use.
- 2It supports two main strategies: `ProviderStrategy` leverages native model API capabilities for reliability, while `ToolStrategy` uses tool calling for broader compatibility across models.
- 3LangChain's `ToolStrategy` includes robust error handling with intelligent retry mechanisms for issues like schema validation failures or multiple structured outputs, along with customizable tool messages.
The provided document details LangChain's approach to structured output for agents, enabling them to return data in a predictable and directly usable format such as JSON objects, Pydantic models, or dataclasses, rather than free-form natural language. This functionality is primarily controlled via the response_format parameter within the create_agent function, with the resulting structured data available under the structured_response key in the agent's final state.
LangChain employs two primary strategies for achieving structured output:
- Provider Strategy (Native Structured Output):
create_agent's response_format (e.g., ), LangChain automatically defaults to this strategy if the chosen model and provider support it.
- Mechanism: The model provider's API directly enforces the output schema, leading to high reliability and strict validation.
- Configuration: Explicitly configured using .
schema: A required parameter defining the structured output format. Supported types include Pydantic models (BaseModelsubclasses), Python dataclasses,TypedDictclasses, and JSON Schema dictionaries. For Pydantic models, a validated instance is returned; for others, a dictionary is returned.strict: An optional boolean parameter (requires ) to enable strict schema adherence, supported by some providers like OpenAI and xAI.
- Benefit: Provider-native enforcement ensures robust schema adherence and validation.
- Tool Calling Strategy:
ToolStrategy.
- Mechanism: The core methodology involves instructing the Language Model (LLM) to "call" a hypothetical tool whose input arguments precisely match the desired structured output schema. The LLM is prompted to generate a tool call with arguments conforming to this schema. LangChain then captures, validates, and processes these arguments as the structured output.
- Configuration: Explicitly configured using .
schema: A required parameter defining the structured output format. Similar toProviderStrategy, it supports Pydantic models, dataclasses,TypedDictclasses, JSON Schema, and additionally,Uniontypes to allow the model to choose among multiple schema options.tool_message_content: An optional string to customize the content of theToolMessagethat appears in the conversation history after structured output is generated. By default, it shows the structured response data.handle_errors: This parameter defines the strategy for handling structured output validation failures and enables intelligent retry mechanisms.True(default): Catches all errors with a default error template, prompting the model to retry.str: Uses a custom error message, prompting a retry.type[Exception]orTuple[type[Exception], ...]: Retries only for specified exception types using a default message, otherwise propagates the exception.Callable[[Exception], str]: A custom function that receives the exception and returns an error message, allowing fine-grained control over error feedback. LangChain defines specific error types likeStructuredOutputValidationError(for schema mismatches) andMultipleStructuredOutputsError(when the model incorrectly calls multiple output tools) that can be handled by this callable.False: Disables retry, causing exceptions to propagate immediately.
Both strategies return the structured response in the structured_response key of the agent's state. The choice between strategies is either automatic (based on model capabilities) or explicitly controlled via the response_format parameter's specific strategy class. The ToolStrategy offers robust error handling to guide the LLM towards correct schema adherence through intelligent retry loops and informative feedback.