Add `SystemPromptPart.metadata` to identify system prompts across merged requests and continued conversations

Merged
DO
2mo ago
+353-1

Adds SystemPromptPart.metadata: dict[str, Any] | None = None, so code that adds a system prompt can find it again by metadata rather than by its visible text.

ModelRequest.metadata can't do this. When _merge_consecutive_messages merges adjacent requests (in the history a run starts with, and in each request it prepares), the merged request keeps only the __pydantic_ai__ namespace. So a request-level marker on an enqueued system prompt is gone once the conversation continues. Parts survive the merge, so the marker belongs on the part. TextContent.metadata already does this for user content (#10550), but SystemPromptPart.content is a plain str.

What the field does:

  • Not sent to the model. Provider adapters only read content. The test compares request bodies byte for byte with and without metadata on a standing prompt, a mid-conversation prompt and a request-scoped prompt, for OpenAI Chat, OpenAI Responses, Anthropic, Google and Mistral. It uses a mock transport because cassette matchers ignore the body.
  • Serialization. It round-trips through ModelMessagesTypeAdapter (JSON and Python). Payloads without the field load with None. Older versions ignore the unknown key (checked: unknown part keys are dropped on validation). None is emitted in dumps, the same as dynamic_ref and scope. No existing snapshot includes a dumped SystemPromptPart, so none changed.
  • Merges and continuation. Tested end to end: a tool enqueues ModelRequest([SystemPromptPart(..., metadata=...)], metadata=...), and the next run continues from all_messages(). The merged request has lost its request metadata, but the part keeps its own, both in all_messages() and in what capabilities see in request_context.messages.
  • Dynamic system prompt re-evaluation. This is the one place the core rebuilds a SystemPromptPart from an existing one (_reevaluate_dynamic_prompts), and it now carries metadata over. A one-line change, tested.
  • ReinjectSystemPrompt, compaction trimming, request-scoped projection. They keep or drop whole parts and never rebuild them, so metadata rides along with no change. There's a test for the reinjector with replace_existing=True keeping a capability-added part along with its metadata.
  • UI adapters (Vercel AI, AG-UI): dropped. This follows the existing rule for TextContent.metadata: application-only fields have no place in those wire formats. A system prompt loaded from a frontend has no metadata, so it can't be forged that way. No code change.
  • OTel: not reported, the same as TextContent.metadata.

Docs: a new "Identifying system prompts" section in docs/message-history.md (after request-scoped system prompts), with a runnable capability example: identify framework records by metadata, never by visible text. The field's docstring links to it, and the agent skill's INPUT-AND-HISTORY.md has a matching paragraph.

Decisions (DouweM)

  1. Name and type: metadata: dict[str, Any] | None, matching ModelRequest.metadata and ModelResponse.metadata. __pydantic_ai__ isn't documented as reserved yet; that waits for the follow-up that uses it.
  2. No typed origin field for now. An origin convention can go into metadata in the follow-up that stamps origins.

Follow-ups (not in this PR)

  • TrajectoryJudge steering identification (#10545): mark the steer with part metadata instead of request metadata.
  • CodeMode announcement marker (#10559).
  • RepoContext nested notes (P16 in the instruction-source audit).
  • ReinjectSystemPrompt(replace_existing=True): tell agent-origin parts from framework parts (stamp origin on agent and enqueued prompts, replace agent-origin and unmarked parts, keep marked framework parts). sanitize_messages strips client-supplied __pydantic_ai__.

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. (None.)
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code