Structured output - Docs by LangChain
Blog

Structured output - Docs by LangChain

2025.11.23
·Web·by Anonymous
#Structured Output#Agent#LangChain#Tool Calling#Pydantic

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:

  1. Provider Strategy (Native Structured Output):
This strategy leverages native structured output capabilities offered directly by certain model providers (e.g., OpenAI, Anthropic (Claude), xAI (Grok), Gemini). It is considered the most reliable method when available. When a schema type is directly passed to create_agent's response_format (e.g., responseformat=ContactInforesponse_format=ContactInfo), 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 ProviderStrategy(schema:type[SchemaT],strict:boolNone=None)ProviderStrategy(schema: type[SchemaT], strict: bool | None = None).
    • schema: A required parameter defining the structured output format. Supported types include Pydantic models (BaseModel subclasses), Python dataclasses, TypedDict classes, and JSON Schema dictionaries. For Pydantic models, a validated instance is returned; for others, a dictionary is returned.
    • strict: An optional boolean parameter (requires langchain>=1.2langchain>=1.2) to enable strict schema adherence, supported by some providers like OpenAI and xAI.
  • Benefit: Provider-native enforcement ensures robust schema adherence and validation.
  1. Tool Calling Strategy:
For models that lack native structured output support, LangChain employs a tool-calling mechanism to achieve the same result. This strategy is compatible with most modern models that support tool calling and is automatically selected if the Provider Strategy is not applicable or explicitly configured using 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 ToolStrategy(schema:type[SchemaT],toolmessagecontent:strNone=None,handleerrors:Union[bool,str,type[Exception],Tuple[type[Exception],...],Callable[[Exception],str]])ToolStrategy(schema: type[SchemaT], tool_message_content: str | None = None, handle_errors: Union[bool, str, type[Exception], Tuple[type[Exception], ...], Callable[[Exception], str]]).
    • schema: A required parameter defining the structured output format. Similar to ProviderStrategy, it supports Pydantic models, dataclasses, TypedDict classes, JSON Schema, and additionally, Union types to allow the model to choose among multiple schema options.
    • tool_message_content: An optional string to customize the content of the ToolMessage that 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] or Tuple[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 like StructuredOutputValidationError (for schema mismatches) and MultipleStructuredOutputsError (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.