diff --git a/litellm/llms/anthropic/messages/native/handler.py b/litellm/llms/anthropic/messages/native/handler.py index 3f495853655..51b28c9ead8 100644 --- a/litellm/llms/anthropic/messages/native/handler.py +++ b/litellm/llms/anthropic/messages/native/handler.py @@ -16,6 +16,7 @@ from pydantic import TypeAdapter import litellm from litellm.anthropic_beta_headers_manager import aload_beta_headers_config +from litellm.constants import PROVIDERS_THAT_AUTHENTICATE_ON_PROVIDER_INFO from litellm.integrations.anthropic_cache_control_hook import AnthropicCacheControlHook from litellm.litellm_core_utils.core_helpers import as_str_mapping from litellm.litellm_core_utils.exception_mapping_utils import exception_type @@ -113,7 +114,11 @@ def _resolve_requested_provider( litellm_params=litellm_params, ) requested_provider: Final = model.partition("/")[0] - if custom_llm_provider in (None, requested_provider) or requested_provider not in LlmProvidersSet: + if ( + custom_llm_provider in (None, requested_provider) + or requested_provider not in LlmProvidersSet + or requested_provider in PROVIDERS_THAT_AUTHENTICATE_ON_PROVIDER_INFO + ): return resolved own: Final = _resolve_by_model_prefix(model, litellm_params) return own if own is not None and own[1] == custom_llm_provider else resolved diff --git a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_handler.py b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_handler.py index 6e7600861ee..eabc18e2550 100644 --- a/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_handler.py +++ b/tests/unit/llms/anthropic/messages/native/test_anthropic_messages_handler.py @@ -1225,6 +1225,30 @@ async def test_bridged_messages_keep_the_callers_explicit_provider_over_the_mode assert {json.loads(call.request.content)["model"] for call in upstream.calls} == {model} +@pytest.mark.parametrize("model", ["chatgpt/gpt-5", "github_copilot/gpt-5"]) +def test_bridged_messages_reach_the_callers_provider_without_signing_in_to_the_prefix_provider( + model, monkeypatch, tmp_path +): + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setenv("OPENAI_API_KEY", "server-openai-key") + monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path)) + monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path)) + litellm.in_memory_llm_clients_cache.flush_cache() + + with respx.mock(assert_all_called=True) as respx_mock: + upstream: Final = respx_mock.route().mock(side_effect=httpx.ConnectError("cut at the wire")) + with pytest.raises(openai.APIError): + litellm.anthropic.messages.create( + max_tokens=100, + messages=[{"role": "user", "content": "ping"}], + model=model, + custom_llm_provider="openai", + ) + + assert {call.request.url.host for call in upstream.calls} == {"api.openai.com"} + assert {call.request.headers["authorization"] for call in upstream.calls} == {"Bearer server-openai-key"} + + @pytest.mark.asyncio async def test_bridged_messages_send_the_deployments_key_for_azure_models_served_as_openai(monkeypatch): monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) diff --git a/tests/unit/llms/anthropic/messages/native/test_mcp_handler.py b/tests/unit/llms/anthropic/messages/native/test_mcp_handler.py index 82181c8d543..5fe6bb122e8 100644 --- a/tests/unit/llms/anthropic/messages/native/test_mcp_handler.py +++ b/tests/unit/llms/anthropic/messages/native/test_mcp_handler.py @@ -67,25 +67,19 @@ async def test_anthropic_messages_with_mcp_reaches_an_env_only_deployment_with_i monkeypatch.setenv("AZURE_AI_API_KEY", "azure-env-key") litellm.in_memory_llm_clients_cache.flush_cache() - with ( - patch.object( - import_module("litellm.responses.mcp.litellm_proxy_mcp_handler").LiteLLM_Proxy_MCP_Handler, - "process_mcp_tools_without_openai_transform", - new=AsyncMock(return_value=([], {})), - ), - respx.mock(assert_all_called=True) as respx_mock, - ): + with respx.mock(assert_all_called=True) as respx_mock: upstream: Final = respx_mock.route().mock(side_effect=httpx.ConnectError("cut at the wire")) with pytest.raises(openai.APIError): await litellm.anthropic.messages.acreate( max_tokens=100, messages=[{"role": "user", "content": "ping"}], model="azure_ai/gpt-6-luna", - tools=[MCP_REFERENCE], + tools=[{**MCP_REFERENCE, "server_url": "litellm_proxy"}], ) assert {call.request.url.host for call in upstream.calls} == {"example-resource.cognitiveservices.azure.com"} assert {call.request.headers.get("api-key") for call in upstream.calls} == {"azure-env-key"} + assert not any(b"litellm_proxy" in call.request.content for call in upstream.calls) def test_anthropic_messages_handler_skips_the_gateway_on_recursion():