Skip to content

Commit 83ed269

Browse files
refactor(websocket): add SDK-owned Responses policy helpers (#4219)
Add private SDK-owned helpers for Responses WebSocket setup, iteration, progress tracking, and cleanup. Focused sync and async tests cover prepared headers, argument collisions, generator controls, retry progress, and cleanup error precedence. Existing public managers do not yet call these helpers. Rollout needs three steps: merge this helper change into public main, land compiler delegation, then regenerate and promote the bindings. The compiler must wait for the merged helper change; an open PR is insufficient. The first dependency needs no package release. Validation: 585 focused WebSocket tests passed, including the 18 new helper cases. Ruff formatting and lint, plus ty 0.0.87 checks for both changed files, passed. --------- Co-authored-by: markstuart-oai <[email protected]>
1 parent 0dcd904 commit 83ed269

2 files changed

Lines changed: 372 additions & 1 deletion

File tree

‎src/openai/_websocket_connection.py‎

Lines changed: 214 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,191 @@
11
from __future__ import annotations
22

3+
import json
34
import time
45
import random
56
import logging
67
import threading
78
import contextlib
8-
from typing import TYPE_CHECKING, Callable, Awaitable
9+
from typing import (
10+
TYPE_CHECKING,
11+
Any,
12+
Mapping,
13+
Callable,
14+
Iterator,
15+
Awaitable,
16+
Generator,
17+
AsyncIterator,
18+
AsyncGenerator,
19+
cast,
20+
)
921

1022
import anyio
1123

1224
from ._types import Query, Headers
25+
from ._utils import is_given
26+
from ._models import FinalRequestOptions
27+
from ._exceptions import OpenAIError, WebSocketConnectionClosedError
1328
from ._send_queue import SendQueue, SendReceipt
29+
from ._base_client import _merge_mappings
1430
from .types.websocket_reconnection import ReconnectingEvent, ReconnectingOverrides, is_recoverable_close
1531

1632
if TYPE_CHECKING:
33+
import httpx2
1734
from websockets.sync.client import ClientConnection as WebSocketConnection
1835
from websockets.asyncio.client import ClientConnection as AsyncWebSocketConnection
1936

37+
from ._client import OpenAI, AsyncOpenAI
38+
39+
40+
def _websocket_request_options(
41+
client: OpenAI | AsyncOpenAI, url: httpx2.URL, extra_query: Query, extra_headers: Headers
42+
) -> FinalRequestOptions:
43+
try:
44+
query_params = client._websocket_query_params
45+
except AttributeError:
46+
query_params = client._websocket_query_params_fallback
47+
url = url.copy_with(params=query_params(url, extra_query))
48+
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
49+
return FinalRequestOptions.construct(
50+
method="get", url=str(url), headers=dict(extra_headers), security={"bearer_auth": True}
51+
)
52+
53+
54+
def _websocket_connect_options(
55+
client: OpenAI | AsyncOpenAI,
56+
options: FinalRequestOptions,
57+
connection_options: Mapping[str, Any],
58+
default_headers: Headers,
59+
logger: logging.Logger,
60+
) -> tuple[str, dict[str, str], dict[str, Any]]:
61+
url = client._prepare_url(options.url).copy_merge_params(client.qs.stringify(cast(Any, options.params)))
62+
url = url.copy_with(scheme={"http": "ws", "https": "wss"}.get(url.scheme, url.scheme))
63+
headers = {
64+
key.lower(): (key, value)
65+
for header_set in (
66+
client.auth_headers,
67+
default_headers,
68+
client.default_headers,
69+
options.headers if is_given(options.headers) else {},
70+
)
71+
for key, value in header_set.items()
72+
}
73+
logger.debug("Connecting to WebSocket API")
74+
if connection_options:
75+
logger.debug("Custom WebSocket connection options provided")
76+
websocket_options = dict(connection_options)
77+
websocket_options.setdefault("max_size", None)
78+
if websocket_options.get("max_size") is None:
79+
websocket_options.setdefault("compression", None)
80+
return str(url), _merge_mappings(dict(headers.values()), {}), websocket_options
81+
82+
83+
def _connect_websocket(
84+
client: OpenAI,
85+
prepare_url: Callable[[], httpx2.URL],
86+
extra_query: Query,
87+
extra_headers: Headers,
88+
connection_options: Mapping[str, Any],
89+
default_headers: Headers,
90+
logger: logging.Logger,
91+
) -> WebSocketConnection:
92+
try:
93+
from websockets.sync.client import connect
94+
except ImportError as exc:
95+
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc
96+
options = client._prepare_options(_websocket_request_options(client, prepare_url(), extra_query, extra_headers))
97+
url, headers, websocket_options = _websocket_connect_options(
98+
client, options, connection_options, default_headers, logger
99+
)
100+
return connect(url, user_agent_header=None, additional_headers=headers, **websocket_options)
101+
102+
103+
async def _async_connect_websocket(
104+
client: AsyncOpenAI,
105+
prepare_url: Callable[[], httpx2.URL],
106+
extra_query: Query,
107+
extra_headers: Headers,
108+
connection_options: Mapping[str, Any],
109+
default_headers: Headers,
110+
logger: logging.Logger,
111+
) -> AsyncWebSocketConnection:
112+
try:
113+
from .lib._websocket import _WebSocketConnect as connect
114+
except ImportError as exc:
115+
raise OpenAIError("You need to install `openai[realtime]` to use this method") from exc
116+
options = await client._prepare_options(
117+
_websocket_request_options(client, prepare_url(), extra_query, extra_headers)
118+
)
119+
url, headers, websocket_options = _websocket_connect_options(
120+
client, options, connection_options, default_headers, logger
121+
)
122+
return await connect(url, user_agent_header=None, additional_headers=headers, **websocket_options)
123+
124+
125+
def _record_websocket_progress(
126+
connection: WebSocketConnectionBase | AsyncWebSocketConnectionBase, event: object, *, raw: bool = False
127+
) -> None:
128+
if raw:
129+
if not connection._reconnect_attempt:
130+
return
131+
try:
132+
event = json.loads(cast("str | bytes", event))
133+
except (ValueError, RecursionError):
134+
return
135+
event_type = event.get("type") if isinstance(event, dict) else None if raw else getattr(event, "type", None)
136+
if isinstance(event_type, str) and event_type and event_type != "error":
137+
connection._reconnect_attempt = 0
138+
139+
140+
@contextlib.contextmanager
141+
def _websocket_entry_guard(
142+
connection: WebSocketConnection, clear_connection: Callable[[], None]
143+
) -> Generator[None, None, None]:
144+
try:
145+
yield
146+
except BaseException:
147+
clear_connection()
148+
try:
149+
connection.close()
150+
except BaseException:
151+
# Preserve the exception that prevented successful entry.
152+
pass
153+
raise
154+
155+
156+
def _exit_websocket(connection: WebSocketConnectionBase | None, exc: BaseException | None) -> None:
157+
if connection is not None:
158+
try:
159+
connection.close()
160+
except Exception:
161+
if exc is None:
162+
raise
163+
164+
165+
async def _async_exit_websocket(connection: AsyncWebSocketConnectionBase | None, exc: BaseException | None) -> None:
166+
if connection is not None:
167+
try:
168+
await connection.close()
169+
except Exception:
170+
if exc is None:
171+
raise
172+
173+
174+
@contextlib.asynccontextmanager
175+
async def _async_websocket_entry_guard(
176+
connection: AsyncWebSocketConnection, clear_connection: Callable[[], None]
177+
) -> AsyncGenerator[None, None]:
178+
try:
179+
yield
180+
except BaseException:
181+
clear_connection()
182+
try:
183+
await connection.close()
184+
except BaseException:
185+
# Preserve the exception that prevented successful entry.
186+
pass
187+
raise
188+
20189

21190
class WebSocketConnectionBase:
22191
def __init__(
@@ -52,6 +221,28 @@ def __init__(
52221
if interruptible_reconnect:
53222
self._close_event = threading.Event()
54223

224+
def _iter_events(self) -> Iterator[Any]:
225+
from websockets.exceptions import ConnectionClosed, ConnectionClosedOK, ConnectionClosedError
226+
227+
while True:
228+
try:
229+
yield cast(Any, self).recv()
230+
except ConnectionClosed as exc:
231+
reconnect_exc = (
232+
ConnectionClosedError(exc.rcvd, exc.sent, exc.rcvd_then_sent)
233+
if isinstance(exc, ConnectionClosedOK)
234+
else exc
235+
)
236+
if not self._reconnect(reconnect_exc):
237+
unsent = self._send_queue.drain()
238+
if unsent:
239+
raise WebSocketConnectionClosedError(
240+
"WebSocket connection closed with unsent messages", unsent_messages=unsent
241+
) from exc
242+
if isinstance(exc, ConnectionClosedOK):
243+
return
244+
raise
245+
55246
def send_raw(self, data: bytes | str) -> None:
56247
self._send(data)
57248

@@ -258,6 +449,28 @@ def __init__(
258449
self._send_queue = send_queue if send_queue is not None else SendQueue()
259450
self._logger = logger
260451

452+
async def _iter_events(self) -> AsyncIterator[Any]:
453+
from websockets.exceptions import ConnectionClosed, ConnectionClosedOK, ConnectionClosedError
454+
455+
while True:
456+
try:
457+
yield await cast(Any, self).recv()
458+
except ConnectionClosed as exc:
459+
reconnect_exc = (
460+
ConnectionClosedError(exc.rcvd, exc.sent, exc.rcvd_then_sent)
461+
if isinstance(exc, ConnectionClosedOK)
462+
else exc
463+
)
464+
if not await self._reconnect(reconnect_exc):
465+
unsent = self._send_queue.drain()
466+
if unsent:
467+
raise WebSocketConnectionClosedError(
468+
"WebSocket connection closed with unsent messages", unsent_messages=unsent
469+
) from exc
470+
if isinstance(exc, ConnectionClosedOK):
471+
return
472+
raise
473+
261474
async def send_raw(self, data: bytes | str) -> None:
262475
await self._send(data)
263476

0 commit comments

Comments
 (0)