Structured Outputs

This is the feature Pydantic AI is named for and built around: you declare a Pydantic model as your agent's output type, and you get back a validated instance of it — not a string to parse, not JSON to hope about, but a real typed object. It turns the single most brittle part of LLM applications into the most reliable.

The Agent post noted that a Pydantic AI agent is parameterized by an output type. This post is about that output type — structured outputs, the framework’s flagship capability. If you take one thing from this series, it’s this: Pydantic AI makes “get validated, typed data out of an LLM” a solved, first-class operation. This post covers how it works, why it’s more than convenience, and what it means for building reliable applications.

The problem: LLMs return text, apps need data

An LLM produces text. But your application almost never wants text — it wants data: an Order with items and a total, a Classification with a label and confidence, a list of extracted Contact records. The traditional path from text to data is painful and fragile:

  1. Prompt the model to return JSON in a specific shape.
  2. Parse the returned string as JSON (which may be malformed, wrapped in prose, or subtly wrong).
  3. Validate the parsed data has the right fields and types (which it may not).
  4. Handle all the ways steps 2 and 3 fail.

This “prompt-parse-validate-handle” dance is everywhere in LLM code, and it’s brittle: the model returns almost-valid JSON, or the right shape with a wrong type, or extra commentary around it, and your parsing breaks — often silently, propagating bad data downstream. Structured outputs replace this entire dance with a declaration.

The solution: declare the type

With Pydantic AI, you define the output you want as a Pydantic model and tell the agent that’s its output type. The framework then ensures the model’s output conforms to it, validated, and hands you back a typed instance:

# Illustrative shape — see the Pydantic AI docs for exact API.
from pydantic import BaseModel
from pydantic_ai import Agent

class Order(BaseModel):
    item: str
    quantity: int
    total: float

agent = Agent("openai:gpt-...", output_type=Order)

result = agent.run_sync("Two coffees at $3.50 each")
order = result.output          # this is a validated Order instance
print(order.quantity, order.total)   # typed fields, IDE autocomplete, no parsing

result.output is an Order — a real, validated Python object with typed fields — not a string you parsed. The prompt-parse-validate-handle dance is gone, replaced by “declare the model, get the model.” This is the core value: the most brittle part of LLM applications becomes a type declaration.

How it works: schema plus validation plus retry

Understanding the mechanism explains why it’s reliable, not magic. Pydantic AI does three things:

This schema → validate → retry pipeline is the machinery that turns “the model usually returns the right shape” into “I get a validated object or a clear error.” It’s Pydantic’s validation rigor applied at the LLM-output boundary, with automatic correction — exactly where LLM applications are most fragile.

Why this is more than convenience

Structured outputs aren’t just less code — they change the reliability profile of your application:

The shift is from “hope the string is right” to “the output is a validated object satisfying my model’s rules, or I get a clear error” — a fundamentally more robust foundation for anything that consumes LLM output.

The range of output types

Pydantic AI’s output typing is flexible, covering the real cases:

This range means structured outputs aren’t limited to “fill in this form” — you can model rich, branching, rule-constrained results, all type-safe. It’s the feature that most distinguishes Pydantic AI, and the one that makes it especially strong for the extraction, classification, and typed-workflow applications the first post highlighted. With reliable typed output established, the next post covers how the agent acts on the world to produce those outputs: tools.

Key takeaways

Further reading

Sources & References

The validation library underneath