Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ def _should_route_to_responses_api(custom_llm_provider: Optional[str]) -> bool:
return custom_llm_provider in _RESPONSES_API_PROVIDERS


def _deployment_passes_through_anthropic_messages(model_info: object) -> bool:
"""Whether the deployment opted into forwarding /v1/messages untranslated.

The opt-in is ``model_info.supported_endpoints`` containing ``"/v1/messages"``,
declared per deployment in config.yaml and plumbed here as ``kwargs["model_info"]``
by the router.
"""
if not isinstance(model_info, dict):
return False
supported_endpoints = model_info.get("supported_endpoints")
return isinstance(supported_endpoints, (list, tuple)) and "/v1/messages" in supported_endpoints


####### ENVIRONMENT VARIABLES ###################
# Initialize any necessary instances or variables here
base_llm_http_handler = BaseLLMHTTPHandler()
Expand Down Expand Up @@ -456,6 +469,14 @@ def anthropic_messages_handler(
model=model,
provider=litellm.LlmProviders(custom_llm_provider),
)
if anthropic_messages_provider_config is None and _deployment_passes_through_anthropic_messages(
kwargs.get("model_info")
):
from litellm.llms.openai_like.messages.transformation import (
OpenAILikeAnthropicMessagesConfig,
)

anthropic_messages_provider_config = OpenAILikeAnthropicMessagesConfig()
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
if anthropic_messages_provider_config is None:
# Route to Responses API for OpenAI / Azure, chat/completions for everything else.
_shared_kwargs = dict(
Expand Down
Empty file.
66 changes: 66 additions & 0 deletions litellm/llms/openai_like/messages/transformation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from typing import Any, Optional

from litellm.llms.anthropic.experimental_pass_through.messages.transformation import (
AnthropicMessagesConfig,
)

DEFAULT_ANTHROPIC_API_VERSION = "2023-06-01"


class OpenAILikeAnthropicMessagesConfig(AnthropicMessagesConfig):
"""
Forwards Anthropic /v1/messages requests to an OpenAI-compatible server that
also natively exposes the Anthropic Messages API, with no translation.

Opted into per deployment via ``model_info.supported_endpoints`` containing
``"/v1/messages"``. The inbound Anthropic payload (system, cache_control,
thinking, tools, ...) is forwarded essentially unchanged to
``{api_base}/v1/messages``, so Anthropic-only features that the
Anthropic->OpenAI translation would otherwise drop are preserved. Response
parsing and streaming are inherited from the native Anthropic config.
"""

def validate_anthropic_messages_environment(
self,
headers: dict[str, str],
model: str,
messages: list[Any],
optional_params: dict,
litellm_params: dict,
api_key: Optional[str] = None,
api_base: Optional[str] = None,
) -> tuple[dict[str, str], Optional[str]]:
present = {key.lower() for key in headers}
needs_auth = bool(api_key) and "authorization" not in present and "x-api-key" not in present
defaults: dict[str, str] = {
**({"authorization": f"Bearer {api_key}"} if needs_auth else {}),
**({"anthropic-version": DEFAULT_ANTHROPIC_API_VERSION} if "anthropic-version" not in present else {}),
**({"content-type": "application/json"} if "content-type" not in present else {}),
}
combined = {**headers, **defaults}
normalized = {
("anthropic-beta" if key.lower() == "anthropic-beta" else key): value for key, value in combined.items()
}
merged = self._update_headers_with_anthropic_beta(
headers=normalized,
optional_params=optional_params,
)
return merged, api_base
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

def get_complete_url(
self,
api_base: Optional[str],
api_key: Optional[str],
model: str,
optional_params: dict,
litellm_params: dict,
stream: Optional[bool] = None,
) -> str:
if not api_base:
raise ValueError("api_base is required to forward Anthropic /v1/messages to a native endpoint")
base = api_base.rstrip("/")
if base.endswith("/v1/messages"):
return base
if base.endswith("/v1"):
base = base[: -len("/v1")]
return f"{base}/v1/messages"
Original file line number Diff line number Diff line change
Expand Up @@ -715,3 +715,109 @@ def fake_handler(*args, **kwargs):
assert spy.call_count == 1
assert captured["presanitized"] is True
assert [b["type"] for b in captured["messages"][0]["content"]] == ["tool_use"]


def _gate_stubs(monkeypatch):
"""Patch the gate's downstream dispatch targets so config selection can be
observed without making a network call.

Returns ``(captured, translation_calls)`` where ``captured["config"]`` is the
provider config handed to the native passthrough path and ``translation_calls``
counts hits on the Anthropic->OpenAI translation handlers.
"""
from litellm.llms.anthropic.experimental_pass_through.messages import handler

captured = {}
translation_calls = {"count": 0}

def fake_native(**kwargs):
captured["config"] = kwargs.get("anthropic_messages_provider_config")
return "native-passthrough"

def fake_translation(**kwargs):
translation_calls["count"] += 1
return "translated"

monkeypatch.setattr(handler.base_llm_http_handler, "anthropic_messages_handler", fake_native)
monkeypatch.setattr(
handler.LiteLLMMessagesToResponsesAPIHandler,
"anthropic_messages_handler",
staticmethod(fake_translation),
)
monkeypatch.setattr(
handler.LiteLLMMessagesToCompletionTransformationHandler,
"anthropic_messages_handler",
staticmethod(fake_translation),
)
return captured, translation_calls


def test_gate_passthrough_when_supported_endpoints_opts_in(monkeypatch):
"""provider=openai + model_info.supported_endpoints containing /v1/messages
must route to the native passthrough config, NOT the translation handlers."""
from litellm.llms.anthropic.experimental_pass_through.messages.handler import (
anthropic_messages_handler,
)
from litellm.llms.openai_like.messages.transformation import (
OpenAILikeAnthropicMessagesConfig,
)

captured, translation_calls = _gate_stubs(monkeypatch)

result = anthropic_messages_handler(
max_tokens=100,
messages=[{"role": "user", "content": "Hello"}],
model="openai/some-model",
api_key="sk-test",
api_base="https://host/v1",
model_info={"supported_endpoints": ["/v1/chat/completions", "/v1/messages"]},
)

assert result == "native-passthrough"
assert isinstance(captured["config"], OpenAILikeAnthropicMessagesConfig)
assert translation_calls["count"] == 0


def test_gate_translates_when_supported_endpoints_absent(monkeypatch):
"""Default behavior is unchanged: without the /v1/messages opt-in, an openai
deployment is translated (Responses API), never passed through natively."""
from litellm.llms.anthropic.experimental_pass_through.messages.handler import (
anthropic_messages_handler,
)

captured, translation_calls = _gate_stubs(monkeypatch)

result = anthropic_messages_handler(
max_tokens=100,
messages=[{"role": "user", "content": "Hello"}],
model="openai/some-model",
api_key="sk-test",
api_base="https://host/v1",
)

assert result == "translated"
assert translation_calls["count"] == 1
assert "config" not in captured


def test_gate_passthrough_skipped_when_only_chat_completions_supported(monkeypatch):
"""A deployment that lists only /v1/chat/completions is still translated;
the opt-in is specifically the /v1/messages entry."""
from litellm.llms.anthropic.experimental_pass_through.messages.handler import (
anthropic_messages_handler,
)

captured, translation_calls = _gate_stubs(monkeypatch)

result = anthropic_messages_handler(
max_tokens=100,
messages=[{"role": "user", "content": "Hello"}],
model="openai/some-model",
api_key="sk-test",
api_base="https://host/v1",
model_info={"supported_endpoints": ["/v1/chat/completions"]},
)

assert result == "translated"
assert translation_calls["count"] == 1
assert "config" not in captured
Empty file.
Loading
Loading