Frame a capability's tool content at its authority ceiling when the tool is called through Code Mode or `ToolBridge`

DS
8h ago
+648-33
  • No linked issue.

Why we make these changes

Custom capabilities documents that the ToolReturn.content of a tool a capability at 'none' contributes goes out as <capability_content> data naming the capability, not as user text. That held when the model called the tool directly. Called from run_code (CodeMode, IPythonCodeMode) or ToolBridge's tool_call, the content was re-emitted as the calling tool's own, so the calling capability's authority applied and it went out as plain user text.

The nested paths now return that content with the capability each tool came from, and core holds each tool's content to each capability it passed through, innermost first: its own, then the calling tool's. That also covers code that calls tool_call from run_code.

Two separate commits fix the docstrings of RetryFeedbackPart and RetryFeedbackCause, which said retry feedback goes out in the user voice: OpenAIDecisionsModel sends it as a developer message, as the Decisions API docs say.

New public surface

None.

User-visible behavior

from pydantic_ai import Agent, ToolReturn
from pydantic_ai.capabilities import Capability
from pydantic_ai_harness import CodeMode


def fetch() -> ToolReturn:
    return ToolReturn(return_value='ok', content='page text')


agent = Agent(..., capabilities=[Capability(id='plugin', tools=[fetch]).with_authority('none'), CodeMode()])
# The model's `run_code` calls `await fetch()`. The next request carries:
# before: 'page text' as plain user text
# after:  the same frame a direct `fetch` call gets: a note naming the `plugin` capability, then
#         <capability_content>page text</capability_content>

Verification

  • tests/harness/code_mode/test_code_mode.py::TestCodeMode::test_tool_return_content_is_held_to_its_capabilitys_authority
  • tests/harness/tool_bridge/test_tool_bridge.py::TestToolBridge::test_bridged_tool_content_is_held_to_its_capabilitys_authority
  • tests/harness/tool_bridge/test_tool_bridge.py::TestToolBridge::test_code_mode_keeps_a_bridged_tools_capability
  • tests/harness/ipython_code_mode/test_ipython_code_mode.py::TestIPythonCodeModeTools::test_tool_return_content_is_held_to_its_capabilitys_authority
  • tests/harness/ipython_code_mode/test_ipython_code_mode.py::TestIPythonCodeModeTools::test_a_bridged_tools_content_is_held_to_its_capabilitys_authority

Each compares what the model receives against a direct call of the same tool, and fails without the fix. The Code Mode tests also pin that content from other tools around it stays user text, in call order.

What changes for existing users

Only content from a tool of a capability below 'operator' that a nested call returns is framed differently; every other nested content keeps the single user part it had.

PR decisions

2026-11-24 · Nested content carries its capability through ToolReturn.content, not ctx.enqueue · iter -

  • Decision: Code Mode, IPythonCodeMode and ToolBridge return nested ToolReturn.content as NestedCallContent (private, pydantic_ai._tool_execution); core's _tool_user_parts holds each tool's content to its own capability, then to the calling tool's
  • Why: Enqueued 'asap' content can force one more model request when a run would otherwise end, and ctx.enqueue raises inside durable units; a direct call's ToolReturn.content does neither
  • Source: https://github.com/pydantic/pydantic-ai/tree/nested-call-content-framing
  • Session: c2361f9a-a90d-45f9-aeca-a0a4bb7a329a

2026-11-24 · IPythonCodeMode is in scope · iter -

2026-11-24 · pre-push review 1 of 3 at e288ede08cf2d71daabb8d961dc017d9c44636ec · iter -

2026-11-24 · pre-push review 2 of 3 at fe819d9e78861324c4bdb1e80d2626bcacfb1221 · iter -

2026-11-24 · exempt delta ab5b2faca1 from reviewed fe819d9e78 · iter -

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.

Summary by cubic

Fixes capability authority framing for nested tool content, so a tool's ToolReturn.content reaches the model with its own capability's authority when called through run_code (CodeMode, IPythonCodeMode) or ToolBridge's tool_call, matching a direct call. Previously the nested paths re-emitted that content as the calling tool's own, so a capability at 'none' authority had its content pass as plain user text.

  • Held-to-capability content now carries the run id of every capability it passed through, innermost first; content that stays user text keeps the single part it had.
  • ToolBridge keeps a bridged tool's capability when code calls tool_call, and CodeMode/IPythonCodeMode preserve it in their collection of nested results.
  • A bridged tool no capability below 'operator' contributes keeps its ToolReturn.content as returned, so hooks and result events see the original str rather than a list.
  • A hook that edits run_code's or tool_call's ToolReturn.content in place now reaches the model as edited, held to every capability that contributed to it; replaced content is held to the calling tool's capability alone.
  • Corrects RetryFeedbackPart and RetryFeedbackCause docstrings: OpenAIDecisionsModel sends retry feedback as a developer message, not in the user voice.
  • Refactors _tool_user_parts, shares str-or-sequence flattening, and inlines the ToolReturn guard in ToolBridge._call so importing the media store no longer loads pydantic_ai.

Verification

  • New tests pin that nested content matches a direct call across CodeMode, IPythonCodeMode, bridged calls, code calling tool_call from run_code, in-place hook edits, a chain where each capability lowers the content further, and a bridged tool at no or 'operator' authority; they fail without the fix.

No breaking changes and no new public surface.