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(responses-ws): append ?model= to backend WebSocket URL

joereyna7f87c1f7fbf0
diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py
index 4c9abaad908..4e5b7a3410b 100644
--- a/litellm/llms/custom_httpx/llm_http_handler.py
+++ b/litellm/llms/custom_httpx/llm_http_handler.py
@@ -5027,6 +5027,10 @@ class BaseLLMHTTPHandler:
litellm_params={},
)
ws_url = http_url.replace("https://", "wss://").replace("http://", "ws://")
+ # OpenAI's WebSocket responses endpoint requires ?model= in the URL,
+ # matching the Realtime API convention (wss://.../v1/realtime?model=...).
+ if "?" not in ws_url:
+ ws_url = f"{ws_url}?model={model}"
try:
ssl_context = get_shared_realtime_ssl_context()
diff --git a/tests/test_litellm/responses/test_responses_websocket_all_providers.py b/tests/test_litellm/responses/test_responses_websocket_all_providers.py
index 0d83b9f88de..09bde86593e 100644
--- a/tests/test_litellm/responses/test_responses_websocket_all_providers.py
+++ b/tests/test_litellm/responses/test_responses_websocket_all_providers.py
@@ -971,3 +971,35 @@ class TestWebSocketChunkTypes:
)
assert len(messages) == 1
assert messages[0]["content"][0]["text"] == "Part 1Part 2"
+
+
+class TestNativeWebSocketUrlConstruction:
+ """Test that native WebSocket URLs include the model query parameter."""
+
+ def test_openai_ws_url_includes_model(self):
+ """ws_url for OpenAI native WebSocket must include ?model= so OpenAI
+ knows which model to use before the first response.create event."""
+ config = OpenAIResponsesAPIConfig()
+ http_url = config.get_complete_url(api_base=None, litellm_params={})
+ base_ws_url = http_url.replace("https://", "wss://").replace("http://", "ws://")
+
+ # get_complete_url should not include query params
+ assert "?" not in base_ws_url
+
+ # The handler appends ?model= when none is present
+ model = "gpt-4o-mini"
+ ws_url = f"{base_ws_url}?model={model}" if "?" not in base_ws_url else base_ws_url
+ assert ws_url == "wss://api.openai.com/v1/responses?model=gpt-4o-mini"
+
+ def test_ws_url_model_not_duplicated_if_query_already_present(self):
+ """If api_base already has query params, the ?model= should not be appended."""
+ http_url = "https://custom.example.com/v1/responses?api-version=2024-05-01"
+ ws_url = http_url.replace("https://", "wss://").replace("http://", "ws://")
+
+ # Fix: only append when no query string present
+ if "?" not in ws_url:
+ ws_url = f"{ws_url}?model=gpt-4o"
+
+ assert "api-version=2024-05-01" in ws_url
+ assert ws_url.count("?") == 1
+ assert "model=gpt-4o" not in ws_url