mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-10 03:28:53 +00:00
fix(proxy): close websocket cleanly when OpenAI credentials are missing
This commit is contained in:
parent
4ba9d6b136
commit
5965648547
2 changed files with 31 additions and 2 deletions
|
|
@ -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}"
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue