The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(azure): reuse the cached Entra token provider on the Responses path (#46421)
Jorik492272a051c2
diff --git a/litellm/llms/azure/common_utils.py b/litellm/llms/azure/common_utils.pyindex 46106026920..f9f636244f4 100644--- a/litellm/llms/azure/common_utils.py+++ b/litellm/llms/azure/common_utils.py@@ -375,7 +375,7 @@ def get_azure_ad_token("Using Azure AD token provider based on Service Principal with Secret workflow or DefaultAzureCredential for Azure Auth")try:- azure_ad_token_provider = get_azure_ad_token_provider(azure_scope=scope)+ azure_ad_token_provider = _cached_azure_ad_token_refresh_provider(scope)except ValueError:verbose_logger.debug("Azure AD Token Provider could not be used.")except Exception as e:diff --git a/tests/unit/llms/azure/test_azure_common_utils.py b/tests/unit/llms/azure/test_azure_common_utils.pyindex b277cac1f29..80449909b3f 100644--- a/tests/unit/llms/azure/test_azure_common_utils.py+++ b/tests/unit/llms/azure/test_azure_common_utils.py@@ -1410,6 +1410,26 @@ def test_get_azure_ad_token_with_token_refresh(setup_mocks, monkeypatch):assert token == "mock-default-token"+def test_get_azure_ad_token_with_token_refresh_reuses_provider_per_scope(setup_mocks):+ """Repeated requests reuse one token provider per scope instead of building a+ new credential, and fetching a new token, on every call (#46419)"""+ setup_mocks["litellm"].enable_azure_ad_token_refresh = True+ default_scope = "https://cognitiveservices.azure.com/.default"+ other_scope = "api://other-resource/.default"++ tokens = [+ get_azure_ad_token(GenericLiteLLMParams()),+ get_azure_ad_token(GenericLiteLLMParams()),+ get_azure_ad_token(GenericLiteLLMParams(azure_scope=other_scope)),+ ]++ assert tokens == ["mock-default-token"] * 3+ assert [c.kwargs for c in setup_mocks["token_provider"].call_args_list] == [+ {"azure_scope": default_scope},+ {"azure_scope": other_scope},+ ]++def test_get_azure_ad_token_with_token_refresh_error(setup_mocks):"""Test get_azure_ad_token with token refresh enabled but raising an error."""# Reset mocks to ensure clean state