Skip to content

Mask tool output from every capability in Common AI agents - #73993

Merged
kaxil merged 3 commits into
apache:mainfrom
astronomer:commonai-capability-tool-masking
Oct 14, 2026
Merged

kaxil merged 3 commits into
apache:mainfrom
astronomer:commonai-capability-tool-masking

Conversation

@kaxil

@kaxil kaxil commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

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 a WrapperToolset around the toolsets AgentOperator could find: toolsets=, agent_params["toolsets"], and a Toolset capability holding a toolset. Everything else reached the model unmasked:

  • tools from any other capability: MCP, PrefixTools, CombinedCapability, a Toolset built per run, or a Dag author's own capability exposing tools
  • function tools passed in agent_params["tools"]
  • every tool given to LLMOperator and the other LLM operators, which pass agent_params straight to PydanticAIHook.create_agent

create_agent now adds a MaskingCapability that masks around every tool call with pydantic-ai's wrap_tool_execute hook, so it applies to every agent the provider builds, including one a @task builds with the hook itself. An agent built with Agent(...) gets the same masking by adding the capability last:

from airflow.providers.common.ai.utils.toolset_base import MaskingCapability

agent = Agent(model, toolsets=[my_toolset], capabilities=[MaskingCapability()])

Two related leaks are fixed along the way:

  • Retry and failure messages. By the time a capability sees a tool's ModelRetry or ToolFailed, pydantic-ai has wrapped it in a ToolRetryError or ToolFailedError, and the model is sent the RetryPromptPart or failed ToolReturnPart inside, 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 an InitVar without a default and resets init=False fields; 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-valued ToolReturn.metadata or tool result in the message history of every agent create_agent builds; 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 covers AgentOperator, the LLM operators and hand-built agents alike. AgentOperator no 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 a toolsets= 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 MaskingCapability declares the innermost position. pydantic-ai breaks ties between capabilities that all ask for innermost by list order, so create_agent appends it after the caller's capabilities. A test pins both.

Why MaskingCapability is public. A Dag author who builds Agent(...) directly has no other way to get the masking create_agent applies. 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_request hook 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 (ToolOutput or a function output_type) returns or raises, a result a capability returns in place of running the tool, and tools the model provider runs itself (native WebSearch, WebFetch or ImageGeneration, and MCP(native=True)). The observability page and the capture_content option 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 MaskingCapability docstring names: one passed per run to agent.run(capabilities=...), which pydantic-ai wraps inside the agent's own capabilities, and a toolset wrapper a capability adds with get_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, and ToolLoggingCapability writes to the task log, which masks on the way out.

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
kaxil force-pushed the commonai-capability-tool-masking branch from 9493f44 to b0f813e Compare October 14, 2026 09:31
kaxil added 2 commits October 14, 2026 10:47
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
kaxil marked this pull request as ready for review October 14, 2026 10:53
@kaxil
kaxil merged commit 3259e89 into apache:main Oct 14, 2026
158 of 163 checks passed
@kaxil
kaxil deleted the commonai-capability-tool-masking branch October 14, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants