From ed127727c498581e6a3435484294184b1e8ab4ec Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 13 Jul 2026 09:08:52 +0000 Subject: [PATCH] fix(anthropic): honor custom-base bearer auth for messages --- .../messages/transformation.py | 6 ++++- .../anthropic/test_anthropic_common_utils.py | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py index 00941587753..d371cd26b4e 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py @@ -172,7 +172,11 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): headers, api_key = optionally_handle_anthropic_oauth(headers=headers, api_key=api_key) if "x-api-key" not in headers and "authorization" not in headers: - auth_header = AnthropicModelInfo.get_auth_header(api_key) + auth_header = AnthropicModelInfo.get_auth_header( + api_key=api_key, + api_base=api_base if api_base is not None else litellm_params.get("api_base"), + use_bearer_for_custom_base=bool(litellm_params.get("use_bearer_for_custom_base", False)), + ) if auth_header is not None: headers.update(auth_header) if "anthropic-version" not in headers: diff --git a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py index 3c410cf84df..ba96ad75882 100644 --- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py @@ -386,6 +386,29 @@ class TestPassthroughOAuth: assert updated_headers["x-api-key"] == FAKE_REGULAR_KEY assert "authorization" not in updated_headers + def test_passthrough_custom_base_uses_bearer_from_litellm_params(self): + from litellm.llms.anthropic.experimental_pass_through.messages.transformation import ( + AnthropicMessagesConfig, + ) + + config = AnthropicMessagesConfig() + + updated_headers, _ = config.validate_anthropic_messages_environment( + headers={}, + model="claude-sonnet-4-5-20250929", + messages=[{"role": "user", "content": "Hello"}], + optional_params={}, + litellm_params={ + "api_base": "https://custom-gateway.example.com", + "use_bearer_for_custom_base": True, + }, + api_key="custom-api-key", + api_base=None, + ) + + assert updated_headers["authorization"] == "Bearer custom-api-key" + assert "x-api-key" not in updated_headers + class TestIsAnthropicOAuthKey: """Tests for is_anthropic_oauth_key helper function."""