Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
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,
Expand Down Expand Up @@ -1934,6 +1934,51 @@ async def openai_proxy_route(
)


@router.websocket("/openai_passthrough/{endpoint:path}")
Comment thread
mateo-berri marked this conversation as resolved.
Resolved
@router.websocket("/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
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
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.
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
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(
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
websocket=websocket,
target=wss_target,
custom_headers={"Authorization": f"Bearer {openai_api_key}"},
user_api_key_dict=user_api_key_dict,
forward_headers=True,
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
endpoint=f"/openai/{endpoint}",
accept_websocket=True,
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved


class BaseOpenAIPassThroughHandler:
@staticmethod
async def _base_openai_pass_through_handler(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_litellm/proxy/test_openai_ws_passthrough_routes.py
Original file line number Diff line number Diff line change
@@ -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
Loading