create_react_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.
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
: No structured outputProviderStrategy
for models supporting native structured output (OpenAI, Grok)ToolStrategy
for all other modelsstructured_response
key of the agent’s final state.ProviderStrategy
:
BaseModel
subclasses with field validationProviderStrategy
when you pass a schema type directly to create_react_agent.response_format
and the model supports native structured output:
response_format=ProductReview
instead of response_format=ToolStrategy(ProductReview)
. In either case, if structured output is not supported, the agent will fall back to a tool calling strategy.ToolStrategy
:
BaseModel
subclasses with field validationTrue
.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 propagatetool_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:
ToolMessage
and prompts the model to retry:
handle_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
: