Skip to main content
Get structured, typed responses from agents using Pydantic models.

Define a schema

from pydantic import BaseModel, Field

class MovieReview(BaseModel):
    title: str = Field(description="The title of the movie")
    rating: int = Field(ge=1, le=10, description="Rating from 1-10")
    genre: str = Field(description="The movie's genre(s)")
    summary: str = Field(description="A brief summary of the movie")
    pros: list[str] = Field(description="List of positive aspects")
    cons: list[str] = Field(description="List of negative aspects")
    recommendation: str = Field(description="Who should watch this movie")

Create an agent with the schema

from polos import Agent

movie_reviewer = Agent(
    id="movie_reviewer",
    provider="openai",
    model="gpt-4o-mini",
    system_prompt="You are a professional movie critic. Provide comprehensive reviews.",
    output_schema=MovieReview,  # Pydantic model for structured output
)

Get typed responses

result = await movie_reviewer.run(client, "Review the movie 'The Matrix'")

# result.result is a validated MovieReview instance
print(result.result.title)   # "The Matrix"
print(result.result.rating)  # 9
print(result.result.pros)    # ["Innovative visual effects", ...]

Run it

git clone https://github.com/polos-dev/polos.git
cd polos/python-examples/02-structured-output
cp .env.example .env  # Add your POLOS_PROJECT_ID and OPENAI_API_KEY
uv sync
python worker.py      # Terminal 1
python main.py        # Terminal 2
Open http://localhost:5173 to view your agents and workflows, run them from the UI, and see execution traces. Python example on GitHub | TypeScript example on GitHub