Support deferred capability tools in realtime sessions via mid-session `session.update` tool reveals
- Closes #7191
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 thesetupmessage, so it keeps the up-front error. xAI reuses the OpenAI protocol but its server's behavior for a second tools-bearingsession.updateis unverified, so it stays off for now. - Mid-session reveal — when the model calls
load_capabilityon 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_historyin 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 seedloaded_capability_ids/discovered_tool_namesfrom history like a classic run, soload_capabilitynow correctly refuses a redundant re-load instead of allowing unbounded ones. - History stays interchangeable with a classic run — the session now promotes
load_capabilitycalls toLoadCapabilityCallPartand records reveals asToolAvailabilityDeltaPart, exactly like the graph, so a session's history keeps working asAgent.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)
- Connect:
session.updateadvertises always-on tools +load_capability; the capability catalog is part of the instructions (unchanged). - Model emits a
load_capabilitytool call. - Session executes it, which reveals the capability's tool names.
- Session re-resolves the advertised list and sends
session.updatewith the revealed tools appended — first frame in the send group. - Session sends the tool result (the capability's instructions) and requests a response — same send group, so nothing interleaves.
- 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.
- If the socket drops, the redial's
session.updatealready 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.updatebefore flipping its flag; its connection now receives the session config so a user profile override behaves correctly across redials. - Plain
defer_loading=Truetools (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.toolsnaming a still-unloaded capability's tool) raises the sameUserErroras 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_namesfilters history-discovered names but never adds a loaded capability's tools, so a history processor that drops theToolAvailabilityDeltaPartwhile 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.