From 9e123a6eea9c05178d28a56733f8d0b14bc53f80 Mon Sep 17 00:00:00 2001 From: lawrence Date: Tue, 27 Oct 2026 15:10:11 +0800 Subject: [PATCH] fix(credentials): inherit named credentials for blank deployment parameters --- .../litellm_core_utils/credential_accessor.py | 4 ++ litellm/proxy/auth/model_checks.py | 12 +++--- litellm/utils.py | 6 +-- tests/unit/proxy/auth/test_model_checks.py | 40 +++++++++++++++++++ tests/unit/test_utils.py | 32 ++++++++++++++- 5 files changed, 83 insertions(+), 11 deletions(-) diff --git a/litellm/litellm_core_utils/credential_accessor.py b/litellm/litellm_core_utils/credential_accessor.py index b2310894c3a..2ff73816bba 100644 --- a/litellm/litellm_core_utils/credential_accessor.py +++ b/litellm/litellm_core_utils/credential_accessor.py @@ -13,6 +13,10 @@ from litellm.types.utils import ( class CredentialAccessor: + @staticmethod + def is_unset(value: object) -> bool: + return value is None or (isinstance(value, str) and not value.strip()) + @staticmethod def find_credential(credential_name: str) -> CredentialItem | None: return next( diff --git a/litellm/proxy/auth/model_checks.py b/litellm/proxy/auth/model_checks.py index c4c538083ee..41f7530817f 100644 --- a/litellm/proxy/auth/model_checks.py +++ b/litellm/proxy/auth/model_checks.py @@ -266,12 +266,12 @@ def _hydrate_litellm_credential_name( if not credential_values: return litellm_params - litellm_params = litellm_params.model_copy() - for key, value in credential_values.items(): - if key in _CREDENTIAL_LITELLM_PARAM_FIELDS and getattr(litellm_params, key, None) is None: - setattr(litellm_params, key, value) - litellm_params.litellm_credential_name = None - return litellm_params + updates: Final = { + key: value + for key, value in credential_values.items() + if key in _CREDENTIAL_LITELLM_PARAM_FIELDS and CredentialAccessor.is_unset(getattr(litellm_params, key, None)) + } + return litellm_params.model_copy(update={**updates, "litellm_credential_name": None}) def _is_routing_segment( diff --git a/litellm/utils.py b/litellm/utils.py index 781aa5f4fce..98085630429 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -764,7 +764,7 @@ def _named_credential_values(kwargs: Mapping[str, object]) -> Mapping[str, objec { key: value for key, value in credential_api_key_secret_reference(credential.credential_values).items() - if key not in kwargs + if credential_accessor.is_unset(kwargs.get(key)) } ) @@ -831,7 +831,7 @@ def load_credentials_from_list(kwargs: dict[str, object]) -> None: def _credential_updates(kwargs: Mapping[str, object]) -> Mapping[str, object]: credential_values: Final = _named_credential_values(kwargs) - api_key: Final = kwargs.get("api_key", credential_values.get("api_key")) + api_key: Final = credential_values.get("api_key", kwargs.get("api_key")) if not isinstance(api_key, AdminApiKeySecretReference): return credential_values secret: Final = _secret_behind_api_key_reference( @@ -841,7 +841,7 @@ def _credential_updates(kwargs: Mapping[str, object]) -> Mapping[str, object]: upstream=MappingProxyType( { "model": kwargs.get("model"), - **{key: kwargs.get(key, credential_values.get(key)) for key in API_KEY_UPSTREAM_PARAMS}, + **{key: credential_values.get(key, kwargs.get(key)) for key in API_KEY_UPSTREAM_PARAMS}, } ), ) diff --git a/tests/unit/proxy/auth/test_model_checks.py b/tests/unit/proxy/auth/test_model_checks.py index 83761620cc9..80c24f101d0 100644 --- a/tests/unit/proxy/auth/test_model_checks.py +++ b/tests/unit/proxy/auth/test_model_checks.py @@ -518,6 +518,46 @@ def test_wildcard_credential_hydration_preserves_missing_credential_name( } +@pytest.mark.parametrize("unset_value", [None, "", " \t"]) +def test_wildcard_discovery_inherits_blank_params_from_the_named_credential( + monkeypatch: pytest.MonkeyPatch, unset_value: str | None +) -> None: + import litellm + from litellm.proxy.auth.model_checks import get_known_models_from_wildcard + from litellm.types.router import LiteLLM_Params + from litellm.types.utils import CredentialItem + from litellm.utils import AvailableModelsCache + + monkeypatch.setattr(litellm, "check_provider_endpoint", True) + monkeypatch.setattr("litellm.utils._model_cache", AvailableModelsCache()) + monkeypatch.setattr( + litellm, + "credential_list", + [ + CredentialItem( + credential_name="relay", + credential_info={}, + credential_values={"api_base": "https://credential.example/v1", "api_key": "stored-key"}, + ) + ], + ) + params: Final = LiteLLM_Params( + model="openai/*", api_base=unset_value, api_key=unset_value, litellm_credential_name="relay" + ) + original: Final = params.model_dump() + + with respx.mock(assert_all_called=True) as upstream: + upstream.get("https://credential.example/v1/models").respond( + 200, json={"object": "list", "data": [{"id": "credential-test-model", "object": "model"}]} + ) + result: Final = get_known_models_from_wildcard("openai/*", params) + assert result == ["openai/credential-test-model"] + authorization: Final = upstream.calls.last.request.headers["Authorization"] + + assert authorization == "Bearer stored-key" + assert params.model_dump() == original + + def test_hydrate_credential_name_none_leaves_params_untouched(monkeypatch): import litellm from litellm.proxy.auth.model_checks import _hydrate_litellm_credential_name diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 38287b25169..91bfb0c61c2 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -7181,6 +7181,36 @@ def test_load_credentials_from_list_fills_kwargs_from_the_loaded_credential_with assert _credential_warnings(caplog) == [] +@pytest.mark.parametrize("unset_value", [None, "", " \t"]) +@pytest.mark.parametrize("secret_reference", [False, True]) +def test_blank_deployment_params_use_the_named_credentials_endpoint_and_key( + monkeypatch: pytest.MonkeyPatch, unset_value: str | None, secret_reference: bool +) -> None: + monkeypatch.setenv("RELAY_TOKEN", "stored-key") + api_key: Final = "oidc/env/RELAY_TOKEN" if secret_reference else "stored-key" + credential: Final = CredentialItem( + credential_name="relay", + credential_values={"api_base": "https://credential.example/v1", "api_key": api_key}, + credential_info={}, + ) + monkeypatch.setattr(litellm, "credential_list", [credential]) + + with respx.mock(assert_all_called=True) as upstream: + upstream.post("https://credential.example/v1/chat/completions").respond(200, json=_MISTRAL_REPLY) + response: Final = litellm.completion( + model="openai/credential-test-model", + messages=[{"role": "user", "content": "ping"}], + litellm_credential_name="relay", + api_base=unset_value, + api_key=unset_value, + ) + authorization: Final = upstream.calls.last.request.headers["Authorization"] + + assert response.choices[0].message.content == "pong" + assert authorization == "Bearer stored-key" + assert credential.credential_values == {"api_base": "https://credential.example/v1", "api_key": api_key} + + _MISTRAL_CHAT_URL: Final = "https://api.mistral.ai/v1/chat/completions" _MISTRAL_REPLY: Final = { "id": "chatcmpl-oidc", @@ -7569,7 +7599,6 @@ _CALLER_HOST_REFERENCE_REFUSAL: Final = "sends it only to that credential's own ("https://api.mistral.ai/v1", "https://api.mistral.ai@caller.example/v1"), ("https://api.mistral.ai/v1", "https://api.mistral.ai:8443/v1"), ("https://api.mistral.ai/v1", "http://[api.mistral.ai/v1"), - ("https://mistral-gateway.example/v1", None), ], ids=[ "another host", @@ -7578,7 +7607,6 @@ _CALLER_HOST_REFERENCE_REFUSAL: Final = "sends it only to that credential's own "userinfo trick", "another port", "unparseable", - "call clears the api_base", ], ) def test_a_named_credentials_oidc_api_key_never_goes_to_an_api_base_the_credential_does_not_set(