Skip to content

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]

[Fix] OpenAI Realtime API integration fails due to websockets.exceptions.PayloadTooBig error (#15751)

Ishaan Jaff0c25b1a25694
diff --git a/litellm/constants.py b/litellm/constants.py
index 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 compatibility
diff --git a/litellm/llms/azure/realtime/handler.py b/litellm/llms/azure/realtime/handler.py
index 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 LiteLLMLogging
from ....litellm_core_utils.realtime_streaming import RealTimeStreaming
from ..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_obj
diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py
index 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_utils
import litellm.types
import litellm.types.utils
from litellm._logging import verbose_logger
+from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES
from litellm.litellm_core_utils.realtime_streaming import RealTimeStreaming
from 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.py
index 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 LiteLLMLogging
from ....litellm_core_utils.realtime_streaming import RealTimeStreaming
from ..openai import OpenAIChatCompletion
-from litellm.types.realtime import RealtimeQueryParams
class 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_obj
diff --git a/litellm/realtime_api/main.py b/litellm/realtime_api/main.py
index fb38ba3e80b..8978dbca175 100644
--- a/litellm/realtime_api/main.py
+++ b/litellm/realtime_api/main.py
@@ -4,9 +4,11 @@ from typing import Any, Optional, cast
import litellm
from litellm import get_llm_provider
+from litellm.constants import REALTIME_WEBSOCKET_MAX_MESSAGE_SIZE_BYTES
from litellm.llms.base_llm.realtime.transformation import BaseRealtimeConfig
from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler
from litellm.secret_managers.main import get_secret_str
+from litellm.types.realtime import RealtimeQueryParams
from litellm.types.router import GenericLiteLLMParams
from litellm.types.utils import LlmProviders
from litellm.utils import ProviderConfigManager
@@ -15,7 +17,6 @@ from ..litellm_core_utils.get_litellm_params import get_litellm_params
from ..litellm_core_utils.litellm_logging import Logging as LiteLLMLogging
from ..llms.azure.realtime.handler import AzureOpenAIRealtime
from ..llms.openai.realtime.handler import OpenAIRealtime
-from litellm.types.realtime import RealtimeQueryParams
from ..utils import client as wrapper_client
azure_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