liveBook · Manning

Pydantic

As explained in AI Agents in Action, Second Edition

Pydantic is a Python library for defining typed data models and validating, parsing, and serializing data.

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

How AI Agents in Action, Second Edition covers it

How deep it goes
Demonstrates model definitions, nested task structures, strict JSON-schema failures, and a repair using explicit fields.
Examples
Uses research-plan models, list and dictionary task formats, TypedDict task records, and extra='forbid'.
What it assumes
Assumes basic Python classes, lists, dictionaries, and agent configuration while introducing typed output design through small examples.
What you can do after
Build structured agent outputs and diagnose strict-schema failures without relying on brittle string parsing.
How it connects
Connects typed schemas with JSON Schema, multi-agent workflows, prompt engineering, tracing, and agentic loops.

Also explained in

Compare how 4 books explain Pydantic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.