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(proxy): register WebSocket passthrough for OpenAI prefixes

LHMQ8788d6b8d2ce94b
diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
index 40c49df26cf..f9409ab366f 100644
--- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
+++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
@@ -27,7 +27,7 @@ from litellm.llms.anthropic.common_utils import AnthropicModelInfo
from litellm.llms.vertex_ai.vertex_llm_base import VertexBase
from litellm.proxy._types import *
from litellm.proxy.auth.route_checks import RouteChecks
-from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
+from litellm.proxy.auth.user_api_key_auth import user_api_key_auth, user_api_key_auth_websocket
from litellm.proxy.common_utils.http_parsing_utils import (
_read_request_body,
_safe_get_request_headers,
@@ -1934,6 +1934,51 @@ async def openai_proxy_route(
)
[email protected]("/openai_passthrough/{endpoint:path}")
[email protected]("/openai/{endpoint:path}")
+async def openai_websocket_proxy_route(
+ websocket: WebSocket,
+ endpoint: str,
+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth_websocket),
+):
+ """WebSocket passthrough for OpenAI prefixes (realtime / responses.connect)."""
+ base_target_url = os.getenv("OPENAI_API_BASE") or "https://api.openai.com/"
+ openai_api_key = passthrough_endpoint_router.get_credentials(
+ custom_llm_provider=litellm.LlmProviders.OPENAI.value,
+ region_name=None,
+ )
+ if openai_api_key is None:
+ await websocket.close(code=1011)
+ raise Exception("Required 'OPENAI_API_KEY' in environment to make pass-through calls to OpenAI.")
+
+ encoded_endpoint = httpx.URL(endpoint).path
+ if not encoded_endpoint.startswith("/"):
+ encoded_endpoint = "/" + encoded_endpoint
+ base_url = httpx.URL(base_target_url)
+ updated_url = BaseOpenAIPassThroughHandler._join_url_paths(
+ base_url=base_url,
+ path=encoded_endpoint,
+ custom_llm_provider=litellm.LlmProviders.OPENAI,
+ )
+ # HTTP(S) base -> WS(S) target for the upgrade.
+ if updated_url.startswith("https://"):
+ wss_target = "wss://" + updated_url[len("https://") :]
+ elif updated_url.startswith("http://"):
+ wss_target = "ws://" + updated_url[len("http://") :]
+ else:
+ wss_target = updated_url
+
+ return await websocket_passthrough_request(
+ websocket=websocket,
+ target=wss_target,
+ custom_headers={"Authorization": f"Bearer {openai_api_key}"},
+ user_api_key_dict=user_api_key_dict,
+ forward_headers=True,
+ endpoint=f"/openai/{endpoint}",
+ accept_websocket=True,
+ )
+
+
class BaseOpenAIPassThroughHandler:
@staticmethod
async def _base_openai_pass_through_handler(
diff --git a/tests/test_litellm/proxy/test_openai_ws_passthrough_routes.py b/tests/test_litellm/proxy/test_openai_ws_passthrough_routes.py
new file mode 100644
index 00000000000..e0184c6c428
--- /dev/null
+++ b/tests/test_litellm/proxy/test_openai_ws_passthrough_routes.py
@@ -0,0 +1,15 @@
+"""OpenAI passthrough must register WebSocket catch-all routes (#36088)."""
+
+from starlette.routing import WebSocketRoute
+
+from litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints import router
+
+
+def test_openai_websocket_passthrough_routes_registered():
+ ws_paths = {
+ route.path
+ for route in router.routes
+ if isinstance(route, WebSocketRoute)
+ }
+ assert "/openai/{endpoint:path}" in ws_paths
+ assert "/openai_passthrough/{endpoint:path}" in ws_paths