Overview
By default, agents return unstructured text answers:Basic Example
Define a Pydantic model and pass it toresponse_format:
Complex Models
Nested Structures
Models can contain nested objects:Lists of Objects
Extract multiple items:Optional Fields
UseNone for optional fields:
Use Cases
E-commerce Data Extraction
Extract structured product data:Lead Generation
Extract structured contact information:Job Listings
Extract job posting details:Social Media Data
Extract social media profiles:Field Validation
Use Pydantic validators for data quality:Best Practices
1. Be Specific in Task Description
Match your task to the response format:2. Use Appropriate Types
Choose Python types that match the data:3. Make Optional Fields Explicit
Don’t assume data will always be present:4. Use Field Descriptions
Help the agent understand what you want:5. Start Simple, Then Expand
Begin with basic models:Error Handling
Handle validation errors:Limitations
Not Suitable For
Structured output works best for data extraction, not for:- Open-ended creative tasks
- Tasks requiring explanation or reasoning
- When you want natural language responses

