Skip to main content
Agents can return structured data in a specific format using Pydantic models, ensuring type safety and validation.

Overview

By default, agents return unstructured text answers:
overview_default.py
With structured output, define the exact format you want:
overview_structured.py

Basic Example

Define a Pydantic model and pass it to response_format:

Complex Models

Nested Structures

Models can contain nested objects:
nested_structures.py

Lists of Objects

Extract multiple items:
lists_of_objects.py

Optional Fields

Use None for optional fields:

Use Cases

E-commerce Data Extraction

Extract structured product data:
ecommerce_extraction.py

Lead Generation

Extract structured contact information:
lead_generation.py

Job Listings

Extract job posting details:
job_listings.py

Social Media Data

Extract social media profiles:
social_media.py

Field Validation

Use Pydantic validators for data quality:
field_validation.py

Best Practices

1. Be Specific in Task Description

Match your task to the response format:
bp_specific_task.py

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:
error_handling.py

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
limitations_not_suitable.py

Complex Relationships

Very complex nested structures may be challenging:
limitations_complex.py

Next Steps

Configuration

Configure agent parameters

Lifecycle

Understand agent execution modes

Scraping

Alternative data extraction methods

Best Practices

Data extraction best practices