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 either a Zod object or JSON schema. 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, Grok, Gemini). This is the most reliable method when available. To use this strategy, configure aProviderStrategy:
required
The schema defining the structured output format. Supports:
- Zod Schema: A zod schema
- 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, which is most modern models. To use this strategy, configure aToolStrategy:
required
The schema defining the structured output format. Supports:
- Zod Schema: A zod schema
- JSON Schema: A JSON schema object
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 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: