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(anthropic): raise missing-credential error on /v1/messages passthrough
Devin AIe14f485827ab
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.pyindex 032bf0130ce..75146922a39 100644--- a/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py+++ b/litellm/llms/anthropic/experimental_pass_through/messages/transformation.py@@ -8,6 +8,7 @@ from litellm.constants import (DEFAULT_REASONING_EFFORT_MEDIUM_THINKING_BUDGET,DEFAULT_REASONING_EFFORT_XHIGH_THINKING_BUDGET,)+from litellm.exceptions import AuthenticationErrorfrom litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObjfrom litellm.litellm_core_utils.litellm_logging import verbose_loggerfrom litellm.llms.base_llm.anthropic_messages.transformation import (@@ -309,8 +310,17 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig):if "x-api-key" not in headers and "authorization" not in headers:auth_header: Final = AnthropicModelInfo.get_auth_header(api_key)- if auth_header is not None:- headers.update(auth_header)+ if auth_header is None:+ raise AuthenticationError(+ message=(+ "Missing Anthropic API Key - A call is being made to anthropic but no key is set "+ "either in the environment variables or via params. Please set `ANTHROPIC_API_KEY` "+ "or `ANTHROPIC_AUTH_TOKEN` in your environment vars"+ ),+ llm_provider=self._resolved_provider,+ model=model,+ )+ headers.update(auth_header)if "anthropic-version" not in headers:headers["anthropic-version"] = DEFAULT_ANTHROPIC_API_VERSIONif "content-type" 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.pyindex c27362bf49f..a96c44ac5c0 100644--- a/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py+++ b/tests/test_litellm/llms/anthropic/test_anthropic_common_utils.py@@ -1227,6 +1227,50 @@ class TestPassthroughAuthToken:assert updated_headers["x-api-key"] == FAKE_REGULAR_KEYassert "authorization" not in updated_headers+ def test_passthrough_missing_credentials_raises_authentication_error(self):+ """Passthrough endpoint should raise locally instead of forwarding an unauthenticated request."""+ from unittest.mock import patch as mock_patch++ import litellm+ from litellm.llms.anthropic.experimental_pass_through.messages.transformation import (+ AnthropicMessagesConfig,+ )++ config = AnthropicMessagesConfig()+ with mock_patch.dict("os.environ", {}, clear=True):+ with pytest.raises(litellm.AuthenticationError, match="Missing Anthropic API Key"):+ config.validate_anthropic_messages_environment(+ headers={},+ model="claude-sonnet-4-5-20250929",+ messages=[{"role": "user", "content": "Hello"}],+ optional_params={},+ litellm_params={},+ api_key=None,+ api_base=None,+ )++ def test_passthrough_client_x_api_key_header_is_kept(self):+ """A client-forwarded x-api-key header should satisfy validation without env credentials."""+ from unittest.mock import patch as mock_patch++ from litellm.llms.anthropic.experimental_pass_through.messages.transformation import (+ AnthropicMessagesConfig,+ )++ config = AnthropicMessagesConfig()+ with mock_patch.dict("os.environ", {}, clear=True):+ updated_headers, _ = config.validate_anthropic_messages_environment(+ headers={"x-api-key": FAKE_REGULAR_KEY},+ model="claude-sonnet-4-5-20250929",+ messages=[{"role": "user", "content": "Hello"}],+ optional_params={},+ litellm_params={},+ api_key=None,+ api_base=None,+ )++ assert updated_headers["x-api-key"] == FAKE_REGULAR_KEY+def test_passthrough_get_complete_url_honours_base_url_env(self):"""get_complete_url should use ANTHROPIC_BASE_URL when api_base is None."""from unittest.mock import patch as mock_patch