Support deferred capability tools in realtime sessions via mid-session `session.update` tool reveals

AD
8d ago
+850-169

What this does

Until now, a realtime session advertised its tools exactly once, when the connection opened. That meant a defer_loading=True capability that contributes tools was rejected with a UserError before connecting: the model could have loaded the capability mid-call, but there was no way to hand it the tools, so it would have silently gotten less than the catalog promised.

OpenAI's Realtime API can lift that: session.update accepts a fresh tools array at any time. This PR uses it, gated on a new realtime profile flag:

  • New profile flag supports_tool_updates — set for OpenAI and Azure OpenAI. Gemini Live fixes its tools in the setup message, so it keeps the up-front error. xAI reuses the OpenAI protocol but its server's behavior for a second tools-bearing session.update is unverified, so it stays off for now.
  • Mid-session reveal — when the model calls load_capability on a supporting provider, the session recomputes the advertised tool list (the same resolution the connect path uses, shared in one helper so they can't drift) and sends it as a new codec input, UpdateTools, in the same locked send group as — and ahead of — the tool result. The model never hears "capability active" before the tools exist server-side.
  • Reconnects keep the revealed tools — the update rewrites the connection's captured session config in place, so a redial re-advertises what the session had revealed, not the connect-time list.
  • Already-loaded capabilities need no update at all — a session seeded with a message_history in which the capability was loaded (the load call/return plus its tool-reveal record) now advertises those tools at connect, on every provider including Gemini. Sessions also seed loaded_capability_ids/discovered_tool_names from history like a classic run, so load_capability now correctly refuses a redundant re-load instead of allowing unbounded ones.
  • History stays interchangeable with a classic run — the session now promotes load_capability calls to LoadCapabilityCallPart and records reveals as ToolAvailabilityDeltaPart, exactly like the graph, so a session's history keeps working as Agent.run(message_history=...) input with the same reveal state, and vice versa.

Example

from pydantic_ai import Agent
from pydantic_ai.capabilities import Capability
from pydantic_ai.toolsets import FunctionToolset


async def check_availability(date: str) -> str: ...


reservations = Capability(
    id='reservations',
    description='Look up and book restaurant tables.',
    toolset=FunctionToolset(tools=[check_availability]),
    defer_loading=True,  # previously: UserError at session open
)

agent = Agent('openai:gpt-realtime', capabilities=[reservations])

async with agent.realtime().session() as session:
    async for event in session:
        ...

The session connects advertising only load_capability (plus any always-on tools). When the caller asks about booking a table, the model loads the reservations capability; the session sends session.update with check_availability included, then answers the load call, and the model can book from the next response on.

Wire flow (OpenAI)

  1. Connect: session.update advertises always-on tools + load_capability; the capability catalog is part of the instructions (unchanged).
  2. Model emits a load_capability tool call.
  3. Session executes it, which reveals the capability's tool names.
  4. Session re-resolves the advertised list and sends session.update with the revealed tools appended — first frame in the send group.
  5. Session sends the tool result (the capability's instructions) and requests a response — same send group, so nothing interleaves.
  6. Next model turn can call the revealed tools; the session's per-turn tool refresh has marked the capability loaded from history, so the calls are accepted.
  7. If the socket drops, the redial's session.update already contains the revealed tools (the captured config was updated in place at step 4).

Deliberately out of scope / flags for review

  • Native tools stay rejected everywhere: no realtime API turns a server-side tool on mid-session, so a deferred capability contributing native tools keeps the up-front error (now with its own accurate message).
  • xAI: needs a live probe of a second tools-bearing session.update before flipping its flag; its connection now receives the session config so a user profile override behaves correctly across redials.
  • Plain defer_loading=True tools (no owning capability) are still rejected at open — they're revealed by tool search, which a session has no surface for — unless the seeded history already revealed them, in which case they advertise at connect like everything else.
  • Question for @DouweM: with capability tools now live in sessions, a developer-error reveal (an ordinary tool returning ToolReturn.tools naming a still-unloaded capability's tool) raises the same UserError as a graph run — but in a session that ends the live conversation. Pinned by a parity test as-is; happy to soften it to a failed tool return in a follow-up if you'd rather not kill a voice call over it.
  • Question for @DouweM (pre-existing, graph parity): resolve_revealed_tool_names filters history-discovered names but never adds a loaded capability's tools, so a history processor that drops the ToolAvailabilityDeltaPart while keeping the load leaves the capability loaded-but-toolless on any provider (graph runs behave identically; execution is resilient, advertisement is not). Worth deciding whether loading should imply the tools — separate PR if so.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.