Give capabilities a first-class way to opt their tools out of CodeMode's run_code
Context
pydantic/pydantic-ai-harness#592 showed that CodeMode was folding Shell.run_command into run_code, forcing the model to quote a shell script inside a generated Python string. pydantic/pydantic-ai-harness#593 fixed the immediate cases by marking the command tools (and author_capability) with code_arg_name metadata, which _is_code_execution_tool reads to leave them native.
That marking was agreed in pydantic/pydantic-ai-harness#592 to be an interim measure. This issue tracks the real design: a first-class way for capabilities to opt their tools out of run_code.
Problems with the interim mechanism
- Marker overload.
code_arg_namewas a telemetry hint ("render this argument as code in traces", consumed bypydantic_ai/_instrumentation.py). pydantic/pydantic-ai-harness#593 canonized it as a fold-policy flag as well. A tool author can no longer get code rendering in traces without also opting out of CodeMode, and vice versa. The two concerns need separate knobs. - Users cannot express the opt-out themselves.
CodeMode(tools=...)takes aToolSelector, but selecting requires knowing the internal tool names a capability registers (run_command,start_command, ...). Users pairingShell + CodeModeoften do not know Shell handed the model a tool at all. The selector should be expressible at the capability level, not the tool-name level. - Split tool families. Opting out
start_commandwhilecheck_command/stop_commandstay folded splits Shell's background lifecycle across two calling conventions, with the command id hand-copied between a native tool result and sandboxed code. A capability-level opt-out could keep a family together; the metadata marker cannot, because the id tools carry no code argument to mark. - Untested rendering value. pydantic/pydantic-ai-harness#593 introduced
code_arg_language: 'shell'; every prior producer used'python'. Logfire renderer behavior for non-python values is unverified.
Direction discussed in pydantic/pydantic-ai-harness#592
Extend the tools field on CodeMode (or add a sibling field) so users and capability authors can filter by capability rather than by internal tool name. Sketch from the pydantic/pydantic-ai-harness#592 thread: let a capability declare which of its tools are foldable, and let CodeMode consume that declaration, keeping code_arg_name purely as the telemetry hint it started as.
Open questions:
- Declaration site: a method or attribute on
AbstractCapability, per-tool metadata under a dedicated key, or aToolSelectorextension that matches capability identity. - Whether opt-out is all-or-nothing per capability or per tool group (the
start/check/stopfamily argues for groups). - Migration: the pydantic/pydantic-ai-harness#593 markings should become the new mechanism's first users, and
_is_code_execution_toolshould shrink back to genuine code surfaces (run_code,run_workflow).
References
- pydantic/pydantic-ai-harness#592 (original report and interim decision)
- pydantic/pydantic-ai-harness#593 (interim fix, boundary rationale in
_is_code_execution_tool's docstring)
Source: pydantic/pydantic-ai