Basic Structured Output
Define anoutput schema using Zod:
Type Safety
The output is fully typed based on your Zod schema:Complex Schemas
Nested Objects
Arrays and Lists
Optional Fields
Real-World Examples
Research Planning
From the source code examples:research_bot.ts:8:23
Report Generation
research_bot.ts:25:33
Financial Analysis
finanicals_bot.ts:10:46
Extracting Output
Use thetoOutput() helper to extract structured output:
Streaming with Structured Output
Structured output works with streaming:Validation and Descriptions
Add descriptions to schema fields to guide the model:Combining with Context
Structured output works with context variables:Error Handling
Handle schema validation errors:Best Practices
Add Descriptions
Add Descriptions
Always add descriptions to your schema fields to guide the model:
Use Enums for Categories
Use Enums for Categories
Use enums instead of free-form strings for categories:
Validate Ranges
Validate Ranges
Add validation for numeric ranges:
Clear Field Names
Clear Field Names
Use descriptive field names that clearly indicate the expected content:
Next Steps
Context Variables
Share state between agents
Streaming
Stream agent responses
API Reference
Full API documentation