Who it is forA developer parsing reviews, tickets or emails who is tired of fixing malformed JSON from a model.
The problemFree-text model output breaks downstream code when fields are missing or out of range.
The outcomeAn agent whose result.output is a validated Pydantic model, with tools whose arguments are validated too.
What you need first
- Python and uv
- An API key for the model provider you choose
- Optional extras: pydantic-ai-harness[coder], pydantic-ai[temporal] and a Temporal setup, pydantic-ai[openai-realtime]
- Optional: a Logfire account for tracing
Steps
- Install with
uv add pydantic-ai - Define a Pydantic BaseModel for the output, for example a Sentiment with a label and a score between -1 and 1
- Create an Agent with your model string and output_type set to that model
- Add data sources with the @agent.tool decorator; the signature and docstring become the tool schema
- Call run_sync and use result.output as typed data
Prompt for your agent
Using pydantic/pydantic-ai, build an agent with output_type=<Pydantic model> that extracts <fields> from <input type>. Add a tool that fetches <data>, run it on three examples and show the validated outputs.
Caveat. Validation guarantees the shape of the answer, not that the model judged it correctly, so spot check results.
Use this in your AI agent
Ask your agent
Using the Deduped MCP, get the setup steps for pydantic/pydantic-ai and "Get validated, typed data out of an LLM"
Add Deduped to Claude Code once
claude mcp add --transport http deduped https://dedupedai.com/mcp
Every Monday: the 5 hottest repos and exactly how to use them. Free, one email a week, unsubscribe anytime.
Get them freeShare: X LinkedIn Hacker News Email Copy link
Third-party repos we don't own. AI-written use cases can be wrong. Disclaimer