# AI use cases for Pydantic AI (pydantic/pydantic-ai)

Repo: [pydantic/pydantic-ai](https://github.com/pydantic/pydantic-ai) | Use cases: 3 | Page: https://dedupedai.com/use-cases/pydantic-pydantic-ai/

Pydantic AI is a typed Python AI SDK from the Pydantic team: an agent loop where any model is a string swap away, tools and outputs are validated, and the same agent can run as a coding agent, a data extractor, a durable Temporal workflow or a realtime voice session. Pydantic AI Harness adds capabilities such as file access, shell, repo context and sub-agents, Pydantic Evals grades outputs and tool trajectories, and Logfire instruments every run.

This file holds every use case of this repo. Each one starts with a "## " heading and ends with its URL.

- Get validated, typed data out of an LLM: https://dedupedai.com/use-cases/pydantic-pydantic-ai/extract-typed-data-with-an-llm/
- Build your own terminal coding agent in a few lines: https://dedupedai.com/use-cases/pydantic-pydantic-ai/build-your-own-terminal-coding-agent/
- Make a research agent survive crashes with Temporal: https://dedupedai.com/use-cases/pydantic-pydantic-ai/make-an-agent-survive-crashes-with-temporal/

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

Repo: [pydantic/pydantic-ai](https://github.com/pydantic/pydantic-ai) | Difficulty: Beginner | Time: 20 minutes | Basis: From the README

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

**Who it is for:** A developer parsing reviews, tickets or emails who is tired of fixing malformed JSON from a model.

**The problem:** Free-text model output breaks downstream code when fields are missing or out of range.

**The outcome:** An agent whose result.output is a validated Pydantic model, with tools whose arguments are validated too.

### 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 to paste into your agent

```text
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.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/pydantic-pydantic-ai/extract-typed-data-with-an-llm/

## How to build your own terminal coding agent in a few lines with Pydantic AI

Repo: [pydantic/pydantic-ai](https://github.com/pydantic/pydantic-ai) | Difficulty: Intermediate | Time: 30 minutes | Basis: From the README

Use Pydantic AI Harness's Coder capability to get file access, shell, repo context and sub-agents in one agent.

**Who it is for:** An engineer who wants a coding agent they fully control, with their choice of models.

**The problem:** Off-the-shelf coding agents are hard to customise and tie you to one vendor.

**The outcome:** A chat-in-the-terminal coding agent built from composable capabilities you can swap or extend.

### Steps

1. Install with `uv add pydantic-ai "pydantic-ai-harness[coder]"`
2. Create an Agent with capabilities Coder(), and optionally WebSearch() and an Advisor on a second model, as in the README
3. Call agent.to_cli_sync() and run the file to chat with it
4. Or try it without code using the README's uvx command that runs the exported coder_agent with clai
5. Replace Coder() with its building blocks such as FileSystem, Shell and RepoContext to customise it

### Prompt to paste into your agent

```text
Using pydantic/pydantic-ai and pydantic-ai-harness[coder], create a terminal coding agent on <model> with Coder() and WebSearch(), restrict the shell to an allowlist, and run it on this repo to <task>.
```

### Caveat

The README's example model names may not match what your provider offers; use a model string your account supports.

Risk, credentials: The agent needs provider API keys and, with Coder, can read files and run shell commands in its workspace; keep it in a project folder you can reset.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/pydantic-pydantic-ai/build-your-own-terminal-coding-agent/

## How to make a research agent survive crashes with Temporal with Pydantic AI

Repo: [pydantic/pydantic-ai](https://github.com/pydantic/pydantic-ai) | Difficulty: Advanced | Time: 1 hour | Basis: From the README

Attach TemporalDurability so the same Pydantic AI agent runs inside a durable Temporal workflow.

**Who it is for:** A team running long research or reporting agents that cannot afford to restart from scratch.

**The problem:** A long agent run that crashes halfway loses its progress and its token spend.

**The outcome:** An agent running under durable execution in Temporal, or saving and resuming steps with StepPersistence.

### Steps

1. Install with `uv add "pydantic-ai[temporal]"`
2. Create an Agent with instructions, capabilities such as WebSearch and WebFetch, and TemporalDurability()
3. Wrap it in a workflow class based on PydanticAIWorkflow with a run method, as in the README
4. DBOS and Prefect attach the same way if you use those instead of Temporal
5. Without an engine, use StepPersistence from Pydantic AI Harness to save, resume and fork runs

### Prompt to paste into your agent

```text
Using pydantic/pydantic-ai with TemporalDurability, turn my <agent> into a Temporal workflow that writes a brief on a topic. Show me that killing the worker mid-run and restarting it resumes the workflow.
```

### Caveat

You need a running Temporal setup, which the README does not cover; see the Durable Execution docs.

Disclaimer: Third-party repos we don't own or vouch for. AI-generated use cases can be wrong: review everything and run at your own risk. Full text: https://dedupedai.com/disclaimer/

URL: https://dedupedai.com/use-cases/pydantic-pydantic-ai/make-an-agent-survive-crashes-with-temporal/
