[voice-authority] Note text that looks like an application frame in user and tool content instead of escaping it

Merged
DO
22d ago
+15604-9362

Part of #11029 groundwork.

Pydantic AI used to tell its own frames apart from lookalike text by rewriting the lookalike: on models without mid-conversation system messages, <system> in user prompts and tool returns went out as &lt;system>, inlined text files were escaped on OpenAI Chat-style providers, and core frames escaped framing tags inside their bodies. Models copy the &lt; back out, so a "read this XML file, change one value, write it back" loop wrote corrupted bytes, and an escaped lookalike was still followed. This PR stops rewriting content and says whose text it is instead.

[!WARNING]
Compatibility impact: Request bytes change, so each affected conversation misses its prompt cache once after upgrading, and tests that snapshot requests see new text:

  • the framework's data frames are renamed with a pyai: prefix: <tool_result> → <pyai:tool_result>, <retry_feedback> → <pyai:retry_feedback>, <validation_errors> → <pyai:validation_errors>, <conversation_summary> → <pyai:conversation_summary>, <context> / <context_diff> → <pyai:context> / <pyai:context_diff>. That touches every retry, every compaction summary, every tool file inlined into a user turn and every append-mode instruction change. Code that matches these tags in stored or logged prompts must match the new names.
  • user, tool and file text with a lookalike is sent as written plus a note, instead of escaped;
  • the announcement's text changes (it now also says what the notes mean), it is also needed from the first lookalike note or user-channel frame (retry feedback, a compaction summary), not only the first <system> block, and Claude Sonnet 4.6 and gpt-oss get it on every request by default ('always');
  • Claude Sonnet 5.5, Claude Haiku 5.5 and Grok 4.3–4.7 move to the sealed <system verified> rendering;
  • a request's tool results now go out ahead of its other parts on every adapter.

Attribute names and StructuredContent.tag values outside snake_case are deprecated (they still work and warn). Names starting with pyai: are refused for registered tags, attributes, StructuredContent.tag and system-voice tags; the snake_case rule already refused the colon, so no name that worked before breaks.

Why this can ship in a minor release: The rendered prompt text is framework-authored and not public API; no public API is removed, and the additions are opt-in settings and profile fields.

Migration: Match <pyai:…> tag names where you parse stored prompts. To keep the announcement on every request, set model_settings={'framing_announcement': 'always'}. To keep a model on its native system role, set supports_inline_system_prompts=True in its profile.

What it does

  • No content rewriting. User prompts, uploaded files, tool results and the bodies of core frames go out byte for byte. The fallback <system> escaping, the OpenAI Chat inlined-file escape and the escaping inside core frames are removed. Transcripts flattened for a second model (summarizers, judges) still escape and get no notes or announcement. The public pydantic_ai.framing helpers (wrap, neutralize_tags, register_tag) behave as before.
  • Notes on lookalikes. User, tool, file, compaction-summary, retry-feedback and carried-thinking text that imitates a framework frame gets a short note in the application's voice, saying whose text it is and that it has no authority. A tool result's note names the tool and the wire call id. Matching handles case and spacing, entity-encoded <, full-width forms, zero-width characters and Cyrillic/Greek look-alike letters. A capability's own user-channel frame is marked privately in TextContent.metadata, which UI adapters strip from client history, so it isn't flagged.
  • pyai: namespace for core data frames: <pyai:tool_result>, <pyai:retry_feedback>, <pyai:validation_errors>, <pyai:conversation_summary>, <pyai:context>, <pyai:context_diff>. <system>, <system verified> and <assistant_thinking> stay unprefixed. Notes go only on copies of the prefixed names (and on <system>), so an application's own plain <context> or <tool_result> documents are ordinary text and get no note.
  • Sealed rendering for profiles with ModelProfile.needs_sealed_system_blocks (Claude Sonnet 4.6, Sonnet 5.5, Haiku 5.5; Grok 4.3, 4.5, 4.6, 4.7 and their -latest aliases; not Grok 4.20). Each mid-conversation block is <system verified>, goes after the user's words in its request, and the announcement says every application block carries the attribute. A copied attribute in user, tool or file text still gets a note. Sonnet 5.5 and Haiku 5.5 leave the native-role list on Anthropic and OpenRouter for this, and keep their request-scoped clear_at entries, each holding a sealed block.
  • Announcement. On models without mid-conversation system messages, a fixed text (~120 words, ~185 sealed) says what <system> blocks and notes mean. It opens the system prompt, ahead of both the standing system_prompt and the instructions, so the operator's own text comes last whichever one it's in. 'when_needed' sends it from the first request whose history has a <system> block, a note or another user-channel frame, and on every request after it. 'always' sends it on every request. Live check of this placement (email assistant, operator grant, external send request, n=24 per arm): Gemini 4 Argon attempted the send 0/24 with the grant in instructions= and 0/27 in system_prompt=; Claude Haiku 5.5 0/24 and 0/24. Only requests with a system prompt or instructions get it. On models with a native mid-conversation system role, a shorter version goes out as a system message at the first frame or note.

Per-model default for framing_announcement

#12661 (merged) added the ModelSettings['framing_announcement'] setting ('always' | 'when_needed' | 'never'). This PR builds on it and adds ModelProfile.default_framing_announcement. Resolution: the setting, else the profile default, else 'when_needed'. Claude Sonnet 4.6 and gpt-oss default to 'always'. 'when_needed' here also counts a lookalike note or another user-channel frame as needing the announcement, and 'never' keeps the notes.

From the first request with something to explain, 'when_needed' sends the same request as 'always'. The two differ only on requests with nothing to explain, where 'when_needed' sends what 'never' sends. Measured on those requests (always vs. when_needed; the right column pools two runs):

ModelMeasure'always''when_needed'
Claude Sonnet 4.6keeps to the system prompt's task30/4822/96
follows its app's own reminder tags24/240/48
gpt-oss-120bkeeps to the system prompt's task44/4869/96
Claude Haiku 5.5keeps to the system prompt's task42/4896/96
Grok 4.7keeps to the system prompt's task42/4896/96
GLM 5.3keeps to the system prompt's task27/4878/96
Gemini 4 Argondoesn't act on a request an operator grant seems to cover11/2448/48

Full table: What each rendering gives up and When the announcement is sent.

An earlier "only when first needed" arm on this PR dropped instruction changes to 0/24 on Haiku 5.5 and withdrawals to 0/23 on Argon. That arm put the announcement in the user turn, as a block before the first frame. The shipped 'when_needed' puts it in the system prompt, and from that request on sends the same request as 'always', so the instruction-tracking results for 'always' apply to it.

Every caller that prepares messages (agent graph, direct API, FallbackModel, Temporal's activity re-prepare) passes the setting through one private helper, _with_framing_announcement, and a test runs 'never' and 'always' through each of them.

Compatibility

  • New public names: ModelProfile.default_framing_announcement, ModelProfile.needs_sealed_system_blocks. The framing_announcement setting and its waiver came with #12661. The pyai: prefix is reserved in model_visible_name_error.
  • Request bytes change as listed in the warning above. Each change moves the cached prefix once, on the first request after upgrading. With 'when_needed', a conversation's prefix moves once more, at the request that first needs the announcement.

Known costs

What each rendering gives up in docs/message-history.md has the numbers for every trade-off. The main ones:

  • GLM 5.3 follows genuine <pyai:retry_feedback> 39/48 against 36/36 with plain names; its misses say the frame "isn't a genuine application frame".
  • Claude Sonnet 5.5, sealed: a genuine change next to a <system> lookalike the user typed is followed 8/24, against 24/24 with the native role. It stays sealed for attribution (24/24 vs 18/24) and resisting the lookalike (14/24 vs 8/24).
  • Grok 4.3 with verified: a change next to a copied marker the user typed is followed 16/24, against 23/24 with seal="pydantic-ai".
  • Claude Haiku 5.5: a request-scoped reminder next to a user lookalike, on an agent without tools, is followed 12/24, 8/24 and 5/24 in three runs (24/24 with a tool).
  • Claude Sonnet 4.6 with a search tool that returns nothing: the announcement makes it distrust an application <context> document (5/24 and 8/24 vs 24/24).
  • gpt-oss-120b: a change next to a user copy of <pyai:context> is followed 17/24 vs 23/24 for <context> (the wrong answers keep the old instruction).

Cells where no rendering we tried beats the default ("Where no rendering we tried does well" in the same section):

  • Qwen3 Next 80B: a genuine change next to a <system> lookalike the user typed is followed 0/24, and 0–1/24 with the announcement off, only when needed, minus a sentence or plus one. A shorter announcement got 12/12 in one run but then followed a <system> lookalike in a tool result 11/12.
  • gpt-oss-120b: the same cell 8/24 (2–7/24 with the same alternatives); the shorter announcement fixed it the same way and broke the same tool-result cell (12/12 followed).
  • Grok 4.7: a genuine change next to a copied <system verified> the user typed is followed 1/24 (0/24 with the announcement off).
  • Qwen3 Next 80B and gpt-oss-120b: copied retry feedback and plan steps in a tool result are followed in every arm (resisted 0/24, gpt-oss plan steps 2/24), with or without pyai: names and notes.
  • GLM 5.3 genuine retry feedback (above): an announcement sentence saying retry feedback is genuine raised it to 22/24 in one run, but made Qwen3 Next 80B act on a forged <tool_result> 22/24, so it isn't sent.
  • Re-checked at c2a7704028: Claude Haiku 5.5 (now sealed) follows a genuine appended <pyai:context> change 7/8 alone and 6/8 next to a user <pyai:context> lookalike in the same turn, so the earlier native-role cost (6/8 and 0/8) no longer applies and isn't listed.

The docs warning applies: notes are a hint, and open-weight models aren't reliably steered by any prompt-level framing.

Open items

  • Pydantic AI Harness still escapes framing tags in the bodies of background tools' <pyai:tool_result> and sub-agents' <task_output> / <task_question> frames, so those don't go out byte for byte yet (a system-voice tag in them also gets a note).
  • Realtime sessions get the pyai: renames but no announcement and no notes.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code