When to use structured outputsUse structured outputs when you need validated JSON after an agent completes a multi-turn workflow with tools (file searches, command execution, web research, etc.).For single API calls without tool use, see API Structured Outputs.
Why use structured outputs
Structured outputs provide reliable, type-safe integration with your applications:- Validated structure: Always receive valid JSON matching your schema
- Simplified integration: No parsing or validation code needed
- Type safety: Use with TypeScript or Python type hints for end-to-end safety
- Clean separation: Define output requirements separately from task instructions
- Tool autonomy: Agent chooses which tools to use while guaranteeing output format
How structured outputs work
1
Define your JSON schema
Create a JSON Schema that describes the structure you want the agent to return. The schema uses standard JSON Schema format.
2
Add the outputFormat parameter
Include the
outputFormat parameter in your query options with type: "json_schema" and your schema definition.3
Run your query
The agent uses any tools it needs to complete the task (file operations, commands, web search, etc.).
4
Access validated output
The agent’s final result will be valid JSON matching your schema, available in
message.structured_output.Supported JSON Schema features
The Agent SDK supports the same JSON Schema features and limitations as API Structured Outputs. Key supported features:- All basic types: object, array, string, integer, number, boolean, null
enum,const,required,additionalProperties(must befalse)- String formats:
date-time,date,email,uri,uuid, etc. $ref,$def, anddefinitions
Example: TODO tracking agent
Here’s a complete example showing an agent that searches code for TODOs and extracts git blame information:Error handling
If the agent cannot produce valid output matching your schema, you’ll receive an error result:Related resources
- JSON Schema documentation
- API Structured Outputs - For single API calls
- Custom tools - Define tools for your agents
- TypeScript SDK reference - Full TypeScript API
- Python SDK reference - Full Python API