Output Schema
Use `output_schema` to return structured data that matches a Pydantic model.
"""
Output Schema
=============================
Use `output_schema` to return structured data that matches a Pydantic model.
"""
from typing import List
from agno.agent import Agent, RunOutput # noqa
from agno.models.openai import OpenAIResponses
from pydantic import BaseModel, Field
from rich.pretty import pprint # noqa
class BreakingNewsSummary(BaseModel):
topic: str = Field(..., description="The topic or region being summarized")
summary: str = Field(
..., description="A concise summary of the latest developments"
)
key_updates: List[str] = Field(
..., description="Important updates or headlines related to the topic"
)
overall_sentiment: str = Field(
..., description="Overall tone of the news coverage, such as positive or mixed"
)
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
description="You summarize current events into clean structured outputs.",
output_schema=BreakingNewsSummary,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
run: RunOutput = agent.run("Latest news from France?")
pprint(run.content)Check the result and its sources
A completed run can retain raw text if output parsing fails. Check isinstance(run.content, BreakingNewsSummary) before accessing model attributes; completion status alone does not establish a validated object. Schema validation checks shape and types, not factual accuracy.
The source prompt asks for latest news, but this agent has no retrieval tool or supplied news report. Use it to explore output shape, not to establish current events. To summarize real news, supply a dated report or add an explicit retrieval tool and validate the sources.
# In the saved example, replace its agent.run(...) and pprint(...) lines.
from pathlib import Path
news_text = Path("news-report.txt").read_text(encoding="utf-8")
run = agent.run("Summarize only this supplied dated news report:\n" + news_text)
if isinstance(run.content, BreakingNewsSummary):
pprint(run.content.model_dump())
else:
print("No validated news summary returned:", run.content)Create news-report.txt with the dated source report before using this adaptation.
Run the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openaiExport your OpenAI API key
export OPENAI_API_KEY="your_openai_api_key_here"Run the example
Save the code above as output_schema.py, then run:
python output_schema.pyFull source: cookbook/02_agents/02_input_output/output_schema.py