Re-resolve realtime session instructions before each model turn and deliver their changes as appends

Merged
DO
1mo ago
+10516-164

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 with on_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 channel send(SystemPromptPart(...), respond=False) uses:

    ProviderDelivered as
    OpenAI gpt-realtime, Azure (incl. Voice Live)system conversation item
    xAI, Eleven Realtime<system>-tagged user text
    Gemini Live<system>-tagged client_content, turn_complete=False
    ElevenLabs Agentscontextual_update, <system>-tagged
    GPT-Livesession.update appending the change to delegation.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 (and SystemPromptPart statements) a standard run records. The session's first change also records an InstructionBaselinePart for 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_parts decides what is tagged at connect;
  • session_instruction_update folds 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.py uses 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_parity records 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_backend records a GPT-Live cassette: the backend's session.update is 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