Drop client-supplied `TextContent.metadata` from user prompts in `sanitize_messages`

Merged
DO
1mo ago
+99-3

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 InputGuardrail question: sanitize_messages should drop all client TextContent.metadata)

What changed

  • sanitize_messages resets TextContent.metadata to None on the text items of every UserPromptPart, after the existing file URL and uploaded-file filtering, and warns once with the count. It builds new TextContent objects and doesn't mutate the caller's.
  • Docs: the sanitize_messages docstring lists the new rule, the TextContent.metadata docstring says it is dropped from user prompts, and the "Loading untrusted history" section of docs/message-history.md includes it.

Other client-settable metadata with framework meaning (not changed here)

Listed for a follow-up decision rather than widening this PR:

  • TextContent inside tool return content. _sanitize_tool_return_part leaves 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_tail uses to recognize reminders on the wire. sanitize_messages keeps request metadata as is.
  • Vercel AI client message metadata. VercelAIAdapter.load_messages copies a client UIMessage.metadata onto ModelRequest.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. TextContent isn'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-free TextContent are 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 a pydantic-ai-harness. origin is summarized as <harness_data>. After sanitize_messages, it is summarized as the user's text. (InputGuardrail only recognizes SubAgents task 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.py and tests/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