diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py index 283c706e45e..16263ac96ce 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py @@ -71,13 +71,14 @@ def _should_route_to_responses_api( custom_llm_provider: str | None, requested_model: str | None = None, resolved_model: str | None = None, + use_chat_completions_api: bool | None = None, ) -> bool: """Return True when the request should use the Responses API path. Set ``litellm.use_chat_completions_url_for_anthropic_messages = True`` to opt out and route OpenAI/Azure requests through chat/completions instead. """ - if litellm.use_chat_completions_url_for_anthropic_messages: + if litellm.use_chat_completions_url_for_anthropic_messages or use_chat_completions_api is True: return False if custom_llm_provider in _RESPONSES_API_PROVIDERS: return True @@ -589,7 +590,12 @@ def anthropic_messages_handler( custom_llm_provider=custom_llm_provider, **kwargs, ) - if _should_route_to_responses_api(custom_llm_provider, original_model, model): + if _should_route_to_responses_api( + custom_llm_provider=custom_llm_provider, + requested_model=original_model, + resolved_model=model, + use_chat_completions_api=kwargs.get("use_chat_completions_api"), + ): return LiteLLMMessagesToResponsesAPIHandler.anthropic_messages_handler(**_shared_kwargs) # The in-gateway context_management polyfill runs inside diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py index b690b3448ec..ee186c04dae 100644 --- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py +++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py @@ -874,26 +874,30 @@ def _gate_stubs(monkeypatch): from litellm.llms.anthropic.experimental_pass_through.messages import handler captured = {} - translation_calls = {"count": 0} + translation_calls = {"responses": 0, "chat_completions": 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" + def fake_responses(**kwargs): + translation_calls["responses"] += 1 + return "responses" + + def fake_chat_completions(**kwargs): + translation_calls["chat_completions"] += 1 + return "chat-completions" monkeypatch.setattr(handler.base_llm_http_handler, "anthropic_messages_handler", fake_native) monkeypatch.setattr( handler.LiteLLMMessagesToResponsesAPIHandler, "anthropic_messages_handler", - staticmethod(fake_translation), + staticmethod(fake_responses), ) monkeypatch.setattr( handler.LiteLLMMessagesToCompletionTransformationHandler, "anthropic_messages_handler", - staticmethod(fake_translation), + staticmethod(fake_chat_completions), ) return captured, translation_calls @@ -921,7 +925,8 @@ def test_gate_passthrough_when_supported_endpoints_opts_in(monkeypatch): assert result == "native-passthrough" assert isinstance(captured["config"], OpenAILikeAnthropicMessagesConfig) - assert translation_calls["count"] == 0 + assert translation_calls["responses"] == 0 + assert translation_calls["chat_completions"] == 0 def test_gate_translates_when_supported_endpoints_absent(monkeypatch): @@ -941,8 +946,32 @@ def test_gate_translates_when_supported_endpoints_absent(monkeypatch): api_base="https://host/v1", ) - assert result == "translated" - assert translation_calls["count"] == 1 + assert result == "responses" + assert translation_calls["responses"] == 1 + assert translation_calls["chat_completions"] == 0 + assert "config" not in captured + + +def test_gate_uses_chat_completions_when_requested(monkeypatch): + """The per-deployment chat-completions opt-in must bypass Responses API.""" + 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", + use_chat_completions_api=True, + ) + + assert result == "chat-completions" + assert translation_calls["responses"] == 0 + assert translation_calls["chat_completions"] == 1 assert "config" not in captured @@ -964,8 +993,9 @@ def test_gate_passthrough_skipped_when_only_chat_completions_supported(monkeypat model_info={"supported_endpoints": ["/v1/chat/completions"]}, ) - assert result == "translated" - assert translation_calls["count"] == 1 + assert result == "responses" + assert translation_calls["responses"] == 1 + assert translation_calls["chat_completions"] == 0 assert "config" not in captured