Session 8· 07· 15 min

Structured Agent Output

What you'll learn
  • Return a typed Pydantic object from an agent instead of prose
  • Use the response_format parameter on create_agent
  • Read the result from result["structured_response"]

Sometimes you want the agent to do its reasoning with tools AND return data in a fixed schema. response_format on create_agent gives you both: the agent loops with tools, then outputs a Pydantic object as its final answer.

07_structured_output.py
from pydantic import BaseModel, Field

class WeatherReport(BaseModel):
    city: str = Field(description="City name")
    temperature_c: float
    condition: str
    recommendation: str

agent = create_agent(
    model=f"openai:{model_name}",
    tools=[get_weather],
    response_format=WeatherReport,                               ①
)

result = agent.invoke({"messages": [...]})
report = result["structured_response"]                           ②
print(report.city, report.temperature_c)                         ③
response_format takes a Pydantic class. The agent will return this shape as its final output.
The typed result lives at result["structured_response"], not in messages.
Full attribute access with IDE autocomplete.
$ python 07_structured_output.py
Code Check
Where does the structured result live in the agent's output?
Recap — what you just learned
  • response_format=PydanticModel on create_agent
  • Result is at result["structured_response"]
  • Agent still reasons with tools — structured output is the FINAL shape, not the tool format
Next up: 08 — Human-in-the-Loop