Structured Output
Use of Pydantic models for structured output with OpenRouter's Responses API.
Pass a Pydantic model as output_schema to request a JSON schema through OpenRouter's Responses API. Agno parses the result locally; check its type before using schema fields.
from typing import List
from agno.agent import Agent
from agno.models.openrouter import OpenRouterResponses
from pydantic import BaseModel, Field
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
class MovieScript(BaseModel):
name: str = Field(..., description="Give a name to this movie")
setting: str = Field(
..., description="Provide a nice setting for a blockbuster movie."
)
ending: str = Field(
...,
description="Ending of the movie. If not available, provide a happy ending.",
)
genre: str = Field(
...,
description="Genre of the movie. If not available, select action, thriller or romantic comedy.",
)
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(
..., description="3 sentence storyline for the movie. Make it exciting!"
)
agent = Agent(
model=OpenRouterResponses(id="openai/gpt-oss-20b", reasoning={"enabled": True}),
description="You write movie scripts.",
output_schema=MovieScript,
)
response = agent.run("New York")
if isinstance(response.content, MovieScript):
print(response.content.model_dump_json(indent=2))
else:
print("The response did not validate as MovieScript:", response.content)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passRun the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno openaiSet your OpenRouter API key
Use an account with access to the selected model's structured-output capability.
export OPENROUTER_API_KEY="your_openrouter_api_key_here"Run the example
Save the code above as structured_output.py, then run:
python structured_output.pyAdapted from the cookbook example.