PydanticAI in Production: Type-Driven Agent Design Patterns
Focused on structured outputs, tool calling, and error recovery, this article presents practical PydanticAI patterns for production systems.
PydanticAI helps teams move from prompt-centric experiments to reliable, schema-driven agent systems.
Why Type-Driven Design Matters
In production, most failures come from malformed outputs and ambiguous tool inputs. Strong typing reduces these failure modes before runtime.
Pattern 1: Strict Structured Outputs
Define explicit response models and reject invalid outputs early.
Benefits:
- Fewer downstream parsing errors
- Better contract stability across services
- Easier regression testing
Pattern 2: Typed Tool Interfaces
Represent tool inputs and outputs as validated models instead of loose dictionaries.
Benefits:
- Clear invocation contracts
- Safer parameter evolution
- Better traceability in logs
Pattern 3: Recoverable Error Flows
Classify errors into validation, retriable execution, and hard business failures.
For each class, define deterministic handling strategies so retries do not become uncontrolled loops.
Pattern 4: Guarded Multi-Step Tasks
For long workflows, validate at every step and persist intermediate state. This improves resilience when model outputs vary.
Operational Checklist
Before going live:
- Version all core schemas
- Log validation failures with context
- Add unit tests for critical tool contracts
- Monitor error-category trends over time
This approach keeps agent behavior predictable as complexity grows.
Type safety is not overhead in agent systems; it is reliability infrastructure.
Tool Calls: From "Looks Right" to "Validates Right"
PydanticAI's real strength is not model wrapping — it makes tool calls part of the same schema validation pipeline. A common failure mode: the model returns "call tool X with args Y", but the args are free-form text. Enforce:
- Declare input schema (Pydantic models) when registering tools
- Return structured
ErrorResulton validation failure, not raw exceptions - Wrap tool outputs in Output models so downstream does not need try/except on unknown dicts
This "validate both ends" pattern minimizes the blast radius of model hallucinations. After adopting it, one fintech team moved from 3 data-dirty tickets per week to roughly 1 per quarter.
Multi-Agent Systems: Roles as Typed Protocols
In multi-agent systems, the biggest maintainability issue is unclear role boundaries — agent A and agent B both claim to have queried the order; whose result wins? PydanticAI supports dependency injection plus Result models that enforce:
- Each role sees only its allowed context slice
- Each role's output must match its role-specific schema
- Cross-role communication goes through a typed message bus, not free strings
In practice this typed protocol turns a 6-role customer-service agent system from "debug by reading logs" to "read the type signature and locate the problem".
Three Things to Do Before Going Live
- Version your schemas: All Output models get semver. Agent upgrades and consumer upgrades decouple.
- Make validation failures observable: Each failure logs a span plus context — don't just
print. Platforms like Langfuse let you see schema-drift trends directly. - Keep the previous schema as fallback: When upgrading the model or changing prompts, retain a fallback path to the prior schema for at least two weeks.
Common Anti-Patterns
- "The model is a black box; the framework handles it" — actually PydanticAI's fallback is explicit; the framework cannot make schema decisions for you.
- "Use
Anyeverywhere for flexibility" — fast short term; changing one field three months later breaks everything. - "Eval once is enough" — schema evolution inevitably causes behavioral drift; you must rerun regression samples after every change.
Projects in this article
PydanticAI
18.1k ⭐PydanticAI builds agents on top of type systems, emphasizing verifiable data structures, tool calling, and production-grade reliability.
DSPy
35.7k ⭐DSPy is a declarative LLM programming framework focused on optimizable prompts and program structure, suitable for complex agent workflows.
LangGraph
36.2k ⭐LangGraph is an agent workflow orchestration framework from the LangChain team, using graph structures to model agent state and transitions.
TaskWeaver
6.2k ⭐TaskWeaver is Microsoft's open-source code-interpreter-style agent framework, suitable for data analysis and complex task automation.