How to beginner · 3 min read

Pydantic AI dependency injection

Quick answer
Use pydantic_ai by defining dependencies as callable functions or classes and injecting them into your Agent or Signature classes via constructor or method parameters. This enables clean, testable, and modular AI agent code with automatic dependency resolution.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install pydantic-ai openai>=1.0

Setup

Install pydantic-ai and openai packages and set your OpenAI API key as an environment variable.

  • Run pip install pydantic-ai openai
  • Set OPENAI_API_KEY in your environment
bash
pip install pydantic-ai openai
output
Collecting pydantic-ai
Collecting openai
Successfully installed openai-1.x.x pydantic-ai-x.x.x

Step by step

Define dependencies as functions or classes and inject them into your Agent. Use the @agent.tool decorator to register tools that depend on injected services.

python
import os
from pydantic_ai import Agent
from pydantic import BaseModel

# Dependency: a simple service class
class WeatherService:
    def get_weather(self, location: str) -> str:
        # Simulate fetching weather
        return f"Sunny weather in {location}"

# Define a Pydantic model for the tool output
class WeatherResult(BaseModel):
    forecast: str

# Create an agent with a system prompt
agent = Agent(
    "openai:gpt-4o-mini",
    system_prompt="You are a helpful assistant.",
    api_key=os.environ["OPENAI_API_KEY"]
)

# Inject dependency by passing it as a parameter to the tool
@agent.tool
def get_weather(ctx, location: str, weather_service: WeatherService) -> WeatherResult:
    forecast = weather_service.get_weather(location)
    return WeatherResult(forecast=forecast)

# Run the agent with dependency injection
result = agent.run_sync("What's the weather in New York?", dependencies={"weather_service": WeatherService()})
print(result.data.forecast)
output
Sunny weather in New York

Common variations

You can use async tools by defining async functions and calling agent.run() with await. You can also inject multiple dependencies or use classes with state. Different models can be used by changing the agent string, e.g., "openai:gpt-4o".

python
import asyncio

# Async dependency example
class AsyncService:
    async def fetch_data(self):
        return "Async data fetched"

@agent.tool
async def async_tool(ctx, service: AsyncService) -> str:
    data = await service.fetch_data()
    return data

async def main():
    result = await agent.run("Run async tool", dependencies={"service": AsyncService()})
    print(result.data)

asyncio.run(main())
output
Async data fetched

Troubleshooting

  • If you see Missing dependency errors, ensure you pass all required dependencies in the dependencies dict when calling agent.run_sync() or agent.run().
  • For type errors, verify your tool function signatures match the expected parameters and return types.
  • If the agent does not respond correctly, check your API key and model string.

Key Takeaways

  • Define dependencies as callable classes or functions and inject them via the agent's dependencies parameter.
  • Use the @agent.tool decorator to register functions that receive injected dependencies automatically.
  • Support async dependencies by defining async tools and using await with agent.run().
Verified 2026-04 · openai:gpt-4o-mini, openai:gpt-4o
Verify ↗