Skip to main content
Pass an output_schema to constrain the agent’s final answer to a typed shape. The SDK validates the model’s output against the schema and returns a fully typed object.

Pydantic / Zod

from pydantic import BaseModel
from opperai.agent import Agent

class Summary(BaseModel):
    title: str
    key_points: list[str]
    sentiment: str  # "positive" | "negative" | "neutral"

agent = Agent(
    name="summarizer",
    instructions="Summarize the input.",
    output_schema=Summary,
)

result = await agent.run("Python 3.12 brings major performance improvements...")

summary = result.output  # validated Summary instance
print(summary.title)
print(summary.sentiment)
for point in summary.key_points:
    print("-", point)

Other schema sources

Both SDKs accept raw JSON Schema. Python additionally accepts dataclasses and TypedDict. TypeScript accepts any Standard Schema implementation (Valibot, ArkType, etc.).
Python
output_schema = {
    "type": "object",
    "properties": {
        "title": {"type": "string"},
        "key_points": {"type": "array", "items": {"type": "string"}},
    },
    "required": ["title", "key_points"],
}

Without a schema

Omit output_schema and the agent returns a string in result.output.