Pydantic is a Python library for defining typed data models and validating, parsing, and serializing data. In agent workflows, it turns unpredictable language-model responses into structured inputs and outputs that downstream agents can use reliably. section 2.3.3 section 2.6
Without types, an agent may return varying text that breaks the next step. With a Pydantic model, the workflow declares the expected shape before the response moves onward, reducing brittle string parsing and ambiguity. section 2.3.3 section 2.6
Build a typed output progressively:
- Import BaseModel and define a model whose fields describe the required response. section 2.3.3
- Assign the model as the agent’s output type so the agent returns structured data rather than an unconstrained response. section 2.3.3
- Prefer strict, closed structures when JSON schema is enabled; use explicit fields instead of open-ended mappings. section 2.3.3
A typed research plan
from pydantic import BaseModel class ResearchPlanModel(BaseModel): tasks: list[str] agent = Agent( name="Research Planner", instructions=instructions, output_type=ResearchPlanModel, ) The model can later become more explicit: define a Task with id and description, use tasks: list[Task], and set model_config = ConfigDict(extra='forbid'). This avoids the strict-schema failure caused by an open dict[int, str] field. section 2.3.3
Figure 2.5 Comparing how an agent workflow may run with and without typed outputs/inputs. At the top, the agent does not use strict outputs and could respond in any fashion, increasing variability when passing output to the next agent. Conversely, at the bottom, the agent provides strict outputs, which reduces variability and improves clarity for agents or processes receiving the output.
Compare an untyped agent workflow with one using strict typed inputs and outputs; the contrast makes variability and handoff risks visible. section 2.3.3
Treat strict-schema errors as design feedback rather than disabling validation by default. In iterative agents, typed output should include control data such as follow-up questions, goal-satisfaction flags, and confidence scores so the loop controller can manage its next step. section 2.3.3 section 9.6



