From d7468dab7e812054568c73e99392e954114674a8 Mon Sep 17 00:00:00 2001 From: Daniel Krueger Date: Mon, 29 Dec 2025 15:29:54 +0100 Subject: [PATCH] fix authentication errors at messages API via azure_ai Use x-api-key instead of api-key. This has been removed by commit 61e737e361d1b2649e9fd491580529ea894dd0d4 for unknown reason. --- .../anthropic/messages_transformation.py | 7 ++++++- ...azure_anthropic_messages_transformation.py | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/litellm/llms/azure_ai/anthropic/messages_transformation.py b/litellm/llms/azure_ai/anthropic/messages_transformation.py index 73dc84167a..55818cc07d 100644 --- a/litellm/llms/azure_ai/anthropic/messages_transformation.py +++ b/litellm/llms/azure_ai/anthropic/messages_transformation.py @@ -48,7 +48,12 @@ class AzureAnthropicMessagesConfig(AnthropicMessagesConfig): headers = BaseAzureLLM._base_validate_azure_environment( headers=headers, litellm_params=litellm_params_obj ) - + + # Azure Anthropic uses x-api-key header (not api-key) + # Convert api-key to x-api-key if present + if "api-key" in headers and "x-api-key" not in headers: + headers["x-api-key"] = headers.pop("api-key") + # Set anthropic-version header if "anthropic-version" not in headers: headers["anthropic-version"] = "2023-06-01" diff --git a/tests/test_litellm/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py b/tests/test_litellm/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py index d78a638fd8..bdced849c7 100644 --- a/tests/test_litellm/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py +++ b/tests/test_litellm/llms/azure_ai/claude/test_azure_anthropic_messages_transformation.py @@ -55,11 +55,12 @@ class TestAzureAnthropicMessagesConfig: assert isinstance(call_args[1]["litellm_params"], GenericLiteLLMParams) assert call_args[1]["litellm_params"].api_key == "test-api-key" assert "anthropic-version" in result - # api-key header is preserved as-is (no conversion to x-api-key) - assert "api-key" in result + assert "x-api-key" in result + assert result["x-api-key"] == "test-api-key" + assert "api-key" not in result - def test_validate_anthropic_messages_environment_preserves_api_key_header(self): - """Test that api-key header is preserved as-is (Azure handles the header internally)""" + def test_validate_anthropic_messages_environment_converts_api_key_to_x_api_key(self): + """Test that api-key header is converted to x-api-key""" config = AzureAnthropicMessagesConfig() headers = {} model = "claude-sonnet-4-5" @@ -79,9 +80,10 @@ class TestAzureAnthropicMessagesConfig: litellm_params=litellm_params, ) - # Verify api-key header is preserved as-is - assert "api-key" in result - assert result["api-key"] == "test-api-key" + # Verify api-key was converted to x-api-key + assert "x-api-key" in result + assert result["x-api-key"] == "test-api-key" + assert "api-key" not in result def test_validate_anthropic_messages_environment_sets_headers(self): """Test that required headers are set""" @@ -108,8 +110,7 @@ class TestAzureAnthropicMessagesConfig: assert result["anthropic-version"] == "2023-06-01" assert "content-type" in result assert result["content-type"] == "application/json" - # api-key header is preserved as-is - assert "api-key" in result + assert "x-api-key" in result def test_get_complete_url_with_base_url(self): """Test get_complete_url with base URL"""