Agent with Structured Outputs

Return a validated Pydantic object from a Claude agent.

Set output_schema to return a validated Pydantic object instead of free-form text.

Code

from agno.agent import Agent, RunOutput
from agno.models.anthropic import Claude
from pydantic import BaseModel, Field


class MovieScript(BaseModel):
    name: str = Field(description="Movie title")
    genre: str
    characters: list[str]
    storyline: str = Field(description="Three-sentence storyline")


movie_agent = Agent(
    model=Claude(id="claude-sonnet-4-6"),
    description="You help people write movie scripts.",
    output_schema=MovieScript,
)

if __name__ == "__main__":
    run: RunOutput = movie_agent.run("Write a thriller set in New York.")
    movie = run.content

    assert isinstance(movie, MovieScript)
    print(movie.model_dump_json(indent=2))

run.content is a MovieScript instance after Claude returns valid JSON and Agno validates it against the schema.

Agno merges the generated schema into output_config.format, preserving other settings such as effort, and adds the structured-outputs-2025-11-13 beta automatically. Let Agno populate the format when you use output_schema.

Anthropic lists Claude Sonnet 4.6 as supporting structured outputs. Refusals and responses stopped at max_tokens can end without a schema-valid result. Anthropic JSON outputs are also incompatible with citations and message prefilling; Agno disables document citations for these requests.

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U "agno[anthropic]"

Export your Anthropic API key

export ANTHROPIC_API_KEY="your_anthropic_api_key_here"

Run the agent

Save the code above as structured_output.py, then run:

python structured_output.py

Developer Resources