mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
fix(anthropic): keep an azure deployment's Entra credential over the env key on bridged /v1/messages calls
This commit is contained in:
parent
97e5917ab4
commit
ea5f02e8dd
2 changed files with 52 additions and 1 deletions
|
|
@ -133,6 +133,15 @@ def _resolve_by_model_prefix(
|
|||
return None
|
||||
|
||||
|
||||
def _sets_own_entra_credential(litellm_params: GenericLiteLLMParams) -> bool:
|
||||
return bool(
|
||||
litellm_params.azure_ad_token
|
||||
or (litellm_params.model_extra or {}).get("azure_ad_token_provider") is not None
|
||||
or (litellm_params.tenant_id and litellm_params.client_id and litellm_params.client_secret)
|
||||
or (litellm_params.azure_username and litellm_params.azure_password and litellm_params.client_id)
|
||||
)
|
||||
|
||||
|
||||
def _bridge_target(requested: _BridgeTarget, resolved: _BridgeTarget, custom_llm_provider: str) -> _BridgeTarget:
|
||||
requested_provider: Final = requested.model.partition("/")[0]
|
||||
if requested_provider == custom_llm_provider or requested_provider not in LlmProvidersSet:
|
||||
|
|
@ -700,7 +709,11 @@ def anthropic_messages_handler(
|
|||
|
||||
bridge: Final = _bridge_target(
|
||||
_BridgeTarget(model=original_model, api_key=api_key, api_base=api_base),
|
||||
_BridgeTarget(model=model, api_key=dynamic_api_key, api_base=dynamic_api_base),
|
||||
_BridgeTarget(
|
||||
model=model,
|
||||
api_key=None if _sets_own_entra_credential(litellm_params) else dynamic_api_key,
|
||||
api_base=dynamic_api_base,
|
||||
),
|
||||
custom_llm_provider,
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -1245,6 +1245,44 @@ async def test_bridged_messages_send_the_deployments_key_for_azure_models_served
|
|||
assert {call.request.headers["authorization"] for call in upstream.calls} == {"Bearer deployment-key"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"entra_credential",
|
||||
[
|
||||
{"azure_ad_token": "entra-token"},
|
||||
{"azure_ad_token_provider": lambda: "entra-token"},
|
||||
{"tenant_id": "tenant", "client_id": "client", "client_secret": "secret"},
|
||||
{"azure_username": "user", "azure_password": "password", "client_id": "client"},
|
||||
],
|
||||
)
|
||||
async def test_bridged_messages_keep_the_deployments_entra_credential_over_the_env_key(monkeypatch, entra_credential):
|
||||
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
||||
monkeypatch.setenv("AZURE_AI_API_KEY", "azure-env-key")
|
||||
litellm.in_memory_llm_clients_cache.flush_cache()
|
||||
|
||||
with (
|
||||
patch("litellm.llms.azure.common_utils.get_azure_ad_token_from_entra_id", return_value=lambda: "entra-token"), # test-quality-ok: stands in for the Entra token fetch so the test can see which credential reaches Azure
|
||||
patch(
|
||||
"litellm.llms.azure.common_utils.get_azure_ad_token_from_username_password",
|
||||
return_value=lambda: "entra-token",
|
||||
), # test-quality-ok: stands in for the Entra token fetch so the test can see which credential reaches Azure
|
||||
respx.mock(assert_all_called=True) as respx_mock,
|
||||
):
|
||||
upstream: Final = respx_mock.route().mock(side_effect=httpx.ConnectError("cut at the wire"))
|
||||
with pytest.raises(openai.APIError):
|
||||
await litellm.anthropic.messages.acreate(
|
||||
max_tokens=100,
|
||||
messages=[{"role": "user", "content": "ping"}],
|
||||
model="azure_ai/gpt-6-luna",
|
||||
api_base="https://example-resource.cognitiveservices.azure.com",
|
||||
tools=[{"name": "get_weather", "input_schema": {"type": "object", "properties": {}}}],
|
||||
**entra_credential,
|
||||
)
|
||||
|
||||
assert {call.request.headers.get("authorization") for call in upstream.calls} == {"Bearer entra-token"}
|
||||
assert {call.request.headers.get("api-key") for call in upstream.calls} == {None}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bridged_messages_still_route_through_the_litellm_proxy_when_asked(monkeypatch):
|
||||
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue