mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(vllm): treat an empty key as no key so pass-through sends no empty bearer
This commit is contained in:
parent
d573b95d33
commit
d5e3ade93f
3 changed files with 49 additions and 1 deletions
|
|
@ -49,7 +49,7 @@ class VLLMModelInfo(BaseLLMModelInfo):
|
|||
api_base: str | None = None,
|
||||
) -> dict:
|
||||
upstream_key: Final = self.upstream_api_key(api_key, api_base)
|
||||
return headers if upstream_key is None else {**headers, **self.upstream_auth_headers(upstream_key)}
|
||||
return {**headers, **self.upstream_auth_headers(upstream_key)} if upstream_key else headers
|
||||
|
||||
def upstream_api_key(self, api_key: str | None, api_base: str | None) -> str | None:
|
||||
return api_key
|
||||
|
|
|
|||
|
|
@ -142,6 +142,28 @@ async def test_allm_passthrough_route_keyless_hosted_vllm_without_api_base_sends
|
|||
assert route.calls[0].request.headers["authorization"] == expected_authorization
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_allm_passthrough_route_empty_vllm_api_key_sends_no_authorization_header(
|
||||
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("VLLM_API_BASE", _API_BASE)
|
||||
monkeypatch.setenv("VLLM_API_KEY", "")
|
||||
monkeypatch.delenv("HOSTED_VLLM_API_BASE", raising=False)
|
||||
monkeypatch.delenv("HOSTED_VLLM_API_KEY", raising=False)
|
||||
route: Final = respx_mock.post(f"{_API_BASE}/v1/chat/completions").mock(
|
||||
return_value=httpx.Response(200, json={"ok": True})
|
||||
)
|
||||
response: Final = await allm_passthrough_route(
|
||||
method="POST",
|
||||
endpoint="v1/chat/completions",
|
||||
model="hosted_vllm/my-deployment",
|
||||
json={"model": "anything", "messages": [{"role": "user", "content": "Hello"}]},
|
||||
client=AsyncHTTPHandler(),
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert "authorization" not in route.calls[0].request.headers
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("api_base", "api_key", "expected_authorization"),
|
||||
[
|
||||
|
|
|
|||
|
|
@ -2793,6 +2793,32 @@ def test_vllm_provider_route_reaches_v1_paths_with_its_key_as_a_bearer(
|
|||
assert route.calls.last.request.headers["x-api-key"] == "sk-vllm-upstream"
|
||||
|
||||
|
||||
def test_vllm_provider_route_with_an_empty_key_sends_no_credential_headers(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
from litellm.proxy import proxy_server
|
||||
|
||||
monkeypatch.setenv("VLLM_API_BASE", "http://vllm-upstream.test:8090")
|
||||
monkeypatch.setenv("VLLM_API_KEY", "")
|
||||
monkeypatch.setattr(proxy_server, "llm_router", None)
|
||||
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
||||
litellm.in_memory_llm_clients_cache.flush_cache()
|
||||
monkeypatch.setitem(
|
||||
proxy_server.app.dependency_overrides, user_api_key_auth, lambda: UserAPIKeyAuth(api_key="sk-virtual")
|
||||
)
|
||||
|
||||
with respx.mock(assert_all_called=True) as upstream:
|
||||
route: Final = upstream.post("http://vllm-upstream.test:8090/v1/chat/completions").mock(
|
||||
return_value=httpx.Response(200, json={"id": "chatcmpl-1"})
|
||||
)
|
||||
response: Final = TestClient(proxy_server.app).post(
|
||||
"/vllm/v1/chat/completions",
|
||||
json={"model": "Qwen/Qwen3-0.6B", "messages": [{"role": "user", "content": "hi"}]},
|
||||
)
|
||||
|
||||
assert (response.status_code, response.json()) == (200, {"id": "chatcmpl-1"})
|
||||
assert "authorization" not in route.calls.last.request.headers
|
||||
assert "x-api-key" not in route.calls.last.request.headers
|
||||
|
||||
|
||||
class TestGigachatProxyRoute:
|
||||
@pytest.mark.asyncio
|
||||
@patch( # test-quality-ok: patching litellm internal for unit test isolation
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue