This commit is contained in:
Lokesh Kank 2026-08-26 07:34:39 +01:00 committed by GitHub
commit 980a5638ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 3 deletions

View file

@ -290,9 +290,11 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig):
stream: bool | None = None,
) -> str:
api_base = AnthropicModelInfo.get_api_base(api_base) or "https://api.anthropic.com"
if not api_base.endswith("/v1/messages"):
api_base = f"{api_base}/v1/messages"
return api_base
base = api_base.rstrip("/")
if base.endswith("/v1/messages"):
return base
base = base.removesuffix("/v1")
return f"{base}/v1/messages"
def validate_anthropic_messages_environment(
self,

View file

@ -0,0 +1,37 @@
import pytest
from litellm.llms.anthropic.experimental_pass_through.messages.transformation import (
AnthropicMessagesConfig,
)
@pytest.fixture
def config() -> AnthropicMessagesConfig:
return AnthropicMessagesConfig()
@pytest.mark.parametrize(
"api_base, expected",
[
(None, "https://api.anthropic.com/v1/messages"),
("https://host", "https://host/v1/messages"),
("https://host/", "https://host/v1/messages"),
("https://host/v1", "https://host/v1/messages"),
("https://host/v1/", "https://host/v1/messages"),
("https://api.groq.com/openai/v1", "https://api.groq.com/openai/v1/messages"),
("https://host/v1/messages", "https://host/v1/messages"),
("https://gateway.example.com/anthropic", "https://gateway.example.com/anthropic/v1/messages"),
("https://gateway.example.com/anthropic/v1", "https://gateway.example.com/anthropic/v1/messages"),
],
)
def test_get_complete_url_deduplicates_trailing_v1(config, api_base, expected, monkeypatch):
monkeypatch.delenv("ANTHROPIC_API_BASE", raising=False)
monkeypatch.delenv("ANTHROPIC_BASE_URL", raising=False)
url = config.get_complete_url(
api_base=api_base,
api_key="sk-test",
model="claude-sonnet-4-5",
optional_params={},
litellm_params={},
)
assert url == expected