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]

Address review feedback: fix OAuth routing in get_auth_header and self-contained validate_environment

Devin Petersohnb7e226994288
diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py
index 5b7937208af..7d2d0a74961 100644
--- a/litellm/llms/anthropic/common_utils.py
+++ b/litellm/llms/anthropic/common_utils.py
@@ -486,6 +486,7 @@ class AnthropicModelInfo(BaseLLMModelInfo):
headers, api_key = optionally_handle_anthropic_oauth(
headers=headers, api_key=api_key
)
+ api_key = AnthropicModelInfo.get_api_key(api_key)
# Resolve auth_token from ANTHROPIC_AUTH_TOKEN if api_key is not set
auth_token: Optional[str] = None
if api_key is None:
@@ -580,6 +581,8 @@ class AnthropicModelInfo(BaseLLMModelInfo):
"""
resolved_key = AnthropicModelInfo.get_api_key(api_key)
if resolved_key is not None:
+ if is_anthropic_oauth_key(resolved_key):
+ return {"authorization": f"Bearer {resolved_key}"}
return {"x-api-key": resolved_key}
auth_token = AnthropicModelInfo.get_auth_token()
if auth_token is not None:
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 fcec155bae0..dd5c49258ee 100644
--- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py
+++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py
@@ -841,6 +841,31 @@ class TestValidateEnvironmentAuthToken:
api_base=None,
)
+ def test_resolves_api_key_from_env_when_param_is_none(self):
+ """validate_environment should resolve ANTHROPIC_API_KEY from env when api_key param is None."""
+ from unittest.mock import patch as mock_patch
+
+ from litellm.llms.anthropic.common_utils import AnthropicModelInfo
+
+ config = AnthropicModelInfo()
+ with mock_patch.dict(
+ "os.environ",
+ {"ANTHROPIC_API_KEY": FAKE_REGULAR_KEY},
+ clear=True,
+ ):
+ headers = config.validate_environment(
+ headers={},
+ model="claude-sonnet-4-5-20250929",
+ messages=[{"role": "user", "content": "Hello"}],
+ optional_params={},
+ litellm_params={},
+ api_key=None,
+ api_base=None,
+ )
+
+ assert headers["x-api-key"] == FAKE_REGULAR_KEY
+ assert "authorization" not in headers
+
@@ -954,6 +979,27 @@ class TestGetAuthHeader:
result = AnthropicModelInfo.get_auth_header()
assert result is None
+ def test_oauth_token_uses_bearer_not_x_api_key(self):
+ """OAuth token (sk-ant-oat*) should return Authorization: Bearer, not x-api-key."""
+ from litellm.llms.anthropic.common_utils import AnthropicModelInfo
+
+ result = AnthropicModelInfo.get_auth_header(api_key=FAKE_OAUTH_TOKEN)
+ assert result == {"authorization": f"Bearer {FAKE_OAUTH_TOKEN}"}
+
+ def test_oauth_token_from_env_uses_bearer(self):
+ """OAuth token in ANTHROPIC_API_KEY env var should return Authorization: Bearer."""
+ from unittest.mock import patch as mock_patch
+
+ from litellm.llms.anthropic.common_utils import AnthropicModelInfo
+
+ with mock_patch.dict(
+ "os.environ",
+ {"ANTHROPIC_API_KEY": FAKE_OAUTH_TOKEN},
+ clear=True,
+ ):
+ result = AnthropicModelInfo.get_auth_header()
+ assert result == {"authorization": f"Bearer {FAKE_OAUTH_TOKEN}"}
+
class TestGetApiBaseFallbackChain:
"""Tests for AnthropicModelInfo.get_api_base() fallback to ANTHROPIC_BASE_URL."""