Building Pydantic AI Agents
pydantic/skills
Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), structured output, streaming, testing, and multi-agent patterns.
Patterns and tested examples for building agents with Pydantic AI: tools, capabilities, structured output, dependency injection, hooks, YAML specs, streaming and testing.
$ npx skills add docling-project/docling --skill building-pydantic-ai-agents -a claude-codeProject install by default; add -g for ~/.claude/skills/.
$ gh skill install docling-project/docling building-pydantic-ai-agents --agent claude-codeProject scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).
$ git clone --depth 1 https://github.com/docling-project/docling.git skills-src && mkdir -p .claude/skills && cp -r skills-src/.agents/skills/building-pydantic-ai-agents .claude/skills/building-pydantic-ai-agents && rm -rf skills-srcUse ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.
Claude Code skills documentation · loads skills from .claude/skills/
Install the "building-pydantic-ai-agents" agent skill from https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agents into .claude/skills/building-pydantic-ai-agents/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "building-pydantic-ai-agents", then confirm the skill loads.Claude Code copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$skill-installer install https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agentsType this inside Codex. $skill-installer <name> installs a curated skill from openai/skills. The installer writes to $CODEX_HOME/skills (default ~/.codex/skills). Restart Codex if the skill does not show up.
$ npx skills add docling-project/docling --skill building-pydantic-ai-agents -a codexProject install goes to .agents/skills/; add -g for ~/.codex/skills/.
$ gh skill install docling-project/docling building-pydantic-ai-agents --agent codexProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/docling-project/docling.git skills-src && mkdir -p .agents/skills && cp -r skills-src/.agents/skills/building-pydantic-ai-agents .agents/skills/building-pydantic-ai-agents && rm -rf skills-srcUse ~/.agents/skills/ instead of .agents/skills for a personal install.
Codex skills documentation · loads skills from .agents/skills/
Install the "building-pydantic-ai-agents" agent skill from https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agents into .agents/skills/building-pydantic-ai-agents/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "building-pydantic-ai-agents", then confirm the skill loads.Codex copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add docling-project/docling --skill building-pydantic-ai-agents -a cursorProject install goes to .agents/skills/; add -g for ~/.cursor/skills/.
$ gh skill install docling-project/docling building-pydantic-ai-agents --agent cursorProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/docling-project/docling.git skills-src && mkdir -p .cursor/skills && cp -r skills-src/.agents/skills/building-pydantic-ai-agents .cursor/skills/building-pydantic-ai-agents && rm -rf skills-srcUse ~/.cursor/skills/ instead of .cursor/skills for a personal install.
Cursor skills documentation · loads skills from .cursor/skills/, .agents/skills/, .claude/skills/, .codex/skills/
Install the "building-pydantic-ai-agents" agent skill from https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agents into .cursor/skills/building-pydantic-ai-agents/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "building-pydantic-ai-agents", then confirm the skill loads.Cursor copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gemini skills install https://github.com/docling-project/docling.git --path .agents/skills/building-pydantic-ai-agents--scope user (default) or --scope workspace; --path is the subfolder of the repo that holds the skill; --consent skips the security confirmation prompt.
$ npx skills add docling-project/docling --skill building-pydantic-ai-agents -a gemini-cliProject install goes to .agents/skills/; add -g for ~/.gemini/skills/.
$ gh skill install docling-project/docling building-pydantic-ai-agents --agent gemini-cliProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/docling-project/docling.git skills-src && mkdir -p .gemini/skills && cp -r skills-src/.agents/skills/building-pydantic-ai-agents .gemini/skills/building-pydantic-ai-agents && rm -rf skills-srcUse ~/.gemini/skills/ instead of .gemini/skills for a personal install, then run /skills reload.
Gemini CLI skills documentation · loads skills from .gemini/skills/, .agents/skills/
Install the "building-pydantic-ai-agents" agent skill from https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agents into .gemini/skills/building-pydantic-ai-agents/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "building-pydantic-ai-agents", then confirm the skill loads.Gemini CLI copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gh skill install docling-project/docling building-pydantic-ai-agentsInstalls for Copilot at project scope by default; add --scope user for a personal install. Preview a skill first with gh skill preview. Needs GitHub CLI 2.90.0 or later (public preview).
$ npx skills add docling-project/docling --skill building-pydantic-ai-agents -a github-copilotProject install goes to .agents/skills/; add -g for ~/.copilot/skills/.
$ git clone --depth 1 https://github.com/docling-project/docling.git skills-src && mkdir -p .github/skills && cp -r skills-src/.agents/skills/building-pydantic-ai-agents .github/skills/building-pydantic-ai-agents && rm -rf skills-srcUse ~/.copilot/skills/ instead of .github/skills for a personal install. Commit .github/skills so cloud agent and code review can use it.
GitHub Copilot skills documentation · loads skills from .github/skills/, .claude/skills/, .agents/skills/
Install the "building-pydantic-ai-agents" agent skill from https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agents into .github/skills/building-pydantic-ai-agents/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "building-pydantic-ai-agents", then confirm the skill loads.GitHub Copilot copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add docling-project/docling --skill building-pydantic-ai-agents -a opencodeOpenCode documents no install command of its own. Project install goes to .agents/skills/; add -g for ~/.config/opencode/skills/.
$ gh skill install docling-project/docling building-pydantic-ai-agents --agent opencodeProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/docling-project/docling.git skills-src && mkdir -p .opencode/skills && cp -r skills-src/.agents/skills/building-pydantic-ai-agents .opencode/skills/building-pydantic-ai-agents && rm -rf skills-srcUse ~/.config/opencode/skills/ instead of .opencode/skills for a personal install.
OpenCode skills documentation · loads skills from .opencode/skills/, .claude/skills/, .agents/skills/
Install the "building-pydantic-ai-agents" agent skill from https://github.com/docling-project/docling/tree/main/.agents/skills/building-pydantic-ai-agents into .opencode/skills/building-pydantic-ai-agents/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "building-pydantic-ai-agents", then confirm the skill loads.OpenCode copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
building-pydantic-ai-agentsPatterns and tested examples for building agents with Pydantic AI: tools, capabilities, structured output, dependency injection, hooks, YAML specs, streaming and testing.
The skill covers Pydantic AI, a Python agent framework for production generative AI applications. It applies when you mention the framework, when code imports pydantic_ai, or when you ask for an agent with tools, streaming, delegation or tests. Quick-start patterns show a basic agent, tools that use RunContext, structured output through Pydantic models, dependency injection, testing with TestModel, capabilities such as Thinking and WebSearch, lifecycle hooks, and agents defined from a YAML or JSON spec with Agent.from_file.
Capabilities are described as reusable units that bundle tools, hooks, instructions and model settings, while Hooks intercept model requests, tool calls and runs through decorators with no subclassing. Eleven reference files go deeper on core agents, architecture, built-in tools, capabilities and hooks, common tasks, input and history, orchestration and integrations, testing and debugging, and core and advanced tools. Observability with Logfire is also mentioned.
It is not meant for the plain Pydantic validation library, for other agent frameworks such as LangChain, LlamaIndex or CrewAI, or for general Python work. Python 3.10 or newer is required.
Read from SKILL.md and the folder at commit 21588f5. It shows what the files ask for, not the result of running them.
Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.
From allowed-tools in the SKILL.md frontmatter.
No scripts in the folder and no shell commands in SKILL.md (its code samples are python).
From the folder's file list and the shell code blocks in SKILL.md.
No URLs in SKILL.md.
From URLs in SKILL.md, links to its own repository left out.
Names no API keys, tokens, secrets or passwords.
From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.
Requires Python 3.10+
From compatibility in the SKILL.md frontmatter.
Building Pydantic AI Agents loads about 2.8k tokens when it runs, and up to ~12k if it reads all its reference files. Until then it costs about 82 tokens; SKILL.md has 778 words of instructions outside code blocks.
Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.
The automated check found no risky patterns in SKILL.md.
Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.
The full file from docling-project/docling at commit 21588f5, republished under its MIT licence (© docling-project). 778 words, ~2,786 tokens.
.claude/skills/building-pydantic-ai-agents/SKILL.md (or your agent's skills folder). This skill also uses 10 other files; get the full folder from GitHub.Pydantic AI is a Python agent framework for building production-grade Generative AI applications. This skill provides patterns, architecture guidance, and tested code examples for building applications with Pydantic AI.
Invoke this skill when:
pydantic_ai or references Pydantic AI classes (Agent, RunContext, Tool)Do not use this skill for:
pydantic/BaseModel without agents)from pydantic_ai import Agent
agent = Agent(
'anthropic:claude-sonnet-4-6',
instructions='Be concise, reply with one sentence.',
)
result = agent.run_sync('Where does "hello world" come from?')
print(result.output)
"""
The first known use of "hello, world" was in a 1974 textbook about the C programming language.
"""import random
from pydantic_ai import Agent, RunContext
agent = Agent(
'google-gla:gemini-3-flash-preview',
deps_type=str,
instructions=(
"You're a dice game, you should roll the die and see if the number "
"you get back matches the user's guess. If so, tell them they're a winner. "
"Use the player's name in the response."
),
)
@agent.tool_plain
def roll_dice() -> str:
"""Roll a six-sided die and return the result."""
return str(random.randint(1, 6))
@agent.tool
def get_player_name(ctx: RunContext[str]) -> str:
"""Get the player's name."""
return ctx.deps
dice_result = agent.run_sync('My guess is 4', deps='Anne')
print(dice_result.output)
#> Congratulations Anne, you guessed correctly! You're a winner!from pydantic import BaseModel
from pydantic_ai import Agent
class CityLocation(BaseModel):
city: str
country: str
agent = Agent('google-gla:gemini-3-flash-preview', output_type=CityLocation)
result = agent.run_sync('Where were the olympics held in 2012?')
print(result.output)
#> city='London' country='United Kingdom'
print(result.usage())
#> RunUsage(input_tokens=57, output_tokens=8, requests=1)from datetime import date
from pydantic_ai import Agent, RunContext
agent = Agent(
'openai:gpt-5.2',
deps_type=str,
instructions="Use the customer's name while replying to them.",
)
@agent.instructions
def add_the_users_name(ctx: RunContext[str]) -> str:
return f"The user's name is {ctx.deps}."
@agent.instructions
def add_the_date() -> str:
return f'The date is {date.today()}.'
result = agent.run_sync('What is the date?', deps='Frank')
print(result.output)
#> Hello Frank, the date today is 2032-01-02.from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
my_agent = Agent('openai:gpt-5.2', instructions='...')
async def test_my_agent():
"""Unit test for my_agent, to be run by pytest."""
m = TestModel()
with my_agent.override(model=m):
result = await my_agent.run('Testing my agent...')
assert result.output == 'success (no tool calls)'
assert m.last_model_request_parameters.function_tools == []Capabilities are reusable, composable units of agent behavior — bundling tools, hooks, instructions, and model settings.
from pydantic_ai import Agent
from pydantic_ai.capabilities import Thinking, WebSearch
agent = Agent(
'anthropic:claude-opus-4-6',
instructions='You are a research assistant. Be thorough and cite sources.',
capabilities=[
Thinking(effort='high'),
WebSearch(),
],
)Use Hooks to intercept model requests, tool calls, and runs with decorators — no subclassing needed.
from pydantic_ai import Agent, RunContext
from pydantic_ai.capabilities.hooks import Hooks
from pydantic_ai.models import ModelRequestContext
hooks = Hooks()
@hooks.on.before_model_request
async def log_request(ctx: RunContext[None], request_context: ModelRequestContext) -> ModelRequestContext:
print(f'Sending {len(request_context.messages)} messages')
return request_context
agent = Agent('openai:gpt-5.2', capabilities=[hooks])Use Agent.from_file to load agents from YAML or JSON — no Python agent construction code needed.
from pydantic_ai import Agent
# agent.yaml:
# model: anthropic:claude-opus-4-6
# instructions: You are a helpful research assistant.
# capabilities:
# - WebSearch
# - Thinking:
# effort: high
agent = Agent.from_file('agent.yaml')Load only the most relevant reference first. Read additional references only if the task spans multiple areas.
| I want to... | Reference |
|---|---|
| Create/configure agents, choose output types, use deps, define specs, or pick run methods | Agents Core |
| Bundle reusable behavior or intercept lifecycle events | Capabilities and Hooks |
| Add function tools, toolsets, MCP servers, or explicit search tools | Tools Core |
| Use provider-native web search, web fetch, or code execution | Built-in Tools |
Use advanced tool features such as approval, retries, ToolReturn, validators, timeouts, or tool search | Tools Advanced |
| Work with multimodal input, message history, or context trimming | Input and History |
| Test or debug agent behavior | Testing and Debugging |
| Coordinate multiple agents or build graph workflows | Orchestration and Integrations |
| Call the model directly, expose A2A, use durable execution, embeddings, evals, or third-party integrations | Orchestration and Integrations |
| Compare abstractions, output modes, decorators, or model-string patterns | Architecture and Decision Guide |
Follow an older link into COMMON-TASKS.md | Task Reference Map |
Load Architecture and Decision Guide only when the user is choosing between abstractions or wants comparison tables and decision trees:
| Topic | What it covers |
|---|---|
| Decision Trees | Tool registration, output modes, multi-agent patterns, capabilities, testing approaches, extensibility |
| Comparison Tables | Output modes, model provider prefixes, tool decorators, built-in capabilities, agent methods |
| Architecture Overview | Execution flow, generic types, construction patterns, lifecycle hooks, model string format |
Quick reference — model string format: "provider:model-name" (e.g., "openai:gpt-5.2", "anthropic:claude-sonnet-4-6", "google-gla:gemini-3-pro-preview")
Quick reference — key agent methods: run(), run_sync(), run_stream(), run_stream_sync(), run_stream_events(), iter()
logfire.instrument_pydantic_ai(). For deeper HTTP-level visibility, logfire.instrument_httpx(capture_all=True) captures the exact payloads sent to model providers.TestModel for deterministic tests, FunctionModel for custom logicThese are mistakes agents commonly make with Pydantic AI. Getting these wrong produces silent failures or confusing errors.
@agent.tool requires RunContext as first param; @agent.tool_plain must not have it. Mixing these up causes runtime errors. Use tool_plain when you don't need deps, usage, or messages.'openai:gpt-5.2' not 'gpt-5.2'. Without the prefix, Pydantic AI can't resolve the provider.TestModel requires agent.override(): Don't set agent.model directly. Always use the context manager: with agent.override(model=TestModel()):.str in output_type allows plain text to end the run: If your union includes str (or no output_type is set), the model can return plain text instead of structured output. Omit str from the union to force tool-based output..on don't repeat on_: Use hooks.on.run_error and hooks.on.model_request_error — not hooks.on.on_run_error.history_processors is plural: The Agent parameter is history_processors=[...], not history_processor=.Load exactly one of these unless the task clearly spans multiple families:
| Task family | Reference |
|---|---|
| Core agent setup, output, deps, specs, models, run methods | Agents Core |
| Capabilities, hooks, and reusable behavior | Capabilities and Hooks |
| Function tools, toolsets, MCP, explicit search tools | Tools Core |
| Provider-native builtin tools | Built-in Tools |
| Approval, retries, validators, timeouts, rich tool returns, deferred loading | Tools Advanced |
| Multimodal input, message history, history processors | Input and History |
| Testing, request inspection, and Logfire debugging | Testing and Debugging |
| Multi-agent patterns, graphs, direct API, A2A, durable execution, embeddings, evals, third-party integrations | Orchestration and Integrations |
Use Task Reference Map only for compatibility with older links or when you need a pointer from an old section name to the new file.
© docling-project, MIT. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file
SKILL.md and 10 other files (references) in .agents/skills/building-pydantic-ai-agents of docling-project/docling.
Open the folder on GitHubat commit 21588f5
Building Pydantic AI Agents next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.
| Skill | Stars | Used in | Tokens | Auto-check | Licence | Repo updated |
|---|---|---|---|---|---|---|
| Building Pydantic AI Agents this skilldocling-project/docling | 69k | — | ~2.8k | Automated safety check: Pass | MIT | |
| Building Pydantic AI Agentspydantic/skills | 140 | — | ~5.4k | Automated safety check: Pass | MIT | |
| Building Pydantic AI Agentspydantic/pydantic-ai | 21k | — | ~8.2k | Automated safety check: Pass | MIT | |
| Migrating Langchain To Pydantic AIpydantic/pydantic-ai | 21k | — | ~2.5k | Automated safety check: Pass | MIT | |
| Logfire Instrumentationpydantic/skills | 140 | — | ~6.1k | Automated safety check: Pass | MIT | |
| Pydantic AIdavila7/claude-code-templates | 33k | 3 repos | ~2.9k | Automated safety check: Pass | MIT |
pydantic/skills
Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), structured output, streaming, testing, and multi-agent patterns.
pydantic/pydantic-ai
Build AI agents with Pydantic AI — tools, capabilities (including on-demand loading), workspaces, structured output, streaming, testing, and multi-agent patterns.
pydantic/pydantic-ai
Migrate Python LangChain, LangGraph, or Deep Agents applications to Pydantic AI and, when the source uses harness features, Pydantic AI Harness.
pydantic/skills
Add Pydantic Logfire observability to application code — traces, logs, metrics, and AI/agent spans.
davila7/claude-code-templates
Build production-ready AI agents with PydanticAI — type-safe tool use, structured outputs, dependency injection, and multi-model support.
langchain-ai/langchain-skills
INVOKE THIS SKILL when you need human-in-the-loop approval, custom middleware, or structured output.
docling-project/docling
Converts PDFs, Office files, HTML, images and other documents into a unified DoclingDocument with Markdown or JSON output, through the docling CLI, Python SDK or a remote service.
docling-project/docling
Applies opinionated production Python conventions chosen by the project's Python version: modern type syntax, pathlib, explicit checks and interface guidance.
docling-project/docling
Reviews or re-reviews a Docling pull request in fixed stages, with findings that can be reproduced and an explicit record of every check that was run.
Works with
Categories
Patterns and tested examples for building agents with Pydantic AI: tools, capabilities, structured output, dependency injection, hooks, YAML specs, streaming and testing. The skill covers Pydantic AI, a Python agent framework for production generative AI applications. It applies when you mention the framework, when code imports pydantic_ai, or when you ask for an agent with tools, streaming, delegation or tests.
Building Pydantic AI Agents fits situations like: building a new AI agent with Pydantic AI; adding tools, capabilities or structured output to an existing agent; streaming agent events or delegating work between agents; writing tests for agent behavior with TestModel.
Run `npx skills add docling-project/docling --skill building-pydantic-ai-agents -a claude-code`. Or copy the skill folder (.agents/skills/building-pydantic-ai-agents in docling-project/docling) into .claude/skills/building-pydantic-ai-agents in your project. Claude Code loads it when a task matches its description.
Run `npx skills add docling-project/docling --skill building-pydantic-ai-agents -a codex`. Or copy the skill folder (.agents/skills/building-pydantic-ai-agents in docling-project/docling) into .agents/skills/building-pydantic-ai-agents in your project. Codex loads it when a task matches its description.
Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add docling-project/docling --skill building-pydantic-ai-agents -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/building-pydantic-ai-agents, .gemini/skills/building-pydantic-ai-agents, .github/skills/building-pydantic-ai-agents and .opencode/skills/building-pydantic-ai-agents in your project.
SKILL.md names no scripts, command-line tools or credentials: Building Pydantic AI Agents is instructions for the agent only. Our summary lists: Python 3.10 or newer; The pydantic_ai package. Compatibility (from SKILL.md): Requires Python 3.10+.
SKILL.md contains no URLs. Any network use would come from the scripts or tools the agent runs. This is read from the text; nothing was executed.
Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.
Building Pydantic AI Agents is published under the MIT licence (declared in SKILL.md). It allows redistribution, so the full SKILL.md is shown on this page.
About 2.8k tokens (SKILL.md is roughly 11k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full. Its references folder adds about 9.5k tokens, read only when the agent opens those files.
Skills that share tags, products or a category with Building Pydantic AI Agents: Building Pydantic AI Agents (pydantic/skills, 140 stars), Building Pydantic AI Agents (pydantic/pydantic-ai, 21k stars), Migrating Langchain To Pydantic AI (pydantic/pydantic-ai, 21k stars) and Logfire Instrumentation (pydantic/skills, 140 stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.
docling-project (a GitHub organization) maintains it in docling-project/docling, which has 68,662 GitHub stars. The repository holds 4 skills in this directory. The repository was last updated on October 11, 2026.
Source: docling-project/docling on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.