pydantic-ai
Python framework for building production-grade AI agents with LLMs. Use when creating agents that need structured outputs, tools, dependency injection, or type-safe interactions. Specifically use for: (1) Building AI agents with OpenAI, Anthropic, Google, or other LLM providers, (2) Creating agents that require structured output validation via Pydantic models, (3) Implementing tool-calling agents with function tools, (4) Building multi-agent applications or A2A (Agent2Agent) protocol servers, (5) Adding observability with Pydantic Logfire, (6) Streaming responses or events from agents
Best use case
pydantic-ai is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Python framework for building production-grade AI agents with LLMs. Use when creating agents that need structured outputs, tools, dependency injection, or type-safe interactions. Specifically use for: (1) Building AI agents with OpenAI, Anthropic, Google, or other LLM providers, (2) Creating agents that require structured output validation via Pydantic models, (3) Implementing tool-calling agents with function tools, (4) Building multi-agent applications or A2A (Agent2Agent) protocol servers, (5) Adding observability with Pydantic Logfire, (6) Streaming responses or events from agents
Teams using pydantic-ai should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/pydantic-ai/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How pydantic-ai Compares
| Feature / Agent | pydantic-ai | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Python framework for building production-grade AI agents with LLMs. Use when creating agents that need structured outputs, tools, dependency injection, or type-safe interactions. Specifically use for: (1) Building AI agents with OpenAI, Anthropic, Google, or other LLM providers, (2) Creating agents that require structured output validation via Pydantic models, (3) Implementing tool-calling agents with function tools, (4) Building multi-agent applications or A2A (Agent2Agent) protocol servers, (5) Adding observability with Pydantic Logfire, (6) Streaming responses or events from agents
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Pydantic AI
## Overview
Pydantic AI is a type-safe Python framework for building AI agents. It provides tools, structured outputs, dependency injection, and comprehensive model support for production-grade applications.
## When to Use Pydantic AI
Use this skill when you need to:
- Build AI agents with any LLM provider (OpenAI, Anthropic, Google, Groq, etc.)
- Ensure type-safe, validated structured outputs using Pydantic models
- Create agents that can call tools (functions) to gather information
- Implement dependency injection for testable, maintainable agents
- Stream agent responses or events in real-time
- Build multi-agent workflows or A2A servers
- Add observability with Pydantic Logfire
## Quick Start
### Installation
```bash
uv add pydantic-ai
```
Or for slim installs with only specific model dependencies:
```bash
uv add "pydantic-ai-slim[openai,anthropic]"
```
### Basic Agent
```python
from pydantic_ai import Agent
agent = Agent('openai:gpt-4o', instructions='Be helpful and concise.')
result = agent.run_sync('What is 2+2?')
print(result.output)
```
### Agent with Tools and Structured Output
```python
from dataclasses import dataclass
from pydantic import BaseModel, Field
from pydantic_ai import Agent, RunContext
@dataclass
class Dependencies:
api_key: str
class Output(BaseModel):
response: str
confidence: float
agent = Agent(
'openai:gpt-4o',
deps_type=Dependencies,
output_type=Output,
instructions='Help users with their queries.',
)
@agent.tool
async def get_info(ctx: RunContext[Dependencies], query: str) -> str:
"""Fetch information about a topic."""
return f"Information about {query}"
result = await agent.run('Tell me about Python', deps=Dependencies(api_key='key'))
print(result.output) # Output(response='...', confidence=0.95)
```
## Running Agents
- `agent.run()` - Async execution
- `agent.run_sync()` - Synchronous execution
- `agent.run_stream()` - Stream text/structured output
- `agent.run_stream_events()` - Stream all events (tool calls, text, etc.)
- `agent.iter()` - Iterate over graph nodes
## Agent Components
| Component | Description |
|-----------|-------------|
| **Instructions** | Static or dynamic instructions for the LLM |
| **Tools** | Functions the LLM can call (`@agent.tool`) |
| **Output Type** | Pydantic model for structured output validation |
| **Dependencies** | Type-safe dependency injection for tools/instructions |
| **Model** | LLM model (OpenAI, Anthropic, Google, etc.) |
## Model Selection
Specify models by provider: `openai:gpt-4o`, `anthropic:claude-3-5-sonnet`, `google:gemini-2.0-flash`, etc.
See `references/models.md` for all supported providers and models.
## Common Patterns
### Dynamic Instructions
```python
@agent.instructions
async def add_context(ctx: RunContext[Dependencies]) -> str:
return f"Current user ID: {ctx.deps.user_id}"
```
### Tool Parameters
```python
@agent.tool
async def search(
ctx: RunContext[Dependencies],
query: str,
max_results: int = 10,
) -> list[str]:
"""Search a database with the given query."""
# Implementation
pass
```
### Streaming Responses
```python
async with agent.run_stream('Tell me a story') as response:
async for chunk in response.stream_text():
print(chunk, end='')
```
## Advanced Features
- **Graphs**: Complex workflows using `pydantic_graph`
- **Multi-Agent**: Agent-to-agent communication with A2A protocol
- **Durable Execution**: DBOS, Prefect, or Temporal integration
- **MCP Integration**: Model Context Protocol support
- **UI Streams**: AG-UI or Vercel AI SDK integration
## Resources
### references/
- `models.md` - All supported LLM providers and models
- `api_reference.md` - API documentation for core classes
- `examples.md` - Detailed examples for common use cases
### scripts/
No executable scripts included. Pydantic AI is a framework, not a tool collection.
### assets/
No assets included. This is a pure Python framework.
## Development
- Test agents with `agent.run_sync()` for quick iteration
- Use `uv run pytest` for testing (project must have tests configured)
- Enable Logfire for observability: `logfire.instrument_pydantic_ai()`Related Skills
pydantic-models-py
Create Pydantic models following the multi-model pattern with Base, Create, Update, Response, and InDB variants. Use when defining API request/response schemas, database models, or data validation ...
bgo
Automates the complete Blender build-go workflow, from building and packaging your extension/add-on to removing old versions, installing, enabling, and launching Blender for quick testing and iteration.
moai-lang-r
R 4.4+ best practices with testthat 3.2, lintr 3.2, and data analysis patterns.
moai-lang-python
Python 3.13+ development specialist covering FastAPI, Django, async patterns, data science, testing with pytest, and modern Python features. Use when developing Python APIs, web applications, data pipelines, or writing tests.
moai-icons-vector
Vector icon libraries ecosystem guide covering 10+ major libraries with 200K+ icons, including React Icons (35K+), Lucide (1000+), Tabler Icons (5900+), Iconify (200K+), Heroicons, Phosphor, and Radix Icons with implementation patterns, decision trees, and best practices.
moai-foundation-trust
Complete TRUST 4 principles guide covering Test First, Readable, Unified, Secured. Validation methods, enterprise quality gates, metrics, and November 2025 standards. Enterprise v4.0 with 50+ software quality standards references.
moai-foundation-memory
Persistent memory across sessions using MCP Memory Server for user preferences, project context, and learned patterns
moai-foundation-core
MoAI-ADK's foundational principles - TRUST 5, SPEC-First TDD, delegation patterns, token optimization, progressive disclosure, modular architecture, agent catalog, command reference, and execution rules for building AI-powered development workflows
moai-cc-claude-md
Authoring CLAUDE.md Project Instructions. Design project-specific AI guidance, document workflows, define architecture patterns. Use when creating CLAUDE.md files for projects, documenting team standards, or establishing AI collaboration guidelines.
moai-alfred-language-detection
Auto-detects project language and framework from package.json, pyproject.toml, etc.
mnemonic
Unified memory system - aggregates communications and AI sessions across all channels into searchable, analyzable memory
mlops
MLflow, model versioning, experiment tracking, model registry, and production ML systems