{"record":{"id":"7ee43b3f1e4716c6","repo":"openai/openai-python","slug":"you-need-to-install-openai-realtime-to-use-this","errorCode":null,"errorMessage":"You need to install `openai[realtime]` to use this method","messagePattern":"You need to install `openai\\[realtime\\]` to use this method","errorType":"exception","errorClass":"OpenAIError","httpStatus":null,"severity":"error","filePath":"src/openai/resources/beta/realtime/realtime.py","lineNumber":357,"sourceCode":"        self.__websocket_connection_options = websocket_connection_options\n\n    async def __aenter__(self) -> AsyncRealtimeConnection:\n        \"\"\"\n        👋 If your application doesn't work well with the context manager approach then you\n        can call this method directly to initiate a connection.\n\n        **Warning**: You must remember to close the connection with `.close()`.\n\n        ```py\n        connection = await client.beta.realtime.connect(...).enter()\n        # ...\n        await connection.close()\n        ```\n        \"\"\"\n        try:\n            from ....lib._websocket import _WebSocketConnect as connect\n        except ImportError as exc:\n            raise OpenAIError(\"You need to install `openai[realtime]` to use this method\") from exc\n\n        extra_query = self.__extra_query\n        await self.__client._refresh_api_key()\n        auth_headers = self.__client.auth_headers\n        if is_async_azure_client(self.__client):\n            from ....lib._azure_websocket import _AzureWebSocketConnect as connect\n\n            url, auth_headers = await self.__client._configure_realtime(self.__model, extra_query)\n        else:\n            url = self._prepare_url().copy_with(\n                params={\n                    **self.__client.base_url.params,\n                    \"model\": self.__model,\n                    **extra_query,\n                },\n            )\n        log.debug(\"Connecting to WebSocket API\")\n        if self.__websocket_connection_options:","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/resources/beta/realtime/realtime.py#L339-L375","documentation":"The async Realtime session `__aenter__` imports the websocket helper from `openai.lib._websocket`, which depends on the optional `websockets` package. If the `[realtime]` extra is not installed, the ImportError is re-raised as OpenAIError telling you to install `openai[realtime]`.","triggerScenarios":"Using `async with client.beta.realtime.connect(model=...) as rt:` without the websockets dependency installed (realtime.py:357).","commonSituations":"Installing plain `openai` instead of `openai[realtime]`; slim Docker/CI images that strip optional deps; upgrading the SDK without reinstalling extras.","solutions":["Install the extra: `pip install 'openai[realtime]'` (or `uv add 'openai[realtime]'`)","Verify `python -c \"import websockets\"` succeeds in the runtime environment","Rebuild deployment images to include extras and pin versions"],"exampleFix":"# before\npip install openai\n# after\npip install 'openai[realtime]'","handlingStrategy":"type-guard","validationCode":"try:\n    from websockets.asyncio.client import connect  # noqa: F401\n    realtime_ok = True\nexcept ImportError:\n    realtime_ok = False","typeGuard":"import importlib.util\n\ndef realtime_available() -> bool:\n    return importlib.util.find_spec(\"websockets\") is not None","tryCatchPattern":"from openai import OpenAIError\ntry:\n    async with client.beta.realtime.connect(model=\"gpt-4o-realtime-preview\") as rt:\n        ...\nexcept OpenAIError as e:\n    if \"openai[realtime]\" in str(e):\n        raise RuntimeError(\"Install extra: pip install 'openai[realtime]'\") from e\n    raise","preventionTips":["Install openai[realtime] in all environments using realtime","Declare the extra in pyproject/requirements","Add a startup check importing websockets before entering realtime sessions"],"tags":["python","realtime","missing-dependency","optional-extra","websocket"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}