Drop client-supplied `TextContent.metadata` from user prompts in `sanitize_messages`
sanitize_messages makes client-submitted history safe to pass as message_history, but it kept TextContent.metadata on user prompt content unchanged. That metadata is never sent to the model, but application code and capabilities read it to recognize content they added themselves. Harness, for example, recognizes background tool completions, compaction pins and receipts, and (with #10550) SubAgents task reports this way, and InputGuardrail treats a recognized task report as not being the user's prompt. A client that can set this metadata can therefore pass its own text off as one of those records. sanitize_messages now resets TextContent.metadata to None on every user prompt item, keeping the text, and warns when it does.
[!WARNING]
Compatibility impact: sanitize_messages now drops TextContent.metadata from user prompt content and emits a UserWarning naming how many items it changed. Code that passes history through sanitize_messages and then reads its own TextContent.metadata back (for example its own pinned or tagged content) will find None.
Why this can ship in a minor release: sanitize_messages is documented as stripping what isn't safe to honor from untrusted input, and client-supplied metadata that grants framework meaning is exactly that. Relying on client-supplied metadata surviving sanitization was never documented.
Migration: pass history that carries your own metadata as trusted server-side message_history instead of sanitizing it, or reapply the metadata after sanitize_messages. The UI adapters are unaffected: their loaders never build TextContent with metadata, so the warning never fires on that path.
- Refs #10550 (DouweM's decision on its
InputGuardrailquestion:sanitize_messagesshould drop all clientTextContent.metadata)
What changed
sanitize_messagesresetsTextContent.metadatatoNoneon the text items of everyUserPromptPart, after the existing file URL and uploaded-file filtering, and warns once with the count. It builds newTextContentobjects and doesn't mutate the caller's.- Docs: the
sanitize_messagesdocstring lists the new rule, theTextContent.metadatadocstring says it is dropped from user prompts, and the "Loading untrusted history" section ofdocs/message-history.mdincludes it.
Other client-settable metadata with framework meaning (not changed here)
Listed for a follow-up decision rather than widening this PR:
TextContentinside tool return content._sanitize_tool_return_partleaves it as is. Nothing I found reads it to grant meaning, but the same rule would apply if something starts to.ModelRequest.metadata['__pydantic_ai__']. Core reads keys in this namespace, for example the request-scoped system prompt tail count that_request_scoped_tailuses to recognize reminders on the wire.sanitize_messageskeeps request metadata as is.- Vercel AI client message
metadata.VercelAIAdapter.load_messagescopies a clientUIMessage.metadataontoModelRequest.metadata(next to its own__pydantic_ai__.ui_message_id), so a client sets top-level request metadata keys on that path. No framework check reads them today.TextContentisn't affected: the loader never builds it with metadata. ToolReturnPart.metadata['discovered_tools']. Core's tool search reads this to decide which deferred tools were discovered. A client could submit a tool return claiming discoveries.SystemPromptPart.metadata(#10641) identifies system prompts, but client system prompt parts are stripped by default (strip_system_prompts=True).CompactionPart.provider_details[STANDING_PROMPT_PLANTED_KEY]is already stripped.
Verification
tests/test_sanitize_messages.py: forged harness-origin metadata is dropped with the text kept, a string item and a metadata-freeTextContentare kept as they were, the caller's objects aren't mutated, and content without metadata produces no warning.tests/harness/compaction/test_compaction.py::TestFormatMessages::test_sanitized_client_history_cannot_pass_as_harness_data: a real consumer of the metadata. Without sanitizing, client content carrying apydantic-ai-harness.origin is summarized as<harness_data>. Aftersanitize_messages, it is summarized as the user's text. (InputGuardrailonly recognizesSubAgentstask reports once #10550 lands, so it isn't the test here.)- Targeted pytest for
tests/test_sanitize_messages.py,tests/test_vercel_ai.py,tests/test_ag_ui.pyandtests/test_ui.py, single-file Pyright and Ruff on touched files.
Checklist
- No breaking changes in accordance with the version policy. (See the compatibility note.)
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (No public signature changes, so no API-check waiver.)
- PR title is fit for the release changelog.
š¤ Generated with Claude Code