Skip to main content

Overview

Structured output lets you define the exact data format you want from agent execution using Pydantic models. Instead of parsing unstructured text, you get validated Python objects with type safety and automatic serialization.
Structured output is ideal for data extraction tasks where you need reliable, type-safe results rather than free-form text.

Quick Start

Defining Output Models

Basic Models

With Field Descriptions

Help the LLM understand what to extract:

Nested Models

Lists of Objects

From examples/features/custom_output.py:29-31.

Accessing Results

Via AgentHistoryList

With Sandbox Execution

Structured output works with sandbox:
From examples/sandbox/structured_output.py:17-46.
When using sandbox, use get_structured_output(Model) instead of structured_output property, as the private _output_model_schema attribute isn’t serialized.

Complex Examples

E-commerce Product Catalog

Social Media Post Analysis

Financial Data Extraction

Validation

Built-in Validators

If the LLM returns invalid data, Pydantic will raise ValidationError.

Custom Validators

Error Handling

Validation Errors

Optional Fields

Make fields optional when data might not be available:

Fallback Values

Best Practices

1. Clear Field Descriptions

Help the LLM understand what to extract:

2. Use Enums for Fixed Values

3. Provide Examples in Task

4. Keep Models Focused

Integration with Tools

Structured output works with custom tools:

Actor API Integration

Use structured extraction with Actor API:
From browser_use/actor/page.py:491-554.

See Also