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 73edcd66bd7..146d1bf2da2 100644 --- a/litellm/proxy/auth/model_checks.py +++ b/litellm/proxy/auth/model_checks.py @@ -292,12 +292,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 9f49e632395..934be4c3715 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -793,7 +793,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)) } ) @@ -937,7 +937,7 @@ async def acredential_updates(kwargs: Mapping[str, object], call_type: str | Non def _api_key_after_credentials(kwargs: Mapping[str, object], credential_values: Mapping[str, object]) -> object: - return kwargs.get("api_key", credential_values.get("api_key")) + return credential_values.get("api_key", kwargs.get("api_key")) def _credential_updates( @@ -958,7 +958,7 @@ def _credential_updates( 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 bc9366aa607..910f659754b 100644 --- a/tests/unit/proxy/auth/test_model_checks.py +++ b/tests/unit/proxy/auth/test_model_checks.py @@ -788,6 +788,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 32063f65765..8066cfb9c97 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -7278,6 +7278,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", @@ -8033,7 +8063,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", @@ -8042,7 +8071,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(