Add persistent Responses WebSocket connections with `Agent.connect()`

Merged
DS
2mo ago
+3974-72

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Repeated Responses requests pay HTTP transport overhead during tool-heavy runs. This adds opt-in persistent WebSocket connections for ordinary Responses runs and keeps the existing HTTP path unchanged. See issue #4414.

New public surface

  • AbstractAgent.connect(): inside the context, runs that select no model of their own use a connection to the agent's configured model. A run's model=, a run spec model, and a run capability model are honored as usual. An override(model=...) entered inside the context still wins; under an active override, connect() connects the overriding model. When the configured model is a model ID, the context owns the temporary model's provider client. It raises UserError inside a durable workflow or flow, on TemporalAgent, DBOSAgent and PrefectAgent, and on agents without connection support.
  • Model.connect() takes no arguments; models without connection support raise UserError. OpenAIResponsesModel.connect() yields an independent connected model and accepts typed SDK WebSocket options and handshake headers. Those options stay on the concrete model's typed signature rather than passing through Model.connect() or agent.connect() as untyped **kwargs: connect the model directly, then pass or wrap the connected model.
  • openai_previous_response_id accepts None, which clears an inherited 'auto' value for full-history recovery.

User-visible behavior

async with agent.connect():
    await agent.run('What is the capital of France?')  # over the socket
    await agent.run('Hi', model='openai-responses:gpt-5.2')  # honored, over HTTP

Each connection serves one response at a time; parallel runs open separate contexts. Cancelling or abandoning a stream closes the socket. There is no automatic retry or HTTP fallback. Each socket keeps the credentials it opened with, and request headers are compared only with the connection's configured headers, never the client's credentials, so a shared client refreshing its token doesn't affect open connections. Codex connections are rejected before a handshake. Install with pydantic-ai-slim[openai-realtime]. Ultrafast support is tracked in #9469.

Verification

Other regression coverage

Out of scope

  • Native steering, several responses multiplexed on one socket, and connection pooling. A connection serves one response at a time, so cancellation and incomplete-stream cleanup have one socket owner. Parallel runs open separate contexts.
  • Automatic reconnect, retry, or HTTP fallback.
  • Codex subscription connections, whose session headers are only known per request.
  • Handshake response headers and response.metadata events: #10237.
  • Structured code, type and param on in-stream errors, for both transports: #10279.

What changes for existing users

Nothing, unless they enter a connection.

Checklist

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