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
Native schema support depends on the model. Agno requests JSON and parses it into the Pydantic model when native schema output is unavailable.
Fallback parsing failures are logged and can leave response.content as a string. Check its type before accessing schema fields when the model lacks native schema support.

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 checks field constraints when the response is parsed:

Use Optional for Uncertain Fields

Mark fields as optional when data might not be available:

Common Patterns

Data Extraction

Classification

Multi-Item Generation

JSON Mode

Agno uses a JSON fallback automatically when the model lacks native schema support. Set use_json_mode=True to force JSON mode for a model that supports native structured output:
JSON mode instructs the model to respond in JSON but doesn't guarantee schema compliance. Prefer models with native structured output support.