Implementation:Openai Openai python Websocket Connection Options
| Knowledge Sources | |
|---|---|
| Domains | API_Types, Python |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for configuring WebSocket connection options when using the OpenAI Realtime API, provided by the openai-python SDK.
Description
WebsocketConnectionOptions is a TypedDict that exposes WebSocket connection configuration options sourced from the websockets library. It allows fine-tuning of connection parameters including extensions (negotiated WebSocket extensions), subprotocols (preferred subprotocols), compression (permessage-deflate control), max_size (incoming message size limit), max_queue (receive buffer high-water mark), and write_limit (write buffer high-water mark). These options are passed through to the underlying websockets.asyncio.client.connect call.
Usage
Import this type when you need to customize the WebSocket connection parameters for the OpenAI Realtime API, particularly for performance tuning, protocol negotiation, or disabling compression.
Code Reference
Source Location
- Repository: openai-python
- File: src/openai/types/websocket_connection_options.py
Signature
class WebsocketConnectionOptions(TypedDict, total=False):
"""Websocket connection options copied from websockets."""
extensions: Sequence[ClientExtensionFactory] | None
subprotocols: Sequence[Subprotocol] | None
compression: str | None
max_size: int | None
max_queue: int | None | tuple[int | None, int | None]
write_limit: int | tuple[int, int | None]
Import
from openai.types import WebsocketConnectionOptions
I/O Contract
Fields
| Name | Type | Required | Description |
|---|---|---|---|
| extensions | Sequence[ClientExtensionFactory] or None | No | List of supported extensions, in order of negotiation and execution. |
| subprotocols | Sequence[Subprotocol] or None | No | List of supported subprotocols, in order of decreasing preference. |
| compression | str or None | No | Compression setting. Set to None to disable permessage-deflate (enabled by default). |
| max_size | int or None | No | Maximum size of incoming messages in bytes. None disables the limit. |
| max_queue | int or None or tuple[int or None, int or None] | No | High-water mark of the buffer where frames are received. Defaults to 16 frames. |
| write_limit | int or tuple[int, int or None] | No | High-water mark of write buffer in bytes. Defaults to 32 KiB. |
Usage Examples
from openai.types import WebsocketConnectionOptions
ws_options: WebsocketConnectionOptions = {
"compression": None, # Disable compression
"max_size": 10 * 1024 * 1024, # 10 MB max message size
"max_queue": 32, # 32 frames buffer
}
# Pass to Realtime API connection
# client.beta.realtime.connect(model="gpt-4o-realtime", **ws_options)