The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
[Fix] Update realtime websocket tests for websockets v15+ API
yuneng-jiang3f0c7b5552d0
diff --git a/tests/llm_translation/realtime/test_openai_realtime.py b/tests/llm_translation/realtime/test_openai_realtime.pyindex 87eeb9b5c97..6b93b21ed6e 100644--- a/tests/llm_translation/realtime/test_openai_realtime.py+++ b/tests/llm_translation/realtime/test_openai_realtime.py@@ -3,6 +3,7 @@ import sysfrom unittest.mock import AsyncMock, MagicMockimport pytest+from websockets.exceptions import ConnectionClosedError, ConnectionClosedOKsys.path.insert(0, os.path.abspath("../..")@@ -26,10 +27,9 @@ async def test_openai_realtime_direct_call_no_intent():Note: This test may be skipped on transient connection failures since it dependson external OpenAI API availability."""- import websocketsimport asyncioimport json-+class RealTimeWebSocketClient:def __init__(self):self.messages_sent = []@@ -39,10 +39,10 @@ async def test_openai_realtime_direct_call_no_intent():self._receive_called = Falseself.close_code = Noneself.close_reason = None-+async def accept(self):pass-+async def send_text(self, message):self.messages_sent.append(message)try:@@ -50,10 +50,10 @@ async def test_openai_realtime_direct_call_no_intent():message_str = message.decode('utf-8')else:message_str = message-+msg_data = json.loads(message_str)msg_type = msg_data.get('type', 'unknown')-+if msg_type == "error":error_info = msg_data.get('error', {})error_code = error_info.get('code', 'unknown')@@ -61,7 +61,7 @@ async def test_openai_realtime_direct_call_no_intent():# Don't fail on error, just record it - some errors are expectedself.messages_received.append(msg_data)return-+if msg_type == "session.created" and not self.received_session_created:self.messages_received.append(msg_data)self.received_session_created = True@@ -69,44 +69,44 @@ async def test_openai_realtime_direct_call_no_intent():except (json.JSONDecodeError, UnicodeDecodeError):# Non-JSON messages are acceptablepass-+async def receive_text(self):if not self._receive_called:self._receive_called = Truemax_wait = 60.0check_interval = 0.1waited = 0.0-+while waited < max_wait:if self.connection_successful:breakawait asyncio.sleep(check_interval)waited += check_interval-+if not self.connection_successful:await asyncio.sleep(3.0)-- raise websockets.exceptions.ConnectionClosed(None, None)-++ raise ConnectionClosedOK(None, None)+async def close(self, code=1000, reason=""):self.close_code = codeself.close_reason = reason-+@propertydef headers(self):return {}websocket_client = RealTimeWebSocketClient()caught_exception = None-+try:await litellm._arealtime(- model="gpt-4o-realtime-preview-2024-10-01",+ model="openai/gpt-4o-realtime-preview-2024-10-01",websocket=websocket_client,api_key=os.environ.get("OPENAI_API_KEY"),timeout=60)- except websockets.exceptions.ConnectionClosed:+ except (ConnectionClosedOK, ConnectionClosedError):passexcept Exception as e:caught_exception = e@@ -153,10 +153,9 @@ async def test_openai_realtime_direct_call_with_intent():Note: This test may be skipped on transient connection failures since it dependson external OpenAI API availability."""- import websocketsimport asyncioimport json-+class RealTimeWebSocketClient:def __init__(self):self.messages_sent = []@@ -186,7 +185,7 @@ async def test_openai_realtime_direct_call_with_intent():error_info = msg_data.get('error', {})error_code = error_info.get('code', 'unknown')error_message = error_info.get('message', 'unknown')-+if error_code == "invalid_intent":self.intent_error_received = {'code': error_code,@@ -203,7 +202,7 @@ async def test_openai_realtime_direct_call_with_intent():except (json.JSONDecodeError, UnicodeDecodeError):# Non-JSON messages are acceptablepass-+async def receive_text(self):if not self._receive_called:self._receive_called = True@@ -220,7 +219,7 @@ async def test_openai_realtime_direct_call_with_intent():if not self.connection_successful:await asyncio.sleep(3.0)- raise websockets.exceptions.ConnectionClosed(None, None)+ raise ConnectionClosedOK(None, None)async def close(self, code=1000, reason=""):self.close_code = code@@ -232,21 +231,21 @@ async def test_openai_realtime_direct_call_with_intent():websocket_client = RealTimeWebSocketClient()caught_exception = None-+query_params: RealtimeQueryParams = {- "model": "gpt-4o-realtime-preview-2024-10-01",+ "model": "openai/gpt-4o-realtime-preview-2024-10-01","intent": "chat"}-+try:await litellm._arealtime(- model="gpt-4o-realtime-preview-2024-10-01",+ model="openai/gpt-4o-realtime-preview-2024-10-01",websocket=websocket_client,api_key=os.environ.get("OPENAI_API_KEY"),query_params=query_params,timeout=60)- except websockets.exceptions.ConnectionClosed:+ except (ConnectionClosedOK, ConnectionClosedError):passexcept Exception as e:caught_exception = e