mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
Merge 22d86e3251 into d5ca88900c
This commit is contained in:
commit
e9a0506e3a
5 changed files with 83 additions and 11 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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},
|
||||
}
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue