Skip to main content
Structured output constrains an agent’s response to match a Pydantic schema. Instead of parsing free-form text, you get a validated object with typed fields.

Basic Usage

Define a Pydantic model and pass it as output_schema:

How It Works

When you set output_schema, Agno:
  1. Converts your Pydantic model to a JSON schema
  2. Passes this schema to the model’s structured output API (if supported)
  3. Validates the response against your schema
  4. Returns a typed Pydantic object in response.content
Most major providers support structured output natively: OpenAI, Anthropic, Google.

Control output_schema Per-Run

Override or set the schema at run time:
This is useful when one agent handles multiple tasks with different output formats.

With Tools

Structured output works alongside tools. The agent calls tools during execution, then formats the final response according to your schema:

Schema Design Tips

Use Field Descriptions

Descriptions guide the model on what to generate:

Use Constraints

Pydantic validators ensure valid output:

Use Optional for Uncertain Fields

Mark fields as optional when data might not be available:

Common Patterns

Data Extraction

Classification

Multi-Item Generation

Fallback with use_json_mode

Enable JSON mode for models that don’t support structured output natively:
JSON mode instructs the model to respond in JSON but doesn't guarantee schema compliance. Prefer models with native structured output support.