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.
Install via CLI (Recommended)
clawhub install openclaw/skills/skills/anderskev/pydantic-ai-dependency-injectionWhat This Skill Does
The pydantic-ai-dependency-injection skill provides a robust architectural framework for passing shared state, external services, and context-dependent data into PydanticAI agents. By utilizing the RunContext object and deps_type parameter, developers can move away from global variables and hard-coded service instances, resulting in cleaner, more modular, and testable agent code. This skill enables seamless integration with database connections, authenticated API clients, and user-specific session data, ensuring that your agents can interact securely and predictably with external systems during their execution lifecycle.
Installation
To integrate this skill into your project, use the following OpenClaw command:
clawhub install openclaw/skills/skills/anderskev/pydantic-ai-dependency-injection
Ensure your development environment is configured with pydantic-ai installed and your Python environment supports type hinting as per the agent definition requirements.
Use Cases
Dependency injection is essential for building production-grade AI agents. Common use cases include:
- Database Access: Injecting a validated database session or connection pool to allow tools to perform CRUD operations on user data.
- External API Clients: Passing pre-configured HTTP clients (like
httpx) with existing headers, authentication tokens, or base URLs to tools that need to fetch real-time data. - User Contextualization: Providing a current user ID, permission set, or account status to influence the agent's system prompt or tool behavior.
- Caching: Sharing a cache client instance across multiple tools to ensure efficient retrieval of previously processed information.
Example Prompts
- "Configure the agent dependency injection to allow access to my MongoDB instance and fetch user purchase history."
- "How do I pass an authenticated API client to my PydanticAI agent so that it can query the weather service using the user's saved location?"
- "Create a structure for my agent dependencies that includes both a cache client and a database connection for optimized lookups."
Tips & Limitations
- Strict Typing: Always use dataclasses for your dependency objects to take advantage of IDE autocompletion and static analysis. Explicitly define your agent type as
Agent[Deps, Output]for maximum type safety. - Security: Avoid passing sensitive raw credentials like master database passwords directly; prefer passing client objects that are already initialized with secure credentials.
- Testing: Dependency injection makes unit testing easier. You can inject mock objects into
agent.run()during tests instead of real services, allowing for deterministic testing of agent logic without external side effects. - Memory Management: Ensure the objects passed in the
depsargument have a lifecycle that matches the agent execution; do not pass heavy, non-serializable objects that require specific teardown if they are meant to be reused globally.
Metadata
Not sure this is the right skill?
Describe what you want to build — we'll match you to the best skill from 16,000+ options.
Find the right skillPaste this into your clawhub.json to enable this plugin.
{
"plugins": {
"official-anderskev-pydantic-ai-dependency-injection": {
"enabled": true,
"auto_update": true
}
}
}Tags(AI)
Flags: external-api
Related Skills
tutorial-docs
Tutorial patterns for documentation - learning-oriented guides that teach through guided doing
fetch-pr-feedback
Fetch review comments from a PR and evaluate with receive-feedback skill
swift-testing-code-review
Reviews Swift Testing code for proper use of
rust-testing-code-review
Reviews Rust test code for unit test patterns, integration test structure, async testing, mocking approaches, and property-based testing. Covers Rust 2024 edition changes including async fn in traits for mocks,
explanation-docs
Explanation documentation patterns for understanding-oriented content - conceptual guides that explain why things work the way they do