# Output Schema (/examples/agents/input-output/output-schema)



```python title="output_schema.py"
"""
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 [#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.

```python
# 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 [#run-the-example]

<Steps>
    <Step title="Set up your virtual environment">
      <CodeBlockTabs defaultValue="Mac">
        <CodeBlockTabsList>
          <CodeBlockTabsTrigger value="Mac">
            Mac
          </CodeBlockTabsTrigger>

          <CodeBlockTabsTrigger value="Windows">
            Windows
          </CodeBlockTabsTrigger>
        </CodeBlockTabsList>

        <CodeBlockTab value="Mac">
          ```bash
          uv venv --python 3.12
          source .venv/bin/activate
          ```
        </CodeBlockTab>

        <CodeBlockTab value="Windows">
          ```bash
          uv venv --python 3.12
          .venv\Scripts\activate
          ```
        </CodeBlockTab>
      </CodeBlockTabs>
    </Step>

  <Step title="Install dependencies">
    ```bash
    uv pip install -U agno openai
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeBlockTabs defaultValue="Mac/Linux">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="Mac/Linux">
          Mac/Linux
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="Windows">
          Windows
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="Mac/Linux">
        ```bash
        export OPENAI_API_KEY="your_openai_api_key_here"
        ```
      </CodeBlockTab>

      <CodeBlockTab value="Windows">
        ```bash
        $Env:OPENAI_API_KEY="your_openai_api_key_here"
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Step>

  <Step title="Run the example">
    Save the code above as `output_schema.py`, then run:

    ```bash
    python output_schema.py
    ```
  </Step>
</Steps>

Full source: [cookbook/02\_agents/02\_input\_output/output\_schema.py](https://github.com/agno-agi/agno/blob/8f36eaf2d18e91afa7b327eec66a3cd3685dcb87/cookbook/02_agents/02_input_output/output_schema.py)
