Agent skill · Databases

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.

majiayu000github.com/majiayu000GitHub ↗
claude-codeMIT
Install
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.

Facts
Files in the skill folder: 2
SKILL.md size: 5 KB
Bundled scripts: none
Path: skills/agent/pydantic-ai-dependency-injection/SKILL.md
Open the folder on GitHub →
Where it comes from
Stars: 534
Language: HTML

Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.

From the SKILL.md

# 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

What's inside
Steps it walks through
  1. Core Pattern
  2. Defining Dependencies
  3. Accessing Dependencies
  4. Type Safety
  5. No Dependencies Pattern
  6. Complete Example
  7. Override for Testing
  8. Gates
  9. Best Practices
Ships with 1 file
  • metadata.json
More from claude-skill-registry
All skills →
About this skill
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.

Keep going