From 21c8c337f75121ee4f487e242c05e7ee570983cd Mon Sep 17 00:00:00 2001 From: Apricooooot <82204590+Apricooooot@users.noreply.github.com> Date: Tue, 18 Aug 2026 12:50:06 -0700 Subject: [PATCH] fix: use resolved credentials in health checks --- litellm/main.py | 15 ++++++--- .../litellm_utils_tests/test_health_check.py | 32 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index 2a8ed6c87b6..6a0401842f9 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -8385,29 +8385,36 @@ async def ahealth_check( api_base_from_params: Final = model_params.get("api_base", None) api_key_from_params: Final = model_params.get("api_key", None) - model, custom_llm_provider, _, _ = get_llm_provider( + model, custom_llm_provider, dynamic_api_key, dynamic_api_base = get_llm_provider( model=model, custom_llm_provider=custom_llm_provider_from_params, api_base=api_base_from_params, api_key=api_key_from_params, ) + resolved_model_params: Final = dict( # mutable-ok: health-check handlers require mutable request parameters + model_params + ) + if dynamic_api_base is not None: + resolved_model_params["api_base"] = dynamic_api_base + if dynamic_api_key is not None: + resolved_model_params["api_key"] = dynamic_api_key + resolved_model_params["cache"] = {"no-cache": True} if model in litellm.model_cost and mode is None: mode = litellm.model_cost[model].get("mode") - model_params["cache"] = {"no-cache": True} # don't used cached responses for making health check calls mode = mode or "chat" if "*" in model: return await HealthCheckHelpers.ahealth_check_wildcard_models( model=model, custom_llm_provider=custom_llm_provider, - model_params=model_params, + model_params=resolved_model_params, litellm_logging_obj=litellm_logging_obj, ) mode_handlers: Final = HealthCheckHelpers.get_mode_handlers( model=model, custom_llm_provider=custom_llm_provider, - model_params=model_params, + model_params=resolved_model_params, prompt=prompt, input=input, ) diff --git a/tests/litellm_utils_tests/test_health_check.py b/tests/litellm_utils_tests/test_health_check.py index de6f7c38fed..321538c0508 100644 --- a/tests/litellm_utils_tests/test_health_check.py +++ b/tests/litellm_utils_tests/test_health_check.py @@ -15,6 +15,38 @@ import asyncio import litellm +@pytest.mark.asyncio +async def test_hosted_vllm_health_check_uses_resolved_provider_credentials( + monkeypatch, +): + monkeypatch.setenv("HOSTED_VLLM_API_BASE", "https://env.example/v1") + monkeypatch.setenv("HOSTED_VLLM_API_KEY", "env-key") + + mock_response = litellm.ModelResponse( + choices=[{"message": {"role": "assistant", "content": "ok"}}] + ) + with ( + patch( + "litellm.litellm_core_utils.health_check_helpers.HealthCheckHelpers." + "_update_model_params_with_health_check_tracking_information", + side_effect=lambda model_params: model_params, + ), + patch( + "litellm.acompletion", + new_callable=AsyncMock, + return_value=mock_response, + ) as mock_acompletion, + ): + response = await litellm.ahealth_check( + model_params={"model": "hosted_vllm/org/model"}, + mode="chat", + ) + + assert "error" not in response + assert mock_acompletion.await_args.kwargs["api_base"] == "https://env.example/v1" + assert mock_acompletion.await_args.kwargs["api_key"] == "env-key" + + @pytest.mark.asyncio async def test_azure_health_check(): response = await litellm.ahealth_check(