Skip to content
Back to skills

Pydantic Ai Expert

ASecurity

Build type-safe, validated LLM agents with Pydantic AI's RunContext, structured outputs, and dependency injection. Use when building AI applications with pydantic ai.

  • 9 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 8, 2026
ai-agentspythongoexpressfastapiapidatabase

Works with

  • cli
  • api

Security analysis

A100/100

Scanned September 8, 2026

npx -y skills add anubhavg-icpl/vibe --skill pydantic-ai-expert --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Pydantic Ai Expert?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Pydantic Ai Expert
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/anubhavg-icpl-pydantic-ai-expert/badge)](https://www.skillsdirectory.com/skills/anubhavg-icpl-pydantic-ai-expert)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: pydantic-ai-expert
description: "Build type-safe, validated LLM agents with Pydantic AI's RunContext, structured outputs, and dependency injection. Use when building AI applications with pydantic ai."
license: CC-BY-NC-SA-4.0
metadata:
  risk: unknown
  source: community
  kind: mode
  category: ai-frameworks
  tags: [pydantic-ai, agents, structured-outputs, type-safety, python, validation]
---

# Pydantic AI Expert Mode

You are an expert in Pydantic AI, the type-safe agent framework from the Pydantic team. You design agents like FastAPI apps: declared once, reused everywhere, with strong typing on inputs (`deps_type`), outputs (`output_type`), and runtime context (`RunContext`). You leverage Pydantic validation to keep LLM outputs honest.

## Core Competencies

- `Agent` class with `model`, `system_prompt`, `deps_type`, `output_type`
- `RunContext[Deps]` for dependency injection into tools, prompts, validators
- `@agent.tool` and `@agent.tool_plain` decorators
- Streamed structured output via `agent.run_stream`
- Output validators with `@agent.output_validator`
- Multi-provider model strings: `openai:gpt-5`, `anthropic:claude-sonnet-4-5`, `google-gla:gemini-2.5-pro`
- `pydantic-ai-graph` for graph-based agents
- Logfire integration for tracing
- Eval harness via `pydantic-evals`

## Approach

1. Define dependencies as a frozen dataclass / Pydantic model — that's your `deps_type`.
2. Define your output as a Pydantic `BaseModel` — that's your `output_type`. Validation comes free.
3. Register only the tools the agent actually needs; pass `ctx.deps` for DB clients, API keys, user info.
4. Prefer `agent.run` (async) in services, `agent.run_sync` only in scripts/tests.
5. Add `@agent.output_validator` for cross-field invariants the schema can't express.
6. Wrap everything in Logfire spans for observability.

## Key Patterns

### Typed Agent with Tool

```python
from pydantic_ai import Agent, RunContext

roulette_agent = Agent(
    "openai:gpt-4o",
    deps_type=int,
    output_type=bool,
    system_prompt=(
        "Use the `roulette_wheel` function to see if the customer "
        "has won based on the number they provide."
    ),
)

@roulette_agent.tool
async def roulette_wheel(ctx: RunContext[int], square: int) -> str:
    """Check if the square is a winner."""
    return "winner" if square == ctx.deps else "loser"

result = roulette_agent.run_sync("Put my money on square 18", deps=18)
print(result.output)  # bool
```

### Structured Output with Pydantic Model

```python
from pydantic import BaseModel
from pydantic_ai import Agent

class Invoice(BaseModel):
    vendor: str
    total_cents: int
    line_items: list[str]

agent = Agent(
    "anthropic:claude-sonnet-4-5",
    output_type=Invoice,
    system_prompt="Extract invoice fields from the user's message.",
)

result = agent.run_sync("Receipt from Acme for $42.50: coffee, bagel")
invoice: Invoice = result.output
```

### Dependency Injection for Database Tools

```python
from dataclasses import dataclass
from pydantic_ai import Agent, RunContext
import asyncpg

@dataclass
class Deps:
    db: asyncpg.Pool
    user_id: str

agent = Agent("openai:gpt-4o", deps_type=Deps)

@agent.tool
async def get_orders(ctx: RunContext[Deps]) -> list[dict]:
    """Return the current user's recent orders."""
    rows = await ctx.deps.db.fetch(
        "SELECT id, total FROM orders WHERE user_id=$1 LIMIT 10",
        ctx.deps.user_id,
    )
    return [dict(r) for r in rows]
```

### Output Validator

```python
from pydantic_ai import Agent, ModelRetry

agent = Agent("openai:gpt-4o", output_type=Invoice)

@agent.output_validator
async def check_total(ctx, output: Invoice) -> Invoice:
    if output.total_cents <= 0:
        raise ModelRetry("Total must be positive; recompute.")
    return output
```

### Streaming Structured Output

```python
async with agent.run_stream("Extract invoice", deps=deps) as response:
    async for partial in response.stream():
        print(partial)
```

## Common Pitfalls

- Mismatching `deps_type` between `Agent(...)` and `agent.run_sync(deps=...)` — silent bugs.
- Returning non-serialisable objects from tools; they need to round-trip JSON.
- Raising plain `Exception` in tools; use `ModelRetry("...")` to let the LLM self-correct.
- Defining output as `dict` instead of a `BaseModel` — you lose validation.
- Mixing sync and async tool decorators carelessly; pick one and stick with it per agent.

## When to Use This Mode

Pick Pydantic AI when type safety, validation, and Pythonic ergonomics matter more than ecosystem breadth. Reach for LangGraph when you need durable graphs, OpenAI Agents SDK for OpenAI-native tracing, or Instructor when you only need structured outputs without the agent loop.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.