DEDUPED Hot AI repos. Real use cases. What people build. Get the Monday 5

How to get validated, typed data out of an LLM with Pydantic AI

Give a Pydantic AI agent an output type and tools so every run returns a checked Pydantic model.

Short answer: Install with uv add pydantic-ai; then 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. All steps.

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

  1. Install with uv add pydantic-ai
  2. Define a Pydantic BaseModel for the output, for example a Sentiment with a label and a score between -1 and 1
  3. Create an Agent with your model string and output_type set to that model
  4. Add data sources with the @agent.tool decorator; the signature and docstring become the tool schema
  5. 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

Cursor, ChatGPT, others

Share: X LinkedIn Hacker News Email Copy link

Third-party repos we don't own. AI-written use cases can be wrong. Disclaimer