Skip to content

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]

fix: support Azure AD token auth for non-Claude azure_ai models (#20981)

Ishaan Jaff9975a9e3d42d
diff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.md
index a6d4e3102a6..38ad9bdd0ee 100644
--- a/docs/my-website/docs/proxy/config_settings.md
+++ b/docs/my-website/docs/proxy/config_settings.md
@@ -812,6 +812,7 @@ router_settings:
| MAX_RETRY_DELAY | Maximum delay in seconds for retrying requests. Default is 8.0
| MAX_LANGFUSE_INITIALIZED_CLIENTS | Maximum number of Langfuse clients to initialize on proxy. Default is 50. This is set since langfuse initializes 1 thread everytime a client is initialized. We've had an incident in the past where we reached 100% cpu utilization because Langfuse was initialized several times.
| MAX_MCP_SEMANTIC_FILTER_TOOLS_HEADER_LENGTH | Maximum header length for MCP semantic filter tools. Default is 150
+| MAX_POLICY_ESTIMATE_IMPACT_ROWS | Maximum number of rows returned when estimating the impact of a policy. Default is 1000
| MIN_NON_ZERO_TEMPERATURE | Minimum non-zero temperature value. Default is 0.0001
| MINIMUM_PROMPT_CACHE_TOKEN_COUNT | Minimum token count for caching a prompt. Default is 1024
| MISTRAL_API_BASE | Base URL for Mistral API. Default is https://api.mistral.ai
@@ -828,6 +829,8 @@ router_settings:
| MICROSOFT_USER_ID_ATTRIBUTE | Field name for user ID in Microsoft SSO response. Default is `id`
| MICROSOFT_USER_LAST_NAME_ATTRIBUTE | Field name for user last name in Microsoft SSO response. Default is `surname`
| MICROSOFT_USERINFO_ENDPOINT | Custom userinfo endpoint URL for Microsoft SSO (overrides default Microsoft Graph userinfo endpoint)
+| MODEL_COST_MAP_MAX_SHRINK_RATIO | Maximum allowed shrinkage ratio when validating a fetched model cost map against the local backup. Rejects the fetched map if it is smaller than this fraction of the backup. Default is 0.5
+| MODEL_COST_MAP_MIN_MODEL_COUNT | Minimum number of models a fetched cost map must contain to be considered valid. Default is 50
| NO_DOCS | Flag to disable Swagger UI documentation
| NO_REDOC | Flag to disable Redoc documentation
| NO_PROXY | List of addresses to bypass proxy
diff --git a/litellm/llms/azure_ai/chat/transformation.py b/litellm/llms/azure_ai/chat/transformation.py
index 04d2b3a2769..585efd3307d 100644
--- a/litellm/llms/azure_ai/chat/transformation.py
+++ b/litellm/llms/azure_ai/chat/transformation.py
@@ -11,12 +11,14 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (
_audio_or_image_in_message_content,
convert_content_list_to_str,
)
+from litellm.llms.azure.common_utils import BaseAzureLLM
from litellm.llms.base_llm.chat.transformation import LiteLLMLoggingObj
from litellm.llms.openai.common_utils import drop_params_from_unprocessable_entity_error
from litellm.llms.openai.openai import OpenAIConfig
from litellm.llms.xai.chat.transformation import XAIChatConfig
from litellm.secret_managers.main import get_secret_str
from litellm.types.llms.openai import AllMessageValues
+from litellm.types.router import GenericLiteLLMParams
from litellm.types.utils import ModelResponse, ProviderField
from litellm.utils import _add_path_to_api_base, supports_tool_choice
@@ -64,12 +66,21 @@ class AzureAIStudioConfig(OpenAIConfig):
api_key: Optional[str] = None,
api_base: Optional[str] = None,
) -> dict:
- if api_base and self._should_use_api_key_header(api_base):
- headers["api-key"] = api_key
+ if api_key:
+ if api_base and self._should_use_api_key_header(api_base):
+ headers["api-key"] = api_key
+ else:
+ headers["Authorization"] = f"Bearer {api_key}"
else:
- headers["Authorization"] = f"Bearer {api_key}"
+ # No api_key provided — fall back to Azure AD token-based auth
+ litellm_params_obj = GenericLiteLLMParams(
+ **(litellm_params if isinstance(litellm_params, dict) else {})
+ )
+ headers = BaseAzureLLM._base_validate_azure_environment(
+ headers=headers, litellm_params=litellm_params_obj
+ )
- headers["Content-Type"] = "application/json" # tell Azure AI Studio to expect JSON
+ headers["Content-Type"] = "application/json"
return headers
diff --git a/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py b/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py
index c91ef31bba5..d903d7c85f1 100644
--- a/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py
+++ b/tests/test_litellm/llms/azure_ai/chat/test_azure_ai_transformation.py
@@ -45,6 +45,58 @@ def test_azure_ai_validate_environment():
assert headers["Content-Type"] == "application/json"
+def test_azure_ai_validate_environment_with_api_key():
+ """
+ Test that when api_key is provided, it is set in the api-key header
+ for Azure Foundry endpoints (.services.ai.azure.com).
+ """
+ config = AzureAIStudioConfig()
+ headers = config.validate_environment(
+ headers={},
+ model="Kimi-K2.5",
+ messages=[],
+ optional_params={},
+ litellm_params={},
+ api_key="test-api-key",
+ api_base="https://my-endpoint.services.ai.azure.com",
+ )
+ assert headers["api-key"] == "test-api-key"
+ assert headers["Content-Type"] == "application/json"
+
+
+def test_azure_ai_validate_environment_with_azure_ad_token():
+ """
+ Test that when no api_key is provided but Azure AD credentials are available,
+ the Authorization header is set with a Bearer token.
+
+ Regression test for https://github.com/BerriAI/litellm/issues/20759
+ """
+ import litellm
+
+ config = AzureAIStudioConfig()
+ with patch(
+ "litellm.llms.azure.common_utils.get_azure_ad_token",
+ return_value="fake-azure-ad-token",
+ ), patch(
+ "litellm.llms.azure.common_utils.get_secret_str",
+ return_value=None,
+ ), patch.object(litellm, "api_key", None), patch.object(
+ litellm, "azure_key", None
+ ):
+ headers = config.validate_environment(
+ headers={},
+ model="Kimi-K2.5",
+ messages=[],
+ optional_params={},
+ litellm_params={},
+ api_key=None,
+ api_base="https://my-endpoint.services.ai.azure.com",
+ )
+ assert headers.get("Authorization") == "Bearer fake-azure-ad-token"
+ assert "api-key" not in headers
+ assert headers["Content-Type"] == "application/json"
+
+
def test_azure_ai_grok_stop_parameter_handling():
"""
Test that Grok models properly handle stop parameter filtering in Azure AI Studio.