agent_with_tools_and_depsTier 1 · 70% confidence
ai-agents-agent-with-tools-and-you-need-to-build-an-agent-that-can-call-external--fc9fc1fc
agent: ai_agents
When does this happen?
IF You need to build an agent that can call external tools (e.g., database queries) and return structured, validated output.
How others solved it
THEN Define a dataclass for dependencies, a Pydantic model for output, then create an Agent with deps_type and output_type. Use @agent.tool to register tool functions and RunContext to access dependencies. Optionally use @agent.instructions for dynamic instructions.
from dataclasses import dataclass
from pydantic import BaseModel, Field
from pydantic_ai import Agent, RunContext
@dataclass
class SupportDependencies:
customer_id: int
db: DatabaseConn
class SupportOutput(BaseModel):
support_advice: str = Field(description='Advice')
block_card: bool = Field(description='Block card?')
risk: int = Field(ge=0, le=10)
support_agent = Agent('openai:gpt-5.2', deps_type=SupportDependencies, output_type=SupportOutput, instructions='You are a support agent.')
@support_agent.instructions
async def add_customer_name(ctx: RunContext[SupportDependencies]) -> str:
return f"Customer name is {await ctx.deps.db.customer_name(id=ctx.deps.customer_id)}"
@support_agent.tool
async def customer_balance(ctx: RunContext[SupportDependencies], include_pending: bool) -> float:
return await ctx.deps.db.customer_balance(id=ctx.deps.customer_id, include_pending=include_pending)Related patterns
github
ai-agents-github-support-for-reasoning-in-openrouter-and-deepseek-p-48add6f0
Tier 1 · 40%
githubai-agents-github-server-capabilities-not-affecting-the-stream-of-ca-ca806d9e
Tier 1 · 40%
githubai-agents-github-patrick-von-platen-cd4d7ceb
Tier 1 · 40%
model_loadingai-agents-model-loading-loading-a-gemma-3-checkpoint-with-automodelforcaus-cc5b7a71
Tier 1 · 70%
githubai-agents-github-runtimeerror-cuda-error-cublas-status-not-initiali-9b601119
Tier 1 · 40%
githubai-agents-github-bug-frequent-ide-disconnections-disrupting-workflo-e9f35aca
Tier 1 · 40%
Have you seen this in your site?
Connect AgentMinds to match against your tech stack automatically.