ABZ AgentABZ AgentDocs← Site
Docs/Core Concepts/Structured Output

Structured Output

Parse agent responses directly into a Pydantic model.

Structured Output returns a validated Python object instead of raw text. Define the shape you want as a Pydantic model, pass it as output_type, and get back real, typed data you can use directly in your code — no manual JSON parsing required.

Example#

meeting_assistant.py
from pydantic import BaseModel
from abzagent import Agent

class MeetingSummary(BaseModel):
    title: str
    summary: str
    action_items: list[str]

agent = Agent(
    name="Meeting Assistant",
    instructions="Summarize meeting notes into a structured summary.",
    model="gemini-2.5-flash",
    output_type=MeetingSummary,
)

notes = """
Team sync - Aug 7.
Discussed the Q3 roadmap. Sarah will finalize the pricing page by Friday.
Ali will follow up with the design team about the new logo.
Decided to delay the mobile launch to next quarter.
"""

result = agent.run(notes)

print(result.parsed.title)
print(result.parsed.summary)
print(result.parsed.action_items)

Example Output#

Output
Q3 Roadmap Sync
The team reviewed the Q3 roadmap and pricing page timeline, and decided to
delay the mobile launch to next quarter.
['Sarah finalizes the pricing page by Friday', 'Ali follows up with the design team about the new logo']

How It Works#

  • output_type builds a schema from your Pydantic model automatically — you never write the schema by hand.
  • That schema is sent to the model as part of the request, so it knows the exact shape of data to return.
  • result.parsed is a real instance of your model — result.parsed.title, autocomplete, and type checking all work as expected.
  • result.content still holds the raw text the model returned, in case you need it.

Automatic Validation#

Every response is validated against your Pydantic model before it's returned. If the model returns something that doesn't match the schema — a missing field or the wrong type — the SDK automatically retries the request once.

If the retry still fails validation, the SDK raises ModelBehaviorError instead of handing back malformed data.

Python
from abzagent import Agent, ModelBehaviorError

try:
    result = agent.run(notes)
    print(result.parsed)
except ModelBehaviorError:
    print("The model didn't return data matching MeetingSummary.")
Why this matters
You never have to guard against a half-formed response — by the time result.parsed is available, it's guaranteed to match your schema.

Next Step#

Continue to Guardrails.

← Previous
Built-in Tools
Next →
Guardrails