Repository navigation
Mask tool output from every capability in Common AI agents - #73993
Merged
Merged
Conversation
kaxil
force-pushed
the
commonai-capability-tool-masking
branch
from
October 1, 2026 08:47
80e374e to
9493f44
Compare
Agents built by Common AI masked tool results only for toolsets AgentOperator could find and wrap: toolsets=, agent_params toolsets and a Toolset capability holding a toolset. Tools that arrive through any other capability (MCP, PrefixTools, CombinedCapability, a Toolset built per run), function tools in agent_params, and every tool given to LLMOperator and the other LLM operators reached the model unmasked. PydanticAIHook.create_agent now adds a MaskingCapability that masks around every tool call, innermost so other capabilities see only masked results. It also masks the retry or failure message pydantic-ai builds from a tool's ModelRetry or ToolFailed, and a ToolReturn's metadata. Masking a dataclass result now copies it and masks the copy's fields instead of rebuilding it with dataclasses.replace, which failed on an InitVar without a default and reset init=False fields.
kaxil
force-pushed
the
commonai-capability-tool-masking
branch
from
October 14, 2026 09:31
9493f44 to
b0f813e
Compare
Running out of tool retries is no longer logged as the tool failing: the UnexpectedModelBehavior pydantic-ai raises below MaskingCapability is logged at debug and stripped like a retry. mask_secrets keeps a Pydantic model that holds no registered secret as the model, so a ToolReturn's metadata or a model a tool returns still has its attributes in the message history. The comment on why AgentOperator keeps toolset-level masking now names the real reason, toolset wrappers a capability adds below the capability hooks; the durable journal masks what it records itself, and its test now covers a leaf toolset AirflowDurability journals as well as a durable unit. The MaskingCapability docstring credits create_agent, shows how to add it to an agent built with Agent(...), and names what still sees a result before it is masked. The docs list output functions as not masked, and the Pydantic AI framework page and the capture_content option no longer contradict the masking create_agent now does.
Mask a ToolReturn and the message part inside a ToolRetryError or ToolFailedError through mask_secrets' own dataclass handling instead of listing fields by hand, which also masks a failed ToolReturnPart's metadata. Move the comment on why AgentOperator keeps toolset-level masking next to that code, and drop an operator test the hook test already covers.
kaxil
marked this pull request as ready for review
October 14, 2026 10:53
vatsrahul1001
approved these changes
Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Common AI passes what a tool returns, and the error text it hands the model, through Airflow's secret masker, so a connection password in a database error or a hook's return value reaches the model, the provider and traces as
***. That masking was aWrapperToolsetaround the toolsetsAgentOperatorcould find:toolsets=,agent_params["toolsets"], and aToolsetcapability holding a toolset. Everything else reached the model unmasked:MCP,PrefixTools,CombinedCapability, aToolsetbuilt per run, or a Dag author's own capability exposing toolsagent_params["tools"]LLMOperatorand the other LLM operators, which passagent_paramsstraight toPydanticAIHook.create_agentcreate_agentnow adds aMaskingCapabilitythat masks around every tool call with pydantic-ai'swrap_tool_executehook, so it applies to every agent the provider builds, including one a@taskbuilds with the hook itself. An agent built withAgent(...)gets the same masking by adding the capability last:Two related leaks are fixed along the way:
ModelRetryorToolFailed, pydantic-ai has wrapped it in aToolRetryErrororToolFailedError, and the model is sent theRetryPromptPartor failedToolReturnPartinside, not the exception's text. Those parts are masked, and neither an ordinary retry nor running out of retries is logged as a tool failure.ToolReturn.metadata. Not sent to the model, but kept in message history and recorded in traces; it was never masked.Extending masking to these tools surfaced two problems in the masker itself. It rebuilt a dataclass result with
dataclasses.replace, which raises on anInitVarwithout a default and resetsinit=Falsefields; it now copies the object and masks the copy's fields. And it turned every Pydantic model into a dict, which would have changed the type of a model-valuedToolReturn.metadataor tool result in the message history of every agentcreate_agentbuilds; a model holding no registered secret is now returned as it is.Design rationale
Why in the hook, not the operator. Every Common AI operator builds its Pydantic AI agent with
PydanticAIHook.create_agent, so it is the one place that coversAgentOperator, the LLM operators and hand-built agents alike.AgentOperatorno longer adds anything itself.Why toolset-level masking stays. Toolset wrappers that a capability adds with
get_wrapper_toolset,ToolLoggingCapability's among them, run below every capability's hooks, so without masking the toolset itself they would see the unmasked result of atoolsets=tool.toolsets=results are therefore masked twice; the second pass is small next to the model call that consumes the result. The durable journal does not depend on either layer: it sits below both and masks what it records on its own, and a test now pins that for both paths it records through.Why innermost, and appended last. Other capabilities should only ever see a masked result, so
MaskingCapabilitydeclares the innermost position. pydantic-ai breaks ties between capabilities that all ask for innermost by list order, socreate_agentappends it after the caller's capabilities. A test pins both.Why
MaskingCapabilityis public. A Dag author who buildsAgent(...)directly has no other way to get the maskingcreate_agentapplies. It is a plain pydantic-ai capability with no configuration, so it composes with the author's own capabilities the same way the built-in ones do. The Pydantic AI framework page previously said no public masking wrapper existed; it now documents this one.Why not mask at the model-request boundary as well. A
before_model_requesthook could mask every tool-return and retry part just before it goes to the model, which would also catch the paths below. pydantic-ai keeps the unmasked originals in message history, so that layer would re-mask the whole conversation on every request, and the paths it adds need Dag-author code that puts a secret into a validator's message or a capability's own result. They are listed as not masked in the security docs instead.Gotchas
These are still not masked, and the security page now says so: errors raised while validating a tool's arguments or the agent's output, what an output function (
ToolOutputor a functionoutput_type) returns or raises, a result a capability returns in place of running the tool, and tools the model provider runs itself (nativeWebSearch,WebFetchorImageGeneration, andMCP(native=True)). The observability page and thecapture_contentoption no longer say tool output reaches traces unredacted; tool arguments and output functions still do.Two kinds of capability still see a tool's result before it is masked, which the
MaskingCapabilitydocstring names: one passed per run toagent.run(capabilities=...), which pydantic-ai wraps inside the agent's own capabilities, and a toolset wrapper a capability adds withget_wrapper_toolset, for tools a capability supplies. The model still gets the masked result in both cases. No Common AI operator passes per-run capabilities, andToolLoggingCapabilitywrites to the task log, which masks on the way out.