pydantic-ai-tool-system
Standardizes tool creation for PydanticAI agents.
Install
mkdir -p .claude/skills/pydantic-ai-tool-system && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/14664" && unzip -o skill.zip -d .claude/skills/pydantic-ai-tool-system && rm skill.zipInstalls to .claude/skills/pydantic-ai-tool-system
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Register and implement PydanticAI tools with proper context handling, type annotations, and docstrings. Use when adding tool capabilities to agents, implementing function calling, or creating agent actions.Key capabilities
- →Register PydanticAI tools with `agent.tool`
- →Register PydanticAI tools with `agent.tool_plain`
- →Ensure correct `RunContext` parameter usage
- →Enforce docstring requirements for tools
- →Specify Google-style docstrings for parameters
- →Define tool return types including Pydantic models and `ToolReturn`
How it works
The skill provides guidelines and examples for registering PydanticAI tools, ensuring proper context handling, type annotations, and docstring formats for agent function calling.
Inputs & outputs
When to use pydantic-ai-tool-system
- →Adding tools to agents
- →Implementing function calls
- →Defining agent actions
About this skill
PydanticAI Tool System
Tool Registration
Two decorators based on whether you need context:
from pydantic_ai import Agent, RunContext
agent = Agent('openai:gpt-4o')
# @agent.tool - First param MUST be RunContext
@agent.tool
async def get_user_data(ctx: RunContext[MyDeps], user_id: int) -> str:
"""Get user data from database.
Args:
ctx: The run context with dependencies.
user_id: The user's ID.
"""
return await ctx.deps.db.get_user(user_id)
# @agent.tool_plain - NO context parameter allowed
@agent.tool_plain
def calculate_total(prices: list[float]) -> float:
"""Calculate total price.
Args:
prices: List of prices to sum.
"""
return sum(prices)
Critical Rules
- @agent.tool: First parameter MUST be
RunContext[DepsType] - @agent.tool_plain: MUST NOT have
RunContextparameter - Docstrings: Required for LLM to understand tool purpose
- Google-style docstrings: Used for parameter descriptions
Docstring Formats
Google style (default):
@agent.tool_plain
async def search(query: str, limit: int = 10) -> list[str]:
"""Search for items.
Args:
query: The search query.
limit: Maximum results to return.
"""
Sphinx style:
@agent.tool_plain(docstring_format='sphinx')
async def search(query: str) -> list[str]:
"""Search for items.
:param query: The search query.
"""
Tool Return Types
Tools can return various types:
# String (direct)
@agent.tool_plain
def get_info() -> str:
return "Some information"
# Pydantic model (serialized to JSON)
@agent.tool_plain
def get_user() -> User:
return User(name="John", age=30)
# Dict (serialized to JSON)
@agent.tool_plain
def get_data() -> dict[str, Any]:
return {"key": "value"}
# ToolReturn for custom content types
from pydantic_ai import ToolReturn, ImageUrl
@agent.tool_plain
def get_image() -> ToolReturn:
return ToolReturn(content=[ImageUrl(url="https://...")])
Accessing Context
RunContext provides:
@agent.tool
async def my_tool(ctx: RunContext[MyDeps]) -> str:
# Dependencies
db = ctx.deps.db
api = ctx.deps.api_client
# Model info
model_name = ctx.model.model_name
# Usage tracking
tokens_used = ctx.usage.total_tokens
# Retry info
attempt = ctx.retry # Current retry attempt (0-based)
max_retries = ctx.max_retries
# Message history
messages = ctx.messages
return "result"
Tool Prepare Functions
Dynamically modify tools per-request:
from pydantic_ai.tools import ToolDefinition
async def prepare_tools(
ctx: RunContext[MyDeps],
tool_defs: list[ToolDefinition]
) -> list[ToolDefinition]:
"""Filter or modify tools based on context."""
if ctx.deps.user_role != 'admin':
# Hide admin tools from non-admins
return [t for t in tool_defs if not t.name.startswith('admin_')]
return tool_defs
agent = Agent('openai:gpt-4o', prepare_tools=prepare_tools)
Toolsets
Group and compose tools:
from pydantic_ai import FunctionToolset, CombinedToolset
# Create a toolset
db_tools = FunctionToolset()
@db_tools.tool
def query_users(name: str) -> list[dict]:
"""Query users by name."""
...
@db_tools.tool
def update_user(id: int, data: dict) -> bool:
"""Update user data."""
...
# Use in agent
agent = Agent('openai:gpt-4o', toolsets=[db_tools])
# Combine toolsets
all_tools = CombinedToolset([db_tools, api_tools])
Common Mistakes
Wrong: Context in tool_plain
@agent.tool_plain
async def bad_tool(ctx: RunContext[MyDeps]) -> str: # ERROR!
...
Wrong: Missing context in tool
@agent.tool
def bad_tool(user_id: int) -> str: # ERROR!
...
Wrong: Context not first parameter
@agent.tool
def bad_tool(user_id: int, ctx: RunContext[MyDeps]) -> str: # ERROR!
...
Async vs Sync
Both work, but async is preferred for I/O:
# Async (preferred for I/O operations)
@agent.tool
async def fetch_data(ctx: RunContext[Deps]) -> str:
return await ctx.deps.client.get('/data')
# Sync (fine for CPU-bound operations)
@agent.tool_plain
def compute(x: int, y: int) -> int:
return x * y
When not to use it
- →When not using PydanticAI for agent tool development
- →When the task does not involve defining or implementing agent tools
- →When not working with Python
Prerequisites
Limitations
- →Requires `pydantic_ai` library.
- →Strict rules for `RunContext` parameter placement and presence.
- →Docstrings are required for LLM to understand tool purpose.
How it compares
This skill provides specific PydanticAI decorators and rules for tool definition, ensuring compatibility with the agent system, unlike general Python function definitions.
Compared to similar skills
pydantic-ai-tool-system side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| pydantic-ai-tool-system (this skill) | 0 | 6mo | No flags | Intermediate |
| copilot-sdk | 7 | 4mo | Review | Intermediate |
| guidance | 3 | 7mo | Review | Intermediate |
| adk-engineer | 3 | 1mo | Review | Advanced |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by ray-manaloto
View all by ray-manaloto →You might also like
copilot-sdk
github
Build agentic applications with GitHub Copilot SDK. Use when embedding AI agents in apps, creating custom tools, implementing streaming responses, managing sessions, connecting to MCP servers, or creating custom agents. Triggers on Copilot SDK, GitHub SDK, agentic app, embed Copilot, programmable agent, MCP server, custom agent.
guidance
davila7
Control LLM output with regex and grammars, guarantee valid JSON/XML/code generation, enforce structured formats, and build multi-step workflows with Guidance - Microsoft Research's constrained generation framework
adk-engineer
jeremylongshore
Execute software engineer specializing in creating production-ready ADK agents with best practices, code structure, testing, and deployment automation. Use when asked to "build ADK agent", "create agent code", or "engineer ADK application". Trigger with relevant phrases based on skill purpose.
llm-application-dev
skillcreatorai
Building applications with Large Language Models - prompt engineering, RAG patterns, and LLM integration. Use for AI-powered features, chatbots, or LLM-based automation.
agentica-spawn
parcadei
Spawn Agentica multi-agent patterns
fine-tuning-with-trl
davila7
Fine-tune LLMs using reinforcement learning with TRL - SFT for instruction tuning, DPO for preference alignment, PPO/GRPO for reward optimization, and reward model training. Use when need RLHF, align model with preferences, or train from human feedback. Works with HuggingFace Transformers.