Add `ToolBridge`, which keeps the tool list fixed behind `tool_search`, `tool_describe` and `tool_call` so the prompt cache holds for large or changing tool sets
- Fixes #8998
Adds ToolBridge to Pydantic AI Harness: a capability that replaces the agent's tools with three fixed dispatcher tools, tool_search(query), tool_describe(name) and tool_call(name, args_json). The tool definitions sent to the provider stay the same however many tools sit behind the bridge and however they change mid-run, so the cached prefix holds on every provider, including ones with no native deferral or reveal channel.
from pydantic_ai import Agent
from pydantic_ai_harness import ToolBridge
agent = Agent('anthropic:claude-opus-5-5', capabilities=[ToolBridge()])
How it works
ToolBridgeToolsetwraps the agent's toolset. Tools thetoolsselector picks (default'all') move behind the bridge, and the rest stay native.tool_callruns the bridged tool through a nestedToolManager, the same pathCodeModeuses for sandboxed calls. Argument validation, tool hooks,ModelRetry,HandleDeferredToolCalls, usage and instrumentation spans apply to it as to a direct call. An unknown name or invalid arguments ask the model to retry.- A bridged tool that raises
ApprovalRequiredorCallDeferredis resolved only by aHandleDeferredToolCallshandler, which sees the tool's own name. Without one, the run fails with aUserErrorthat points toToolBridge(tools=...), rather than deferringtool_call, so an approval oftool_callnever covers whichever tool the model named. Tools underapproval_required()therefore need to be kept native or paired with a handler. - Bridged tools with
defer_loading=Truecan be found withtool_search, soToolSearchhas nothing left to reveal.tests/harness/test_history_append_only.pynow has aToolBridgescenario, and a test checks that the tool list stays the same while a dynamic toolset gains a tool mid-run.
Choices made (no maintainer input yet; please push back)
tool_calltakesargs_json: str, not a nested object. This follows the benchmark in the issue comments: with a free-formargs: object, some Gemini models sendnull. An empty string is read as{}.- Some tools always stay native: tools that need approval or run externally (
kind != 'function'), so approval requests name the real tool; tools with atool_kind(framework control tools,read_file), which the run and capabilities such asToolOutputLimits,Memoryand compaction find by kind;unless_nativefallbacks; and deferred tools that a capability owns, which core's capability loading reveals without moving the cached prefix. Tools that step aside for a kind (unless_tool_kind) also stay native, so core's filter still sees them. - Disclosure policy: every bridged tool can be searched, described and called. The issue's three-tier policy (searchable / describable / callable) is left for a follow-up if there's demand. The
toolsselector is the single knob for now. - Search defaults (
search=None) to core's keyword matching, the same functionToolSearchuses, and takes anyToolSearchFunc. - Combine policy: the default
id='tool_bridge'means two bridges merge into one, since the dispatcher names are fixed. - Category: Context management, next to Tool Search.
- Telemetry: the bridge adds no spans of its own. The bridged call gets its own tool span through the normal hooks, and a test covers this.
- Retries and scheduling: the dispatchers'
max_retriesdefaults to the agent's tool retry budget (ctx.max_retries), as core'sToolSearchToolsetdoes. A bridged tool's retries are counted under its own name in the run's tool manager, as for a direct call, so itsctx.retryandmax_retriessee only its own failures. Each failure also spends atool_callretry.tool_callissequentialwhenever a bridged tool is.sequentialonly affects local scheduling, so the provider's tool list doesn't change. THREAT_MODEL.md: listsToolBridgebeside Code Mode as an execution path that has to keep approval controls. It also notes that approvals raised from inside a bridged tool nametool_call, and that bridged tool definitions arrive as tool results.- Shared internals:
ToolBridgeuses thetool_calls_limitreservation from_code_tools.reserve_nested_call, whichCodeModeandSubAgentsalready use, andis_code_execution_tool. - Public core APIs only:
unless_tool_kindhandling doesn't use core's private filter. Tools with a kind orunless_tool_kindstay native, so the run's own filter picks the same tools it would without the bridge. The default search is core'skeywords_search, which this PR exports frompydantic_ai.capabilities(a one-line re-export next toToolSearchFunc), and a customToolSearchFunccan replace it. The nestedToolManagerreceives the run manager'sfailed_toolsandsucceeded_toolssets through its public constructor, so core's retry handling counts bridged tools' retries under their own names. TOOL_CALL_KIND:tool_callhas the tool kindtool_bridge.tool_call, exported withToolCallArgsfrompydantic_ai_harness.tool_bridge, so approval UIs and other capabilities can recognize it whatever it is named.- Ordering:
ToolBridgedeclaresCapabilityOrdering(position='outermost', wrapped_by=[ToolSearch]), so it sits just inside tool search and outside every other wrapper capability.SetToolMetadataandPrefixToolstherefore apply to the bridged tools whatever order the capabilities are listed in.CodeModeandIPythonCodeModenow also declarewraps=[..., ToolBridge]. - Code-execution tools stay native (
code_arg_namemetadata, asCodeModeuses), so a program isn't sent JSON-escaped insideargs_json.
This PR changes core code outside pydantic_ai_harness, so it needs Aditya's (@adtyavrdhn) review. The core change exports the keyword search that ToolSearch(strategy='keywords') uses as pydantic_ai.capabilities.keywords_search. It's renamed from the private toolsets._tool_search.keywords_search_fn, since no other pydantic_ai.capabilities export ends in _fn. Outside harness, docs (the index, the capabilities overview, navigation) and THREAT_MODEL.md also changed.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
- PR title is fit for the release changelog.