Chat Structured Output
Compare JSON mode, strict, and guided structured output for a Pydantic movie schema.
The source's rating: Dict[str, int] emits an open-ended object and is omitted from the generated required fields. That violates OpenAI's strict schema contract. Apply the typed ratings fix below before running the strict examples.
"""
Openai Structured Output
========================
Cookbook example for `openai/chat/structured_output.py`.
"""
import asyncio
from typing import Dict, List
from agno.agent import Agent, RunOutput # noqa
from agno.models.openai import OpenAIChat
from pydantic import BaseModel, Field
from rich.pretty import pprint # noqa
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
class MovieScript(BaseModel):
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.",
)
name: str = Field(..., description="Give a name to this movie")
characters: List[str] = Field(..., description="Name of characters for this movie.")
storyline: str = Field(
..., description="3 sentence storyline for the movie. Make it exciting!"
)
rating: Dict[str, int] = Field(
...,
description="Your own rating of the movie. 1-10. Return a dictionary with the keys 'story' and 'acting'.",
)
# Agent that uses JSON mode
json_mode_agent = Agent(
model=OpenAIChat(id="gpt-5.6-luna"),
description="You write movie scripts.",
output_schema=MovieScript,
use_json_mode=True,
)
# Agent that uses structured outputs with strict_output=True (default)
structured_output_agent = Agent(
model=OpenAIChat(id="gpt-5.6-luna"),
description="You write movie scripts.",
output_schema=MovieScript,
)
# Agent with strict_output=False (guided mode)
guided_output_agent = Agent(
model=OpenAIChat(id="gpt-5.6-luna", strict_output=False),
description="You write movie scripts.",
output_schema=MovieScript,
)
# Get the response in a variable
# json_mode_response: RunOutput = json_mode_agent.run("New York")
# pprint(json_mode_response.content)
# structured_output_response: RunOutput = structured_output_agent.run("New York")
# pprint(structured_output_response.content)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
# --- Sync ---
json_mode_agent.print_response("New York")
structured_output_agent.print_response("New York")
guided_output_agent.print_response("New York")
# --- Sync + Streaming ---
structured_output_agent.print_response("New York", stream=True)
# --- Async + Streaming ---
async def main():
await structured_output_agent.aprint_response("New York", stream=True)
asyncio.run(main())Required Schema Fix
In your saved copy, define Rating before MovieScript:
class Rating(BaseModel):
story: int = Field(..., description="Story rating from 1 to 10.")
acting: int = Field(..., description="Acting rating from 1 to 10.")Replace the entire rating: Dict[str, int] = Field(...) declaration inside MovieScript with:
rating: Rating = Field(..., description="Your own ratings of the movie.")Keep the existing imports, other fields, agents and run calls. JSON mode requests valid JSON; native structured output sends JSON Schema, with strict enforcement enabled by default. strict_output=False relaxes provider enforcement. The descriptions ask for ratings from 1 to 10 but do not enforce that range locally.
output_schema describes the expected type. If parsing or validation fails, result.content can remain a string. Before accessing schema fields in a run result, use isinstance(result.content, YourSchema), replacing YourSchema with the class you passed as output_schema.
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 source as structured_output.py, apply the required schema fix above, then run:
python structured_output.pyFull source: cookbook/90_models/openai/chat/structured_output.py