Skip to main content
Structured output allows agents to return data in a specific, predictable format. Instead of parsing natural language responses, you get typed structured data.
This page covers structured output with agents using createAgent. To use structured output directly on a model (outside of agents), see Models - Structured output.
LangChain’s prebuilt ReAct agent createAgent 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 structuredResponse key of the agent’s state.

Response format

Controls how the agent returns structured data. You can provide a Zod schema, any Standard Schema-compatible schema, or a JSON Schema object. By default, the agent uses a tool calling strategy, in which the output is created by an additional tool call. Certain models support native structured output, in which case the agent will use that strategy instead. You can control the behavior by wrapping ResponseFormat in a toolStrategy or providerStrategy function call:
The structured response is returned in the structuredResponse key of the agent’s final state.
Support for native structured output features is read dynamically from the model’s profile data if using langchain>=1.1. If data are not available, use another condition or specify manually:
If tools are specified, the model must support simultaneous use of tools and structured output.

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 a ProviderStrategy:
schema
required
The schema defining the structured output format. Supports:
  • Zod Schema: A zod schema
  • Standard Schema: Any schema implementing the Standard Schema spec
  • JSON Schema: A JSON schema object
LangChain automatically uses ProviderStrategy when you pass a schema type directly to createAgent.responseFormat and the model supports native structured output:
Provider-native structured output provides high reliability and strict validation because the model provider enforces the schema. Use it when available.
If the provider natively supports structured output for your model choice, it is functionally equivalent to write responseFormat: contactInfoSchema instead of responseFormat: providerStrategy(contactInfoSchema).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 a ToolStrategy:
schema
required
The schema defining the structured output format. Supports:
  • Zod Schema: A zod schema
  • Standard Schema: Any schema implementing the Standard Schema spec
  • JSON Schema: A JSON schema object
options.toolMessageContent
Custom content for the tool message returned when structured output is generated. If not provided, defaults to a message showing the structured response data.
options.handleError
Options parameter containing an optional handleError parameter for customizing the error handling strategy.
  • true: Catch all errors with default error template (default)
  • False: No retry, let exceptions propagate
  • (error: ToolStrategyError) => string | Promise<string>: retry with the provided message or throw the error

Custom tool message content

The toolMessageContent parameter allows you to customize the message that appears in the conversation history when structured output is generated:
Without toolMessageContent, we’d see:

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 a ToolMessage 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 the handleErrors parameter: Custom error message:
Handle specific exceptions only:
Handle multiple exception types:
No error handling: