pydantic-ai-dependency-injection
Implement dependency injection in PydanticAI agents using RunContext and deps_type. Use when agents need database connections, API clients, user context, or any external resources.
npx skills add majiayu000/claude-skill-registry --skill pydantic-ai-dependency-injection --agent claude-code
Same command for any agent — swap --agent for codex, cursor, copilot.
Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.
# PydanticAI Dependency Injection ## Core Pattern Dependencies flow through `RunContext`: ```python from dataclasses import dataclass from pydantic_ai import Agent, RunContext @dataclass class Deps: db: DatabaseConn api_client: HttpClient user_id: int agent = Agent( 'openai:gpt-4o', deps_type=Deps, # Type for static analysis ) @agent.tool async def get_user_balance(ctx: RunContext[Deps]) -> float: """Get the current user's account balance.""" return await ctx.deps.db.get_balance(ctx.deps.user_id) # At runtime, provide deps result = await agent.run( 'What is my balance?', deps=Deps(db=db_conn, api_client=client, user_id=123) ) ``` ## Defining Dependencies Use dataclasses or Pydantic models: ```python from dataclasses import dataclass from pydantic import BaseModel # Dataclass (recommended for simplicity) @dataclass class Deps: db: DatabaseConnection cache: CacheClient user_context: UserContext # Pydantic model (if you need validation) class Deps(BaseModel): api_key: str endpoint: str timeout: int = 30 ``` ## Accessing Dependencies In tools and instructions: ```python @agent.tool async def query_database(ctx: RunContext[Deps], query: str) -> list[dict]: """Run a database query.""" re
- Core Pattern
- Defining Dependencies
- Accessing Dependencies
- Type Safety
- No Dependencies Pattern
- Complete Example
- Override for Testing
- Gates
- Best Practices
What does the pydantic-ai-dependency-injection skill do?
Implement dependency injection in PydanticAI agents using RunContext and deps_type. Use when agents need database connections, API clients, user context, or any external resources.
How do I install it?
Run `npx skills add majiayu000/claude-skill-registry --skill pydantic-ai-dependency-injection --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.
Where does this skill come from?
From majiayu000/claude-skill-registry, a repository with 534 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.
Is a popular skill a good skill?
Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.
