Re-resolve realtime session instructions before each model turn and deliver their changes as appends
A realtime session resolved its instructions once, at connect. It now resolves them again after each model response, and before a tool's result asks for the next one, as a standard run resolves them before each request. When something changed, it delivers the change mid-session.
[!WARNING]
Compatibility impact: In a realtime session, a dynamic instruction with an id of its own (a named instruction function, or one a capability or toolset with an id contributes, such as the deferred-capability catalog) is now sent inside a <context id="..."> element from connect, as the append-mode prefix of a standard run is, and sorts before dynamic instructions without an id. An appending dynamic system prompt in the standing prompt is sent as its <context> element too. The on_change='append' warnings for sessions are gone.
Why this can ship in a minor release: A session can't rewrite the instructions it opened with, so a change can only be appended, and it has to name the block it replaces. Without the tag the model has nothing to match the change to. Static and unnamed instructions go out exactly as before.
Migration: None needed. To keep an instruction untagged, leave it unnamed. Its changes then aren't delivered mid-session, and the session warns once if it changes.
What a session does now
-
Re-resolution. Instruction functions, capability and toolset instructions, the instructions passed to
realtime(), and dynamic system prompts withon_change='append'are resolved again after each response (in the background, so a slow function never stalls audio) and before each tool result. A text turn,create_response(), or tool result waits for a refresh that is still running, so the reply it asks for gets the change. -
Append only. A session never rewrites its instructions, whatever the block's
on_change. A changed block goes out as a<context>element (or a<context_diff>, or a withdrawal), rendered by the same code a standard model uses (_project_instruction_state,_render_system_prompt_contexts). It travels on the channelsend(SystemPromptPart(...), respond=False)uses:Provider Delivered as OpenAI gpt-realtime, Azure (incl. Voice Live)systemconversation itemxAI, Eleven Realtime <system>-tagged user textGemini Live <system>-taggedclient_content,turn_complete=FalseElevenLabs Agents contextual_update,<system>-taggedGPT-Live session.updateappending the change todelegation.responses.instructions, the backend's instructions. Nothing goes to the voice model. -
History. The change is recorded on a request of its own as the
InstructionDeltaParts (andSystemPromptPartstatements) a standard run records. The session's first change also records anInstructionBaselinePartfor the prefix it opened with. A standard run continuing the history therefore treats the session's changes as superseded and sends its own current instructions; this is tested. A loaded capability's block is left out of that baseline, because a standard run delivers it through history. -
Reconnect. A redial that replays history (no native resumption) replays each change as the system prompt it was sent as, where it was sent, after the connect-time instructions. GPT-Live instead updates the captured backend config in place, so a redial or fork starts with the changes, and nothing is replayed to Live.
-
Unnamed instructions. If an instruction with no id changes, no change can name it. The session warns once and the model keeps the original text. Nothing warns when nothing changed.
Interim shape, ahead of #11030
All of a session's instruction state lives in two private functions in _instructions.py:
session_instruction_partsdecides what is tagged at connect;session_instruction_updatefolds the session's own changes onto the connect prefix and diffs against the current resolution.
The session only calls them and renders the result. Moving onto HistoryState means replacing their bodies.
Removed as dead code with the warnings: explicitly_appended_blocks, framework_appended_toolset_sources, SourcedInstruction.framework_default, and AbstractToolset._appends_instructions_by_default (all private).
Tests
tests/realtime/test_instruction_changes.pyuses the scripted connection. It covers:- tagging at connect;
- delivery on both sides of
supports_system_messages; - folding successive changes;
- withdrawal;
- a change a tool makes going out before its result;
- a text turn waiting for a pending refresh;
- the unaddressable warning;
- appending system prompts, fresh and seeded;
- a loaded capability's block;
- a failing instruction function;
- reconnect replay, and the connection that carries changes itself;
- a standard run continuing the history.
test_instruction_change_parityrecords live WS cassettes for every text-driven parity row: OpenAI Ć2, xAI Ć2, Eleven Realtime, Gemini Ć2, both gateway routes, ElevenLabs Agents. The scenario changes a code word mid-call, and the next answer has to use the new one; every provider got it right live. Azure is skipped like the other recent parity scenarios: its key returns 401.test_instruction_change_updates_the_backendrecords a GPT-Live cassette: the backend'ssession.updateis sent, and the next delegated answer uses the change.
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