fix(proxy): close websocket cleanly when OpenAI credentials are missing

This commit is contained in:
mateo-berri 2026-08-16 14:40:37 -07:00
parent 4ba9d6b136
commit 5965648547
2 changed files with 31 additions and 2 deletions

View file

@ -2025,8 +2025,11 @@ async def openai_websocket_proxy_route(
region_name=None,
)
if openai_api_key is None:
await websocket.close(code=1011)
raise ValueError("Required 'OPENAI_API_KEY' in environment to make pass-through calls to OpenAI.")
await websocket.close(
code=1011,
reason="Required 'OPENAI_API_KEY' in environment to make pass-through calls to OpenAI.",
)
return
raw_path: Final = httpx.URL(endpoint).path
encoded_endpoint: Final = raw_path if raw_path.startswith("/") else f"/{raw_path}"

View file

@ -94,6 +94,32 @@ async def test_openai_websocket_accepts_first_client_subprotocol():
websocket.close.assert_not_awaited()
@pytest.mark.asyncio
async def test_openai_websocket_closes_cleanly_when_provider_credentials_missing():
websocket = _mock_websocket("/openai/v1/realtime", "model=gpt-4o-realtime-preview")
with (
patch(
"litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.passthrough_endpoint_router.get_credentials",
return_value=None,
),
patch(
"litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints.websocket_passthrough_request",
new_callable=AsyncMock,
) as mock_ws,
):
await openai_websocket_proxy_route(
websocket=websocket,
endpoint="v1/realtime",
user_api_key_dict=UserAPIKeyAuth(),
)
websocket.close.assert_awaited_once()
assert websocket.close.await_args.kwargs["code"] == 1011
websocket.accept.assert_not_awaited()
mock_ws.assert_not_awaited()
@pytest.mark.asyncio
@pytest.mark.parametrize(
"user_api_key_dict",