Add `SystemPromptPart.metadata` to identify system prompts across merged requests and continued conversations
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 withNone. Older versions ignore the unknown key (checked: unknown part keys are dropped on validation).Noneis emitted in dumps, the same asdynamic_refandscope. No existing snapshot includes a dumpedSystemPromptPart, so none changed. - Merges and continuation. Tested end to end: a tool enqueues
ModelRequest([SystemPromptPart(..., metadata=...)], metadata=...), and the next run continues fromall_messages(). The merged request has lost its request metadata, but the part keeps its own, both inall_messages()and in what capabilities see inrequest_context.messages. - Dynamic system prompt re-evaluation. This is the one place the core rebuilds a
SystemPromptPartfrom an existing one (_reevaluate_dynamic_prompts), and it now carriesmetadataover. 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 withreplace_existing=Truekeeping 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)
- Name and type:
metadata: dict[str, Any] | None, matchingModelRequest.metadataandModelResponse.metadata.__pydantic_ai__isn't documented as reserved yet; that waits for the follow-up that uses it. - No typed
originfield for now. An origin convention can go intometadatain 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 (stamporiginon agent and enqueued prompts, replace agent-origin and unmarked parts, keep marked framework parts).sanitize_messagesstrips 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