published fresh until

PydanticAI: give function tools run dependencies

Summary As of November 2026, set deps_type, accept RunContext[YourDeps] first, and read ctx.deps; supply an instance through deps on each run. Context is excluded from the model-facing tool schema. Register dependency-aware functions with @agent.tool.

Not for LangChain tool decorators or PydanticAI output functions that end a run.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Register a context-aware function with @agent.tool, annotate its first argument as RunContext[YourDeps], and supply deps when running the agent.

Short answer

Pass a dependency type to Agent(deps_type=...) and a matching instance to the run's deps parameter. Read it from ctx.deps. The type declaration supports static checking; it does not perform runtime dependency validation.

What to do

The documentation's string-dependency pattern, shortened to registration:

from pydantic_ai import Agent, RunContext
agent = Agent('google:gemini-3.8-flash', deps_type=str)
@agent.tool
def get_player_name(ctx: RunContext[str]) -> str:
    return ctx.deps

This uses the documentation's google:gemini-3.8-flash example model. Pass deps='Anne' to agent.run_sync(...); asynchronous application code can use await agent.run(..., deps=deps).

NeedAPI / rule
Tool with run context@agent.tool; first parameter RunContext[YourDeps]
Tool without run context@agent.tool_plain
Existing reusable functionsAgent(..., tools=[...]); signatures determine whether RunContext is needed
Explicit context selectionTool(get_player_name, takes_ctx=True); context-free example Tool(roll_dice, takes_ctx=False)
Model-facing schemaFunction parameters except RunContext; dependency context is supplied by the agent
Tool resultAnything Pydantic can serialize to JSON
I/O dependenciesPrefer async def; synchronous def functions run via run_in_executor in a thread pool
Docstring parsingInferred by default; docstring_format can specify google, numpy, or sphinx
Require parameter documentationrequire_parameter_descriptions=True; a missing description raises UserError

Put model-relevant guidance in the leading docstring, parameter descriptions (including Keyword Args / Other Parameters), or the first returns entry. Sections such as Raises, Examples, Notes, Warnings, and Yields are dropped from the model-facing description.

Limits and open questions

run_sync wraps run; that choice is independent of whether dependencies use synchronous or asynchronous functions. Registration makes a tool available; it does not require the model to call it. The pages give no fixed tool-count limit.

Not for: LangChain tool decorators or PydanticAI output functions that end a run.

Sources

Checked

2026-11-17; read Function Tools (input URL); Function Tools (redirect destination); Dependencies; no command was run.

Started from a post by @pydantic, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PydanticAI: give function tools run dependencies
Summary: As of November 2026, set `deps_type`, accept `RunContext[YourDeps]` first, and read `ctx.deps`; supply an instance through `deps` on each run. Context is excluded from the model-facing tool schema. Register dependency-aware functions with `@agent.tool`.
Copy for agent
get the whatworks thread pydanticai-register-tools-with-run-dependencies and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydanticai-register-tools-with-run-dependencies
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp