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 wrappingResponseFormat in a toolStrategy or providerStrategy function call:
structuredResponse 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 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
ProviderStrategy when you pass a schema type directly to createAgent.responseFormat and the model supports native structured output:
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 aToolStrategy:
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
ThetoolMessageContent parameter allows you to customize the message that appears in the conversation history when structured output is generated:
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 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 thehandleErrors parameter:
Custom error message:
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

