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] OpenAI Realtime API integration fails due to websockets.exceptions.PayloadTooBig error (#15751)
Ishaan Jaff0c25b1a25694
diff --git a/litellm/constants.py b/litellm/constants.pyindex 64e92e382f8..175aec73523 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -93,6 +93,11 @@ AIOHTTP_CONNECTOR_LIMIT = int(os.getenv("AIOHTTP_CONNECTOR_LIMIT", 0))AIOHTTP_KEEPALIVE_TIMEOUT = int(os.getenv("AIOHTTP_KEEPALIVE_TIMEOUT", 120))AIOHTTP_TTL_DNS_CACHE = int(os.getenv("AIOHTTP_TTL_DNS_CACHE", 300))+# WebSocket constants+REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES = int(+ os.getenv("REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES", 10 * 1024 * 1024)+) # 10MB default to handle large base64 audio payloads from realtime APIs+# SSL/TLS cipher configuration for faster handshakes# Strategy: Strongly prefer fast modern ciphers, but allow fallback to commonly supported ones# This balances performance with broad compatibilitydiff --git a/litellm/llms/azure/realtime/handler.py b/litellm/llms/azure/realtime/handler.pyindex c5447b4ccd9..23c04e640c4 100644--- a/litellm/llms/azure/realtime/handler.py+++ b/litellm/llms/azure/realtime/handler.py@@ -6,6 +6,8 @@ This requires websockets, and is currently only supported on LiteLLM Proxy.from typing import Any, Optional, cast+from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES+from ....litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingfrom ....litellm_core_utils.realtime_streaming import RealTimeStreamingfrom ..azure import AzureChatCompletion@@ -64,6 +66,7 @@ class AzureOpenAIRealtime(AzureChatCompletion):extra_headers={"api-key": api_key, # type: ignore},+ max_size=REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES,) as backend_ws:realtime_streaming = RealTimeStreaming(websocket, cast(ClientConnection, backend_ws), logging_objdiff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.pyindex d7b7987b670..666dd6bedc1 100644--- a/litellm/llms/custom_httpx/llm_http_handler.py+++ b/litellm/llms/custom_httpx/llm_http_handler.py@@ -20,6 +20,7 @@ import litellm.litellm_core_utilsimport litellm.typesimport litellm.types.utilsfrom litellm._logging import verbose_logger+from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTESfrom litellm.litellm_core_utils.realtime_streaming import RealTimeStreamingfrom litellm.llms.base_llm.anthropic_messages.transformation import (BaseAnthropicMessagesConfig,@@ -3339,7 +3340,9 @@ class BaseLLMHTTPHandler:try:async with websockets.connect( # type: ignore- url, extra_headers=headers+ url,+ extra_headers=headers,+ max_size=REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES,) as backend_ws:realtime_streaming = RealTimeStreaming(websocket,diff --git a/litellm/llms/openai/realtime/handler.py b/litellm/llms/openai/realtime/handler.pyindex e0c85d18178..e1fb3f12602 100644--- a/litellm/llms/openai/realtime/handler.py+++ b/litellm/llms/openai/realtime/handler.py@@ -6,10 +6,12 @@ This requires websockets, and is currently only supported on LiteLLM Proxy.from typing import Any, Optional, cast+from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES+from litellm.types.realtime import RealtimeQueryParams+from ....litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingfrom ....litellm_core_utils.realtime_streaming import RealTimeStreamingfrom ..openai import OpenAIChatCompletion-from litellm.types.realtime import RealtimeQueryParamsclass OpenAIRealtime(OpenAIChatCompletion):@@ -59,6 +61,7 @@ class OpenAIRealtime(OpenAIChatCompletion):"Authorization": f"Bearer {api_key}", # type: ignore"OpenAI-Beta": "realtime=v1",},+ max_size=REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES,) as backend_ws:realtime_streaming = RealTimeStreaming(websocket, cast(ClientConnection, backend_ws), logging_objdiff --git a/litellm/realtime_api/main.py b/litellm/realtime_api/main.pyindex fb38ba3e80b..8978dbca175 100644--- a/litellm/realtime_api/main.py+++ b/litellm/realtime_api/main.py@@ -4,9 +4,11 @@ from typing import Any, Optional, castimport litellmfrom litellm import get_llm_provider+from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTESfrom litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfigfrom litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandlerfrom litellm.secret_managers.main import get_secret_str+from litellm.types.realtime import RealtimeQueryParamsfrom litellm.types.router import GenericLiteLLMParamsfrom litellm.types.utils import LlmProvidersfrom litellm.utils import ProviderConfigManager@@ -15,7 +17,6 @@ from ..litellm_core_utils.get_litellm_params import get_litellm_paramsfrom ..litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingfrom ..llms.azure.realtime.handler import AzureOpenAIRealtimefrom ..llms.openai.realtime.handler import OpenAIRealtime-from litellm.types.realtime import RealtimeQueryParamsfrom ..utils import client as wrapper_clientazure_realtime = AzureOpenAIRealtime()@@ -182,5 +183,6 @@ async def _realtime_health_check(extra_headers={"api-key": api_key, # type: ignore},+ max_size=REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES,):return True