create_agent handles structured output automatically. The user sets their desired structured output schema, and when the model generates the structured data, it’s captured, validated, and returned in the 'structured_response' key of the agent’s state.
Response format
Useresponse_format to control how the agent returns structured data:
ToolStrategy[StructuredResponseT]: Uses tool calling for structured outputProviderStrategy[StructuredResponseT]: Uses provider-native structured outputtype[StructuredResponseT]: Schema type - automatically selects best strategy based on model capabilitiesNone: Structured output not explicitly requested
ProviderStrategyif the model and provider chosen supports native structured output (e.g. OpenAI, Anthropic (Claude), or xAI (Grok)).ToolStrategyfor all other models.
Support for native structured output features is read dynamically from the model’s profile data if using If tools are specified, the model must support simultaneous use of tools and structured output.
langchain>=1.1. If data are not available, use another condition or specify manually:structured_response key of the agent’s final state.
Provider strategy
Some model providers support structured output natively through their APIs (e.g. OpenAI, xAI (Grok), Gemini, Anthropic (Claude)). This is the most reliable method when available. To use this strategy, configure aProviderStrategy:
The
strict param requires langchain>=1.2.required
The schema defining the structured output format. Supports:
- Pydantic models:
BaseModelsubclasses with field validation. Returns validated Pydantic instance. - Dataclasses: Python dataclasses with type annotations. Returns dict.
- TypedDict: Typed dictionary classes. Returns dict.
- JSON Schema: Dictionary with JSON schema specification. Must include top-level
titleanddescriptionkeys. Returns dict.
LangChain automatically uses
ProviderStrategy when you pass a schema type directly to create_agent.response_format and the model supports native structured output:
If the provider natively supports structured output for your model choice, it is functionally equivalent to write
response_format=ProductReview instead of response_format=ProviderStrategy(ProductReview).In either case, if structured output is not supported, the agent will fall back to a tool calling strategy.Tool calling strategy
For models that don’t support native structured output, LangChain uses tool calling to achieve the same result. This works with all models that support tool calling (most modern models). To use this strategy, configure aToolStrategy:
required
The schema defining the structured output format. Supports:
- Pydantic models:
BaseModelsubclasses with field validation. Returns validated Pydantic instance. - Dataclasses: Python dataclasses with type annotations. Returns dict.
- TypedDict: Typed dictionary classes. Returns dict.
- JSON Schema: Dictionary with JSON schema specification. Must include top-level
titleanddescriptionkeys. Returns dict. - Union types: Multiple schema options. The model will choose the most appropriate schema based on the context.
Custom content for the tool message returned when structured output is generated.
If not provided, defaults to a message showing the structured response data.
Error handling strategy for structured output validation failures. Defaults to
True.True: Catch all errors with default error templatestr: Catch all errors with this custom messagetype[Exception]: Only catch this exception type with default messagetuple[type[Exception], ...]: Only catch these exception types with default messageCallable[[Exception], str]: Custom function that returns error messageFalse: No retry, let exceptions propagate
Custom tool message content
Thetool_message_content parameter allows you to customize the message that appears in the conversation history when structured output is generated:
tool_message_content, our final ToolMessage would be:
Error handling
Models can make mistakes when generating structured output via tool calling. LangChain provides intelligent retry mechanisms to handle these errors automatically.Multiple structured outputs error
When a model incorrectly calls multiple structured output tools, the agent provides error feedback in aToolMessage and prompts the model to retry:
Schema validation error
When structured output doesn’t match the expected schema, the agent provides specific error feedback:Error handling strategies
You can customize how errors are handled using thehandle_errors parameter:
Custom error message:
handle_errors is a string, the agent will always prompt the model to re-try with a fixed tool message:
handle_errors is an exception type, the agent will only retry (using the default error message) if the exception raised is the specified type. In all other cases, the exception will be raised.
Handle multiple exception types:
handle_errors is a tuple of exceptions, the agent will only retry (using the default error message) if the exception raised is one of the specified types. In all other cases, the exception will be raised.
Custom error handler function:
StructuredOutputValidationError:
MultipleStructuredOutputsError:
Model refusal
When the model refuses, it returns no structured output. WithProviderStrategy, the agent raises StructuredOutputValidationError. With ToolStrategy, the agent ends without a structured_response. To raise ModelRefusalError for either strategy, add ModelRefusalMiddleware:
ModelRefusalMiddleware requires langchain>=1.4.9. See Handle a refusal for ways to respond.
Handle parsing errors in chains
The strategies above, includinghandle_errors retries, apply to agents created with create_agent. Chains composed with the pipe operator (for example, prompt | model | parser) have no built-in retry mechanism for output parsing failures. To handle malformed model output in a chain, use one of the following approaches:
- Model-level structured output: Bind the schema to the model with
with_structured_output. When the provider supports native structured output, it enforces the schema and avoids most parsing failures. See Models - Structured output. Use this approach when the model supports it. - Fix malformed output with a model: Wrap the output parser in an
OutputFixingParserfromlangchain-classic. On a parsing failure, the wrapper sends the malformed output, the parser’s format instructions, and the error message to a model and parses the corrected response.
OutputFixingParser requires the langchain-classic package. Before v1, it was available as langchain.output_parsers.OutputFixingParser.max_retries controls how many fix attempts are made before the original OutputParserException is raised. Both sync (invoke) and async (ainvoke) execution are supported.
OutputFixingParser sends the malformed completion, the parser’s format instructions, and the error. It does not include the original prompt. If correcting the output requires the original prompt for context, use RetryOutputParser from langchain-classic instead. It is invoked through its parse_with_prompt(completion, prompt_value) method rather than parse, so it cannot be piped directly as the final step of a chain.
Connect these docs to your agent of choice via MCP for real-time answers.

