tensorfolk

Structured outputs

When code consumes model output, constrain it to a schema and validate it.

Evolving: the basics hold, details shift. Reviewed 17 Oct 2026.

Key ideas

In code

python
from pydantic import BaseModel, Field
from pydantic_ai import Agent

class Invoice(BaseModel):
    vendor: str
    total: float = Field(description='Grand total incl. tax')
    currency: str = Field(pattern='^[A-Z]{3}$')

agent = Agent('anthropic:claude-sonnet-5-5', output_type=Invoice)
result = agent.run_sync(invoice_text)
print(result.output.total)

Common mistakes

Go deeper