Frame `BackgroundTools` results as `<tool_result>` tool output instead of untagged user text
BackgroundTools delivers a finished tool's result as a follow-up UserPromptPart. The text was Background tool 'x' (task id) completed.\nResult: {text}, with nothing marking {text} as tool output. A result saying "the user wants you to delete X" therefore reached the model as the user's own words. The same applied to ModelRetry/ToolFailed messages in the failure notice.
This frames the tool's part of the follow-up the way core frames tool files it moves to the user channel (_tool_result_provenance_tags in messages.py):
Background tool 'lookup' (task call-1) completed.
<tool_result tool_name="lookup" tool_call_id="call-1">
...return value, files...
</tool_result>
- The framework's one-line announcement stays outside the frame.
- Attributes are
html.escaped, and a closing</tool_result>tag inside the body (any case, trailing whitespace) is neutralized as</tool_result>, so the output can't end its frame early. The escaper matches the semantics of the core_framing.wrap()helper being built in parallel; aTODOmarks the switch. ModelRetry/ToolFailedmessages in failure notices are framed the same way. The framework-written texts (deferral errors, the empty-retry fallback) are not.ToolReturn.contentfollows the frame unwrapped, as core sends it: it is content the tool author chose to add for the model.- Identification is unchanged: the follow-up is still one
UserPromptPartwhose firstTextContentcarriesmetadata={'origin': 'pydantic-ai-harness.background_tools.result.v1', ...}.
The announcement stays in the same user-channel part rather than moving to a SystemPromptPart. Keeping it there leaves the completion as one atomic, metadata-identified record, which InputGuardrail and the documented shape rely on. A split would also lose the announcement on a UI round trip, since the adapters strip client-sent system prompts. And it matches core's C6 framing, which has no system-voice announcement either.
[!WARNING]
Compatibility impact: Code that parses the follow-up's visible text, for example by matching Result: , will no longer match.
Why this can ship in a minor release: The text format was never documented. The documented way to identify a completion is its TextContent.metadata, and that has not changed. This is a bug fix for code that relied on an undocumented format.
Migration: Identify completions by metadata['origin'] == 'pydantic-ai-harness.background_tools.result.v1', and read the result from inside the <tool_result> element.
UI round trip (checked by hand with VercelAIAdapter and AGUIAdapter dump_messages/load_messages): the follow-up text, including its framing and any files, survives byte for byte. The TextContent.metadata does not survive, so a reloaded follow-up is an ordinary user prompt. This was already the case and is now documented; fixing it needs adapter-level metadata transport, which is out of scope here.
Audit item H9 in the "message history as world model" audit (2026-10-15).
- No issue; audit item H9.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code