mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* fix(router): send the fallback deployment's own key instead of an inherited Anthropic key On a fallback from an Anthropic group, a recognizable Anthropic caller key (sk-ant-) no longer replaces the credential a Bedrock or Bedrock Mantle bearer deployment, or an OpenAI deployment, resolves on its own default endpoint. The Bedrock bearer comes from the deployment api_key, a router default or the provider env bearer. The OpenAI key comes from the deployment, a router default, OPENAI_API_KEY, litellm.api_key or litellm.openai_key. The existing IAM behavior is unchanged for every caller key. Opaque or destination-shaped caller keys, blank keys, fallback targets that set api_key, explicit Authorization headers, custom endpoints (deployment, request, dict target, router defaults, provider env, litellm.api_base), mixed-group members, Claude Platform, and OpenAI deployments with no key of their own all keep today's behavior. * fix(router): check the caller key owner before resolving fallback destination auth leaves_caller_key_behind now classifies the destination by provider and checks the Anthropic owner before it reads any endpoint env var or secret, so direct and non-Anthropic calls resolve nothing new, as before. The OpenAI own-key check reuses openai_static_api_key, and a non-string deployment api_key keeps the caller key. Tests add an OpenAI fallback whose explicit Authorization header keeps the forwarded caller key, and direct and OpenAI-to-OpenAI calls that keep an sk-ant caller key without an Anthropic origin. * test(router): key the alias fallback case on the requested alias * test(router): keep the caller key when a router default api_key is not a string * fix(router): admit fallbacks using their effective caller credentials --------- Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
2630 lines
101 KiB
Python
2630 lines
101 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
import logging
|
|
from collections.abc import AsyncIterator, Callable
|
|
from typing import Final, Literal
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
|
|
import litellm
|
|
from litellm import Router
|
|
import os
|
|
from tests.fake_openai_endpoint import FAKE_OPENAI_API_BASE
|
|
from tests.unit.llms.bedrock.slow_upstream import CONVERSE_ANSWER
|
|
from litellm.integrations.custom_logger import CustomLogger
|
|
from litellm._logging import verbose_logger, verbose_proxy_logger, verbose_router_logger
|
|
from litellm.llms.custom_httpx.async_client_cleanup import close_litellm_async_clients
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_fallbacks_streaming():
|
|
"""Test that router.acompletion with stream=True and mock_response works correctly."""
|
|
litellm.set_verbose = False
|
|
model_list = [
|
|
{
|
|
"model_name": "azure/gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": "fake-key",
|
|
"api_version": "2024-01-01",
|
|
"api_base": "https://fake.openai.azure.com",
|
|
},
|
|
"tpm": 240000,
|
|
"rpm": 1800,
|
|
},
|
|
{
|
|
"model_name": "gpt-4o-mini",
|
|
"litellm_params": {
|
|
"model": "gpt-4o-mini",
|
|
"api_key": "fake-key",
|
|
},
|
|
"tpm": 1000000,
|
|
"rpm": 9000,
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
fallbacks=[{"azure/gpt-3.5-turbo": ["gpt-4o-mini"]}],
|
|
set_verbose=False,
|
|
)
|
|
customHandler = MyCustomHandler()
|
|
litellm.callbacks = [customHandler]
|
|
user_message = "Hello, how are you?"
|
|
try:
|
|
response = await router.acompletion(
|
|
model="azure/gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": user_message}],
|
|
stream=True,
|
|
mock_response="This is a mock streaming response",
|
|
)
|
|
chunks = []
|
|
async for chunk in response:
|
|
chunks.append(chunk)
|
|
assert len(chunks) > 0, "Expected at least one streaming chunk"
|
|
router.reset()
|
|
except litellm.Timeout as e:
|
|
pass
|
|
except Exception as e:
|
|
pytest.fail(f"An exception occurred: {e}")
|
|
finally:
|
|
router.reset()
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@pytest.mark.parametrize("litellm_module_fallbacks", [True, False])
|
|
@pytest.mark.asyncio
|
|
async def test_default_model_fallbacks(sync_mode, litellm_module_fallbacks):
|
|
"""
|
|
Related issue - https://github.com/BerriAI/litellm/issues/3623
|
|
|
|
If model misconfigured, setup a default model for generic fallback
|
|
"""
|
|
if litellm_module_fallbacks:
|
|
litellm.default_fallbacks = ["my-good-model"]
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bad-model",
|
|
"litellm_params": {
|
|
"model": "openai/my-bad-model",
|
|
"api_key": "my-bad-api-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "my-good-model",
|
|
"litellm_params": {
|
|
"model": "gpt-4o",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
],
|
|
default_fallbacks=(
|
|
["my-good-model"] if litellm_module_fallbacks is False else None
|
|
),
|
|
)
|
|
|
|
if sync_mode:
|
|
response = router.completion(
|
|
model="bad-model",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
mock_testing_fallbacks=True,
|
|
mock_response="Hey! nice day",
|
|
)
|
|
else:
|
|
response = await router.acompletion(
|
|
model="bad-model",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
mock_testing_fallbacks=True,
|
|
mock_response="Hey! nice day",
|
|
)
|
|
|
|
assert isinstance(response, litellm.ModelResponse)
|
|
assert response.model is not None and response.model == "gpt-4o"
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@pytest.mark.asyncio
|
|
async def test_client_side_fallbacks_list(sync_mode):
|
|
"""
|
|
|
|
Tests Client Side Fallbacks
|
|
|
|
User can pass "fallbacks": ["gpt-3.5-turbo"] and this should work
|
|
|
|
"""
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bad-model",
|
|
"litellm_params": {
|
|
"model": "openai/my-bad-model",
|
|
"api_key": "my-bad-api-key",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "my-good-model",
|
|
"litellm_params": {
|
|
"model": "gpt-4o",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
],
|
|
)
|
|
|
|
if sync_mode:
|
|
response = router.completion(
|
|
model="bad-model",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
fallbacks=["my-good-model"],
|
|
mock_testing_fallbacks=True,
|
|
mock_response="Hey! nice day",
|
|
)
|
|
else:
|
|
response = await router.acompletion(
|
|
model="bad-model",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
fallbacks=["my-good-model"],
|
|
mock_testing_fallbacks=True,
|
|
mock_response="Hey! nice day",
|
|
)
|
|
|
|
assert isinstance(response, litellm.ModelResponse)
|
|
assert response.model is not None and response.model == "gpt-4o"
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@pytest.mark.parametrize("content_filter_response_exception", [True, False])
|
|
@pytest.mark.parametrize("fallback_type", ["model-specific", "default"])
|
|
@pytest.mark.asyncio
|
|
async def test_router_content_policy_fallbacks(
|
|
sync_mode, content_filter_response_exception, fallback_type
|
|
):
|
|
os.environ["LITELLM_LOG"] = "DEBUG"
|
|
|
|
if content_filter_response_exception:
|
|
mock_response = Exception("content filtering policy")
|
|
else:
|
|
mock_response = litellm.ModelResponse(
|
|
choices=[litellm.Choices(finish_reason="content_filter")],
|
|
model="gpt-3.5-turbo",
|
|
usage=litellm.Usage(prompt_tokens=10, completion_tokens=0, total_tokens=10),
|
|
)
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude-sonnet-4-5-20250929",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-4-5-20250929",
|
|
"api_key": "",
|
|
"mock_response": mock_response,
|
|
},
|
|
},
|
|
{
|
|
"model_name": "my-fallback-model",
|
|
"litellm_params": {
|
|
"model": "openai/my-fake-model",
|
|
"api_key": "",
|
|
"mock_response": "This works!",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "my-default-fallback-model",
|
|
"litellm_params": {
|
|
"model": "openai/my-fake-model",
|
|
"api_key": "",
|
|
"mock_response": "This works 2!",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "my-general-model",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-4-5-20250929",
|
|
"api_key": "",
|
|
"mock_response": Exception("Should not have called this."),
|
|
},
|
|
},
|
|
{
|
|
"model_name": "my-context-window-model",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-4-5-20250929",
|
|
"api_key": "",
|
|
"mock_response": Exception("Should not have called this."),
|
|
},
|
|
},
|
|
],
|
|
content_policy_fallbacks=(
|
|
[{"claude-sonnet-4-5-20250929": ["my-fallback-model"]}]
|
|
if fallback_type == "model-specific"
|
|
else None
|
|
),
|
|
default_fallbacks=(
|
|
["my-default-fallback-model"] if fallback_type == "default" else None
|
|
),
|
|
)
|
|
|
|
if sync_mode is True:
|
|
response = router.completion(
|
|
model="claude-sonnet-4-5-20250929",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
else:
|
|
response = await router.acompletion(
|
|
model="claude-sonnet-4-5-20250929",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
|
|
assert response.model == "my-fake-model"
|
|
|
|
|
|
def mock_post_streaming(url: str, **kwargs: object) -> MagicMock:
|
|
response: Final = MagicMock()
|
|
response.status_code = 529
|
|
response.headers = {"Content-Type": "application/json"}
|
|
response.return_value = {"detail": "Overloaded!"}
|
|
return response
|
|
|
|
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
@pytest.mark.asyncio
|
|
async def test_anthropic_streaming_fallbacks(sync_mode):
|
|
litellm.set_verbose = True
|
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
|
|
|
|
if sync_mode:
|
|
client = HTTPHandler(concurrent_limit=1)
|
|
else:
|
|
client = AsyncHTTPHandler(concurrent_limit=1)
|
|
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "anthropic/claude-sonnet-4-5-20250929",
|
|
"litellm_params": {
|
|
"model": "anthropic/claude-sonnet-4-5-20250929",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"mock_response": "Hey, how's it going?",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"anthropic/claude-sonnet-4-5-20250929": ["gpt-3.5-turbo"]}],
|
|
num_retries=0,
|
|
)
|
|
|
|
with patch.object(client, "post", side_effect=mock_post_streaming) as mock_client:
|
|
chunks = []
|
|
if sync_mode:
|
|
response = router.completion(
|
|
model="anthropic/claude-sonnet-4-5-20250929",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
stream=True,
|
|
client=client,
|
|
)
|
|
for chunk in response:
|
|
print(chunk)
|
|
chunks.append(chunk)
|
|
else:
|
|
response = await router.acompletion(
|
|
model="anthropic/claude-sonnet-4-5-20250929",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
stream=True,
|
|
client=client,
|
|
)
|
|
async for chunk in response:
|
|
print(chunk)
|
|
chunks.append(chunk)
|
|
print(f"RETURNED response: {response}")
|
|
|
|
mock_client.assert_called_once()
|
|
print(chunks)
|
|
assert len(chunks) > 0
|
|
|
|
|
|
def test_router_fallbacks_with_custom_model_costs():
|
|
"""
|
|
Tests prod use-case where a custom model is registered with a different provider + custom costs.
|
|
|
|
Goal: make sure custom model doesn't override default model costs.
|
|
"""
|
|
|
|
default_model_info = litellm.get_model_info(model="claude-sonnet-4-5-20250929")
|
|
|
|
model_list = [
|
|
{
|
|
"model_name": "claude-sonnet-4-5-20250929",
|
|
"litellm_params": {
|
|
"model": "claude-sonnet-4-5-20250929",
|
|
"api_key": os.environ.get("ANTHROPIC_API_KEY", "fake-key"),
|
|
"input_cost_per_token": 30,
|
|
"output_cost_per_token": 60,
|
|
"mock_response": "Hello! How can I help you today?",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "claude-3-5-sonnet-aihubmix",
|
|
"litellm_params": {
|
|
"model": "openai/claude-sonnet-4-5-20250929",
|
|
"input_cost_per_token": 0.000003, # 3$/M
|
|
"output_cost_per_token": 0.000015, # 15$/M
|
|
"api_base": FAKE_OPENAI_API_BASE,
|
|
"api_key": "my-fake-key",
|
|
"mock_response": "Hello! How can I help you today?",
|
|
},
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
fallbacks=[{"claude-sonnet-4-5-20250929": ["claude-3-5-sonnet-aihubmix"]}],
|
|
)
|
|
|
|
router.completion(
|
|
model="claude-3-5-sonnet-aihubmix",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
|
|
model_info = litellm.get_model_info(model="claude-sonnet-4-5-20250929")
|
|
|
|
print(f"key: {model_info['key']}")
|
|
|
|
assert model_info["litellm_provider"] == "anthropic"
|
|
|
|
response = router.completion(
|
|
model="claude-sonnet-4-5-20250929",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
|
|
print(f"response_cost: {response._hidden_params['response_cost']}")
|
|
|
|
assert response._hidden_params["response_cost"] > 10
|
|
|
|
model_info = litellm.get_model_info(model="claude-sonnet-4-5-20250929")
|
|
|
|
print(f"key: {model_info['key']}")
|
|
|
|
assert model_info["input_cost_per_token"] == default_model_info["input_cost_per_token"]
|
|
assert model_info["output_cost_per_token"] == default_model_info["output_cost_per_token"]
|
|
|
|
|
|
def test_router_fallbacks_with_wildcard_model_name():
|
|
router = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "openai/*",
|
|
"litellm_params": {
|
|
"model": "openai/*",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
{
|
|
"model_name": "claude-3-haiku",
|
|
"litellm_params": {
|
|
"model": "claude-haiku-4-5-20251001",
|
|
"api_key": os.getenv("ANTHROPIC_API_KEY"),
|
|
"mock_response": "Hi this is claude!",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"gpt-3.5-turbo": ["claude-3-haiku"]}],
|
|
)
|
|
|
|
response = router.completion(
|
|
model="openai/gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
mock_testing_fallbacks=True,
|
|
)
|
|
|
|
print(response)
|
|
assert response["choices"][0]["message"]["content"] == "Hi this is claude!"
|
|
|
|
|
|
def test_get_fallback_model_group():
|
|
from litellm.router_utils.fallback_event_handlers import get_fallback_model_group
|
|
|
|
args = {
|
|
"fallbacks": [
|
|
{"gpt-3.5-turbo": ["claude-3-haiku"]},
|
|
{"*": ["claude-3-sonnet"]},
|
|
],
|
|
"model_group": "openai/gpt-3.5-turbo",
|
|
}
|
|
fallback_model_group, _ = get_fallback_model_group(**args)
|
|
assert fallback_model_group == ["claude-3-haiku"]
|
|
|
|
|
|
@pytest.mark.parametrize("expected_attempted_fallbacks", [0])
|
|
@pytest.mark.asyncio
|
|
async def test_router_attempted_fallbacks_in_response(
|
|
expected_attempted_fallbacks: int,
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "working-fake-endpoint",
|
|
"litellm_params": {
|
|
"model": "openai/working-fake-endpoint",
|
|
"api_key": "test-key",
|
|
"mock_response": "Hello",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "badly-configured-openai-endpoint",
|
|
"litellm_params": {
|
|
"model": "openai/my-fake-model",
|
|
"api_base": "https://example.invalid",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"badly-configured-openai-endpoint": ["working-fake-endpoint"]}],
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="working-fake-endpoint",
|
|
messages=[{"role": "user", "content": "Hey, how's it going?"}],
|
|
)
|
|
|
|
assert (
|
|
response._hidden_params["additional_headers"]["x-litellm-attempted-fallbacks"] == expected_attempted_fallbacks
|
|
)
|
|
|
|
|
|
class MyCustomHandler(CustomLogger):
|
|
success: bool = False
|
|
failure: bool = False
|
|
previous_models: int = 0
|
|
|
|
def log_pre_api_call(self, model, messages, kwargs):
|
|
print(f"Pre-API Call")
|
|
print(
|
|
f"previous_models: {kwargs['litellm_params']['metadata'].get('previous_models', None)}"
|
|
)
|
|
self.previous_models = len(
|
|
kwargs["litellm_params"]["metadata"].get("previous_models", [])
|
|
) # {"previous_models": [{"model": litellm_model_name, "exception_type": AuthenticationError, "exception_string": <complete_traceback>}]}
|
|
print(f"self.previous_models: {self.previous_models}")
|
|
|
|
def log_post_api_call(self, kwargs, response_obj, start_time, end_time):
|
|
print(
|
|
f"Post-API Call - response object: {response_obj}; model: {kwargs['model']}"
|
|
)
|
|
|
|
def log_stream_event(self, kwargs, response_obj, start_time, end_time):
|
|
print(f"On Stream")
|
|
|
|
def async_log_stream_event(self, kwargs, response_obj, start_time, end_time):
|
|
print(f"On Stream")
|
|
|
|
def log_success_event(self, kwargs, response_obj, start_time, end_time):
|
|
print(f"On Success")
|
|
|
|
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
|
|
print(f"On Success")
|
|
|
|
def log_failure_event(self, kwargs, response_obj, start_time, end_time):
|
|
print(f"On Failure")
|
|
|
|
|
|
def test_async_fallbacks(caplog, respx_mock: respx.MockRouter, monkeypatch):
|
|
monkeypatch.setattr(litellm, "set_verbose", False)
|
|
monkeypatch.setattr(litellm, "success_callback", [])
|
|
monkeypatch.setattr(litellm, "failure_callback", [])
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
respx_mock.post("https://api.openai.com/v1/chat/completions").mock(
|
|
return_value=httpx.Response(
|
|
401,
|
|
json={
|
|
"error": {
|
|
"message": "Incorrect API key provided: bad-key.",
|
|
"type": "invalid_request_error",
|
|
"code": "invalid_api_key",
|
|
}
|
|
},
|
|
)
|
|
)
|
|
verbose_router_logger.setLevel(level=logging.INFO)
|
|
verbose_logger.setLevel(logging.CRITICAL + 1)
|
|
verbose_proxy_logger.setLevel(logging.CRITICAL + 1)
|
|
model_list = [
|
|
{
|
|
"model_name": "azure/gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "azure/gpt-4.1-mini",
|
|
"api_key": os.getenv("AZURE_AI_API_KEY"),
|
|
"api_version": os.getenv("AZURE_API_VERSION"),
|
|
"api_base": os.getenv("AZURE_AI_API_BASE"),
|
|
"mock_response": "Hello world",
|
|
},
|
|
"tpm": 240000,
|
|
"rpm": 1800,
|
|
},
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"api_key": "bad-key",
|
|
},
|
|
"tpm": 1000000,
|
|
"rpm": 9000,
|
|
},
|
|
]
|
|
|
|
router = Router(
|
|
model_list=model_list,
|
|
fallbacks=[{"gpt-3.5-turbo": ["azure/gpt-3.5-turbo"]}],
|
|
num_retries=1,
|
|
)
|
|
|
|
user_message = "Hello, how are you?"
|
|
messages = [{"content": user_message, "role": "user"}]
|
|
|
|
async def _make_request():
|
|
try:
|
|
await router.acompletion(model="gpt-3.5-turbo", messages=messages, max_tokens=1)
|
|
router.reset()
|
|
except litellm.Timeout:
|
|
pass
|
|
except Exception as e:
|
|
pytest.fail(f"An exception occurred: {e}")
|
|
finally:
|
|
router.reset()
|
|
await close_litellm_async_clients()
|
|
|
|
asyncio.run(_make_request())
|
|
captured_logs = [rec.message for rec in caplog.records]
|
|
|
|
captured_logs = [
|
|
log
|
|
for log in captured_logs
|
|
if "Task exception was never retrieved" not in log
|
|
and "Task was destroyed but it is pending" not in log
|
|
and "get_available_deployment" not in log
|
|
and "Selected deployment for model" not in log
|
|
and "in the Langfuse queue" not in log
|
|
and "Unclosed client session" not in log
|
|
and "Unclosed connector" not in log
|
|
]
|
|
|
|
print("\n Captured caplog records - ", captured_logs)
|
|
|
|
expected_logs = [
|
|
"Falling back to model_group = azure/gpt-3.5-turbo",
|
|
"litellm.acompletion(model=azure/gpt-4.1-mini)\x1b[32m 200 OK\x1b[0m",
|
|
"Successful fallback b/w models.",
|
|
]
|
|
|
|
assert captured_logs[-3:] == expected_logs
|
|
|
|
|
|
def _fallback_router(
|
|
fallbacks: list[dict[str, list[str]]] | None = None,
|
|
default_fallbacks: list[str] | None = None,
|
|
) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/primary",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {
|
|
"model": "openai/backup",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
},
|
|
],
|
|
fallbacks=fallbacks if fallbacks is not None else [{"primary": ["backup"]}],
|
|
default_fallbacks=default_fallbacks,
|
|
num_retries=0,
|
|
)
|
|
|
|
|
|
def _fallback_chat_response(model: str, content: str) -> httpx.Response:
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "chatcmpl-fallback-migration",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": model,
|
|
"choices": [
|
|
{
|
|
"index": 0,
|
|
"message": {"role": "assistant", "content": content},
|
|
"finish_reason": "stop",
|
|
}
|
|
],
|
|
},
|
|
)
|
|
|
|
|
|
def _fallback_unavailable_response(status_code: int = 503) -> httpx.Response:
|
|
return httpx.Response(
|
|
status_code,
|
|
json={
|
|
"error": {
|
|
"message": "primary unavailable",
|
|
"type": "server_error",
|
|
"code": "service_unavailable",
|
|
}
|
|
},
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_static_fallback_routes_after_service_unavailable(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router()
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("backup", "served by backup"),
|
|
)
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "fallback prompt"}]
|
|
)
|
|
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
assert outbound_models == ("primary", "backup")
|
|
assert response.choices[0].message.content == "served by backup"
|
|
|
|
|
|
def test_dynamic_fallback_routes_sync_request(respx_mock: respx.MockRouter) -> None:
|
|
router: Final = _fallback_router(fallbacks=[])
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("backup", "served by backup"),
|
|
)
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "fallback prompt"}],
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
)
|
|
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
assert outbound_models == ("primary", "backup")
|
|
assert response.choices[0].message.content == "served by backup"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_dynamic_fallback_routes_async_request(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router(fallbacks=[])
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("backup", "served by backup"),
|
|
)
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "fallback prompt"}],
|
|
fallbacks=[{"primary": ["backup"]}],
|
|
)
|
|
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
assert outbound_models == ("primary", "backup")
|
|
assert response.choices[0].message.content == "served by backup"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_disable_fallbacks_stops_after_primary_error(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router()
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
return_value=_fallback_unavailable_response()
|
|
)
|
|
|
|
with pytest.raises(litellm.ServiceUnavailableError):
|
|
await router.acompletion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "fallback prompt"}],
|
|
disable_fallbacks=True,
|
|
)
|
|
|
|
assert route.call_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_preserves_original_messages(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router()
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("backup", "served by backup"),
|
|
)
|
|
)
|
|
messages: Final = [{"role": "user", "content": "preserve this prompt"}]
|
|
|
|
await router.acompletion(model="primary", messages=messages)
|
|
|
|
outbound_messages: Final = tuple(json.loads(call.request.content)["messages"] for call in route.calls)
|
|
assert outbound_messages == (messages, messages)
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", [True, False], ids=["sync", "async"])
|
|
@pytest.mark.asyncio
|
|
async def test_embedding_fallback_routes_after_primary_error(
|
|
sync_mode: bool, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary-embedding",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
"model_info": {"id": "primary-embedding-deployment"},
|
|
},
|
|
{
|
|
"model_name": "backup-embedding",
|
|
"litellm_params": {
|
|
"model": "openai/text-embedding-3-small",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
"model_info": {"id": "backup-embedding-deployment"},
|
|
},
|
|
],
|
|
fallbacks=[{"primary-embedding": ["backup-embedding"]}],
|
|
num_retries=0,
|
|
)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/embeddings").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(401),
|
|
httpx.Response(
|
|
200,
|
|
json={
|
|
"object": "list",
|
|
"data": [{"object": "embedding", "index": 0, "embedding": [0.1, 0.2]}],
|
|
"model": "text-embedding-3-small",
|
|
"usage": {"prompt_tokens": 1, "total_tokens": 1},
|
|
},
|
|
),
|
|
)
|
|
)
|
|
|
|
if sync_mode:
|
|
response: Final = router.embedding(model="primary-embedding", input="fallback prompt")
|
|
else:
|
|
response: Final = await router.aembedding(model="primary-embedding", input="fallback prompt")
|
|
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
assert outbound_models == ("text-embedding-3-small", "text-embedding-3-small")
|
|
assert len(response.data) == 1
|
|
assert response._hidden_params["model_id"] == "backup-embedding-deployment"
|
|
assert route.call_count == 2
|
|
|
|
|
|
def test_model_id_fallback_returns_selected_deployment(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/primary",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
"model_info": {"id": "primary-deployment"},
|
|
},
|
|
{
|
|
"model_name": "backup",
|
|
"litellm_params": {
|
|
"model": "openai/backup",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
"model_info": {"id": "deployment-123"},
|
|
}
|
|
],
|
|
fallbacks=[{"primary": ["deployment-123"]}],
|
|
num_retries=0,
|
|
)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("backup", "fallback by deployment id"),
|
|
)
|
|
)
|
|
|
|
response: Final = router.completion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "fallback prompt"}],
|
|
)
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
|
|
assert response._hidden_params["model_id"] == "deployment-123"
|
|
assert outbound_models == ("primary", "backup")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_default_fallback_serves_after_primary_error(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router(fallbacks=[], default_fallbacks=["backup"])
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("backup", "served by default fallback"),
|
|
)
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "fallback prompt"}]
|
|
)
|
|
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
assert outbound_models == ("primary", "backup")
|
|
assert response.choices[0].message.content == "served by default fallback"
|
|
|
|
|
|
def test_usage_based_routing_falls_back_after_rpm_exhaustion() -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": model_name,
|
|
"litellm_params": {
|
|
"model": "openai/gpt-4o-mini",
|
|
"api_key": "test-key",
|
|
},
|
|
"model_info": {"id": deployment_id},
|
|
"rpm": rpm,
|
|
}
|
|
for model_name, deployment_id, rpm in (
|
|
("primary", "1", 1),
|
|
("backup", "2", 1),
|
|
("limited", "3", 0),
|
|
("available", "4", 10),
|
|
)
|
|
],
|
|
fallbacks=[
|
|
{"primary": ["backup"]},
|
|
{"backup": ["limited"]},
|
|
{"limited": ["available"]},
|
|
],
|
|
routing_strategy="usage-based-routing-v2",
|
|
num_retries=0,
|
|
)
|
|
responses: Final = tuple(
|
|
router.completion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "usage-based fallback"}],
|
|
mock_response="fallback response",
|
|
)
|
|
for _ in range(11)
|
|
)
|
|
|
|
assert responses[0]._hidden_params["model_id"] == "1"
|
|
assert responses[-1]._hidden_params["model_id"] == "4"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_fallback_request_does_not_retry_primary_when_retries_are_disabled(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "primary",
|
|
"litellm_params": {
|
|
"model": "openai/primary",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
},
|
|
}
|
|
],
|
|
num_retries=0,
|
|
)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
return_value=_fallback_unavailable_response()
|
|
)
|
|
|
|
with pytest.raises(litellm.ServiceUnavailableError):
|
|
await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "retry control"}]
|
|
)
|
|
|
|
assert route.call_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_default_and_model_fallbacks_do_not_repeat_failed_models(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router(default_fallbacks=["primary"])
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
_fallback_unavailable_response(401),
|
|
_fallback_unavailable_response(),
|
|
_fallback_chat_response("primary", "unexpected repeat"),
|
|
)
|
|
)
|
|
|
|
with pytest.raises(litellm.AuthenticationError):
|
|
await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "fallback prompt"}]
|
|
)
|
|
|
|
outbound_models: Final = tuple(json.loads(call.request.content)["model"] for call in route.calls)
|
|
assert outbound_models == ("primary", "backup")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unknown_default_fallback_raises_after_primary_failure(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
monkeypatch.setattr(litellm, "expose_router_debug_in_errors", True)
|
|
router: Final = _fallback_router(fallbacks=[], default_fallbacks=["missing"])
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
return_value=_fallback_unavailable_response()
|
|
)
|
|
|
|
with pytest.raises(litellm.ServiceUnavailableError) as exc_info:
|
|
await router.acompletion(
|
|
model="primary", messages=[{"role": "user", "content": "fallback prompt"}]
|
|
)
|
|
|
|
assert route.call_count == 1
|
|
assert "missing" in str(exc_info.value)
|
|
|
|
|
|
def _context_error_response_stream(
|
|
*, event_type: str = "error", preceding_event: dict[str, object] | None = None, completed: bool = False
|
|
) -> httpx.Response:
|
|
response: Final = {
|
|
"id": "resp_backup" if completed else "resp_primary",
|
|
"object": "response",
|
|
"created_at": 1700000000,
|
|
"model": "backup" if completed else "primary",
|
|
"status": "completed" if completed else "in_progress",
|
|
"output": [],
|
|
"parallel_tool_calls": True,
|
|
"tool_choice": "auto",
|
|
"tools": [],
|
|
"temperature": 1.0,
|
|
"top_p": 1.0,
|
|
}
|
|
error: Final = {
|
|
"type": "invalid_request_error",
|
|
"code": "context_length_exceeded",
|
|
"message": "Your input exceeds the context window of this model. Please adjust your input and try again.",
|
|
}
|
|
terminal: Final = (
|
|
{"type": "response.completed", "response": response, "sequence_number": 2}
|
|
if completed
|
|
else {"type": event_type, "error": error, "sequence_number": 2}
|
|
if event_type == "error"
|
|
else {"type": event_type, "response": {**response, "status": "failed", "error": error}, "sequence_number": 2}
|
|
)
|
|
events: Final = (
|
|
{"type": "response.created", "response": response, "sequence_number": 0},
|
|
*((preceding_event,) if preceding_event is not None else ()),
|
|
terminal,
|
|
)
|
|
return httpx.Response(
|
|
200,
|
|
headers={"content-type": "text/event-stream"},
|
|
content="".join(f"data: {json.dumps(event)}\n\n" for event in events).encode(),
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("event_type", ("error", "response.failed"))
|
|
@pytest.mark.parametrize("context_fallback", (False, True))
|
|
async def test_responses_context_error_uses_the_configured_fallback_before_output(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, event_type: str, context_fallback: bool
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router(fallbacks=[{"primary": ["missing"]}] if context_fallback else None)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/responses").mock(
|
|
side_effect=(
|
|
_context_error_response_stream(event_type=event_type),
|
|
_context_error_response_stream(completed=True),
|
|
)
|
|
)
|
|
stream: Final = await router.aresponses(
|
|
model="primary",
|
|
input="oversized prompt",
|
|
stream=True,
|
|
context_window_fallbacks=[{"primary": ["backup"]}] if context_fallback else None,
|
|
)
|
|
events: Final = [event async for event in stream]
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("primary", "backup")
|
|
assert tuple(event.type for event in events) == ("response.created", "response.completed")
|
|
assert events[0].response.id == events[-1].response.id
|
|
assert events[-1].response.model == "backup"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("barrier", ("text", "tool", "disabled", "unconfigured"))
|
|
async def test_responses_context_error_does_not_restart_committed_or_disabled_streams(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, barrier: str
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router(fallbacks=[] if barrier == "unconfigured" else None)
|
|
preceding: Final = {
|
|
"text": {
|
|
"type": "response.output_text.delta",
|
|
"delta": "already visible",
|
|
"item_id": "msg_primary",
|
|
"output_index": 0,
|
|
"content_index": 0,
|
|
"sequence_number": 1,
|
|
},
|
|
"tool": {
|
|
"type": "response.output_item.added",
|
|
"item": {
|
|
"type": "function_call", "id": "fc_primary", "call_id": "call_primary",
|
|
"name": "lookup", "arguments": "",
|
|
},
|
|
"output_index": 0,
|
|
"sequence_number": 1,
|
|
},
|
|
}.get(barrier)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/responses").mock(
|
|
return_value=_context_error_response_stream(preceding_event=preceding)
|
|
)
|
|
stream: Final = await router.aresponses(
|
|
model="primary", input="oversized prompt", stream=True, disable_fallbacks=barrier == "disabled"
|
|
)
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
async for _ in stream:
|
|
pass
|
|
assert route.call_count == 1
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize("sync_mode", (False, True))
|
|
@pytest.mark.parametrize("emission", ("role", "text", "tool"))
|
|
async def test_chat_context_error_falls_back_after_role_only_but_not_after_output(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, sync_mode: bool, emission: str
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _fallback_router(fallbacks=[{"primary": ["missing"]}])
|
|
delta: Final = (
|
|
{"role": "assistant", "tool_calls": [{
|
|
"index": 0, "id": "call_primary", "type": "function", "function": {"name": "lookup", "arguments": ""}
|
|
}]}
|
|
if emission == "tool"
|
|
else {"role": "assistant", "content": "visible" if emission == "text" else ""}
|
|
)
|
|
first_chunk: Final = {
|
|
"id": "chatcmpl-primary", "object": "chat.completion.chunk", "created": 1700000000, "model": "primary",
|
|
"choices": [{"index": 0, "delta": delta}],
|
|
}
|
|
error: Final = {"error": {
|
|
"type": "invalid_request_error", "code": "context_length_exceeded",
|
|
"message": "This model's maximum context length is 128000 tokens. Your messages resulted in 130000 tokens.",
|
|
}}
|
|
backup: Final = {
|
|
"id": "chatcmpl-backup", "object": "chat.completion.chunk", "created": 1700000000, "model": "backup",
|
|
"choices": [{"index": 0, "delta": {"role": "assistant", "content": "backup answer"}, "finish_reason": "stop"}],
|
|
}
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
httpx.Response(200, content=f"data: {json.dumps(first_chunk)}\n\ndata: {json.dumps(error)}\n\n".encode()),
|
|
httpx.Response(200, content=f"data: {json.dumps(backup)}\n\ndata: [DONE]\n\n".encode()),
|
|
)
|
|
)
|
|
request: Final = {
|
|
"model": "primary", "messages": [{"role": "user", "content": "oversized prompt"}], "stream": True,
|
|
"context_window_fallbacks": [{"primary": ["backup"]}],
|
|
}
|
|
stream: Final = router.completion(**request) if sync_mode else await router.acompletion(**request)
|
|
if emission != "role":
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
tuple(stream) if sync_mode else tuple([chunk async for chunk in stream])
|
|
assert route.call_count == 1
|
|
return
|
|
chunks: Final = tuple(stream) if sync_mode else tuple([chunk async for chunk in stream])
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("primary", "backup")
|
|
assert "".join(chunk.choices[0].delta.content or "" for chunk in chunks if chunk.choices) == "backup answer"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"wrapped,has_output,generated_content,disqualifying,recoverable",
|
|
(
|
|
(False, False, "", False, True),
|
|
(False, True, "", False, False),
|
|
(True, False, "", False, True),
|
|
(True, True, "", False, False),
|
|
(True, False, "already visible", False, False),
|
|
(True, False, "", True, False),
|
|
),
|
|
)
|
|
def test_context_stream_fallback_obeys_all_output_receipts(
|
|
wrapped: bool, has_output: bool, generated_content: str, disqualifying: bool, recoverable: bool
|
|
) -> None:
|
|
from litellm.exceptions import MidStreamFallbackError
|
|
from litellm.router_rust.invoke.streaming import _context_window_stream_fallback_error # noqa: PLC2701
|
|
|
|
original: Final = litellm.ContextWindowExceededError(message="too long", model="primary", llm_provider="openai")
|
|
error: Final = (
|
|
MidStreamFallbackError(
|
|
message="too long", model="primary", llm_provider="openai", original_exception=original,
|
|
is_pre_first_chunk=True, generated_content=generated_content, emitted_disqualifying_content=disqualifying,
|
|
)
|
|
if wrapped
|
|
else original
|
|
)
|
|
if not recoverable:
|
|
with pytest.raises(litellm.ContextWindowExceededError) as raised:
|
|
_context_window_stream_fallback_error(error, "primary", "openai", has_output)
|
|
assert raised.value is original
|
|
return
|
|
recovered: Final = _context_window_stream_fallback_error(error, "primary", "openai", has_output)
|
|
assert recovered.original_exception is original
|
|
assert recovered.is_pre_first_chunk is True
|
|
if wrapped:
|
|
assert recovered is error
|
|
|
|
|
|
def test_context_stream_fallback_preserves_other_errors_for_existing_continuation_policy() -> None:
|
|
from litellm.exceptions import MidStreamFallbackError
|
|
from litellm.router_rust.invoke.streaming import _context_window_stream_fallback_error # noqa: PLC2701
|
|
|
|
error: Final = MidStreamFallbackError(
|
|
message="provider unavailable", model="primary", llm_provider="openai", generated_content="already visible"
|
|
)
|
|
assert _context_window_stream_fallback_error(error, "primary", "openai", True) is error
|
|
|
|
|
|
def _context_fallback_precedence_router(context_key: str | None, ordered: bool) -> Router:
|
|
deployments: Final = (
|
|
("primary", "primary", 1),
|
|
("primary" if ordered else "unused", "ordered", 2),
|
|
("generic", "generic", 1),
|
|
("context", "context", 1),
|
|
)
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": group,
|
|
"litellm_params": {
|
|
"model": f"openai/{model}",
|
|
"api_key": "test-key",
|
|
"api_base": "https://fallback-migration.local/v1",
|
|
"order": order,
|
|
},
|
|
}
|
|
for group, model, order in deployments
|
|
],
|
|
fallbacks=[{"primary": ["generic"]}],
|
|
context_window_fallbacks=None if context_key is None else [{context_key: ["context"]}],
|
|
num_retries=0,
|
|
disable_cooldowns=True,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", (False, True))
|
|
@pytest.mark.parametrize(
|
|
("context_key", "ordered", "flat_fallbacks", "expected_model"),
|
|
(
|
|
(None, True, False, "ordered"),
|
|
("unrelated", True, False, "ordered"),
|
|
("unrelated", False, False, "generic"),
|
|
("primary", True, False, "context"),
|
|
("*", True, False, "context"),
|
|
("primary", True, True, "context"),
|
|
("primary", False, True, "context"),
|
|
),
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_context_fallback_precedence_is_specific_to_the_requested_group(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
sync_mode: bool,
|
|
context_key: str | None,
|
|
ordered: bool,
|
|
flat_fallbacks: bool,
|
|
expected_model: str,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _context_fallback_precedence_router(context_key, ordered)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
httpx.Response(
|
|
400,
|
|
json={
|
|
"error": {
|
|
"message": "This model's maximum context length is 10 tokens",
|
|
"type": "invalid_request_error",
|
|
"code": "context_length_exceeded",
|
|
}
|
|
},
|
|
),
|
|
_fallback_chat_response(expected_model, f"served by {expected_model}"),
|
|
)
|
|
)
|
|
|
|
response: Final = (
|
|
router.completion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "long prompt"}],
|
|
fallbacks=["generic"] if flat_fallbacks else [{"primary": ["generic"]}],
|
|
)
|
|
if sync_mode
|
|
else await router.acompletion(
|
|
model="primary",
|
|
messages=[{"role": "user", "content": "long prompt"}],
|
|
fallbacks=["generic"] if flat_fallbacks else [{"primary": ["generic"]}],
|
|
)
|
|
)
|
|
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("primary", expected_model)
|
|
assert response.choices[0].message.content == f"served by {expected_model}"
|
|
|
|
|
|
@pytest.mark.parametrize("matching_context", (False, True))
|
|
@pytest.mark.asyncio
|
|
async def test_context_fallback_exhaustion_preserves_the_selected_chain(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
matching_context: bool,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _context_fallback_precedence_router("primary" if matching_context else "unrelated", True)
|
|
refusal: Final = httpx.Response(
|
|
400,
|
|
json={"error": {"message": "This model's maximum context length is 10 tokens"}},
|
|
)
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(refusal, refusal, _fallback_chat_response("generic", "generic answer"))
|
|
)
|
|
|
|
if matching_context:
|
|
with pytest.raises(litellm.ContextWindowExceededError):
|
|
await router.acompletion(model="primary", messages=[{"role": "user", "content": "long prompt"}])
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("primary", "context")
|
|
return
|
|
|
|
response: Final = await router.acompletion(model="primary", messages=[{"role": "user", "content": "long prompt"}])
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("primary", "ordered", "generic")
|
|
assert response.choices[0].message.content == "generic answer"
|
|
|
|
|
|
@pytest.mark.parametrize("sync_mode", (False, True))
|
|
@pytest.mark.parametrize("context_error", (False, True))
|
|
@pytest.mark.parametrize("backup_status", (429, 503))
|
|
@pytest.mark.asyncio
|
|
async def test_ordered_fallback_failures_preserve_the_original_context_error(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
sync_mode: bool,
|
|
context_error: bool,
|
|
backup_status: int,
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
router: Final = _context_fallback_precedence_router("unrelated", True)
|
|
original_message: Final = (
|
|
"This model's maximum context length is 10 tokens" if context_error else "primary unavailable"
|
|
)
|
|
backup_error: Final = litellm.RateLimitError if backup_status == 429 else litellm.ServiceUnavailableError
|
|
expected_error: Final = litellm.ContextWindowExceededError if context_error else backup_error
|
|
route: Final = respx_mock.post("https://fallback-migration.local/v1/chat/completions").mock(
|
|
side_effect=(
|
|
httpx.Response(400 if context_error else 503, json={"error": {"message": original_message}}),
|
|
httpx.Response(backup_status, json={"error": {"message": "ordered deployment unavailable"}}),
|
|
httpx.Response(backup_status, json={"error": {"message": "generic deployment unavailable"}}),
|
|
)
|
|
)
|
|
|
|
request: Final = (
|
|
asyncio.to_thread(router.completion, model="primary", messages=[{"role": "user", "content": "long prompt"}])
|
|
if sync_mode
|
|
else router.acompletion(model="primary", messages=[{"role": "user", "content": "long prompt"}])
|
|
)
|
|
with pytest.raises(expected_error) as raised:
|
|
await request
|
|
|
|
assert tuple(json.loads(call.request.content)["model"] for call in route.calls) == ("primary", "ordered", "generic")
|
|
assert raised.value.status_code == (400 if context_error else backup_status)
|
|
assert (original_message if context_error else "generic deployment unavailable") in str(raised.value)
|
|
|
|
|
|
_CALLER_ANTHROPIC_KEY: Final = "sk-ant-api03-caller-key"
|
|
_AWS_DEPLOYMENT_CREDENTIALS: Final = {
|
|
"aws_access_key_id": "AKIAEXAMPLEEXAMPLE00",
|
|
"aws_secret_access_key": "example-secret",
|
|
"aws_region_name": "us-east-1",
|
|
}
|
|
_AWS_FALLBACK_TARGETS: Final = {
|
|
"bedrock": ("bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0", "bedrock-runtime.us-east-1.amazonaws.com"),
|
|
"bedrock_mantle": ("bedrock_mantle/openai.gpt-oss-120b", "bedrock-mantle.us-east-1.api.aws"),
|
|
}
|
|
|
|
|
|
def _anthropic_overloaded() -> httpx.Response:
|
|
return httpx.Response(529, json={"type": "error", "error": {"type": "overloaded_error", "message": "overloaded"}})
|
|
|
|
|
|
def _anthropic_answer() -> httpx.Response:
|
|
return httpx.Response(
|
|
200,
|
|
json={
|
|
"id": "msg_fallback",
|
|
"type": "message",
|
|
"role": "assistant",
|
|
"model": "claude-haiku-4-5",
|
|
"content": [{"type": "text", "text": "served by anthropic"}],
|
|
"stop_reason": "end_turn",
|
|
"stop_sequence": None,
|
|
"usage": {"input_tokens": 1, "output_tokens": 1},
|
|
},
|
|
)
|
|
|
|
|
|
def _aws_answer(target: str) -> httpx.Response:
|
|
if target == "bedrock":
|
|
return httpx.Response(200, content=CONVERSE_ANSWER, headers={"content-type": "application/json"})
|
|
return _fallback_chat_response("openai.gpt-oss-120b", "served by mantle")
|
|
|
|
|
|
def _aws_unavailable() -> httpx.Response:
|
|
return httpx.Response(500, json={"message": "bedrock unavailable"})
|
|
|
|
|
|
def _anthropic_to_aws_router(target: str, fallbacks: list[dict[str, list[object]]], **settings: object) -> Router:
|
|
anthropic_params: Final = {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"}
|
|
return Router(
|
|
model_list=[
|
|
{"model_name": "claude", "litellm_params": dict(anthropic_params), "model_info": {"max_input_tokens": 8}},
|
|
{"model_name": "claude-backup", "litellm_params": dict(anthropic_params)},
|
|
{
|
|
"model_name": "claude-aws",
|
|
"litellm_params": {"model": _AWS_FALLBACK_TARGETS[target][0], **_AWS_DEPLOYMENT_CREDENTIALS},
|
|
},
|
|
],
|
|
fallbacks=fallbacks,
|
|
num_retries=0,
|
|
**settings,
|
|
)
|
|
|
|
|
|
def _carries_caller_key(request: httpx.Request) -> bool:
|
|
return any(_CALLER_ANTHROPIC_KEY in value for value in request.headers.values())
|
|
|
|
|
|
@pytest.fixture
|
|
def _aws_without_env_bearers_or_endpoints(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
for name in (
|
|
"AWS_BEARER_TOKEN_BEDROCK",
|
|
"BEDROCK_MANTLE_API_KEY",
|
|
"AWS_BEDROCK_RUNTIME_ENDPOINT",
|
|
"BEDROCK_MANTLE_API_BASE",
|
|
):
|
|
monkeypatch.delenv(name, raising=False)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize("target", ("bedrock", "bedrock_mantle"))
|
|
async def test_anthropic_fallback_onto_aws_signs_with_the_deployment_credentials_not_the_caller_key(
|
|
respx_mock: respx.MockRouter, target: str
|
|
) -> None:
|
|
router: Final = _anthropic_to_aws_router(target, [{"claude": ["claude-aws"]}])
|
|
anthropic: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock(
|
|
return_value=_anthropic_overloaded()
|
|
)
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS[target][1]).mock(return_value=_aws_answer(target))
|
|
|
|
await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert anthropic.calls[0].request.headers["x-api-key"] == _CALLER_ANTHROPIC_KEY
|
|
sent_to_aws: Final = aws.calls[0].request
|
|
assert sent_to_aws.headers["authorization"].startswith("AWS4-HMAC-SHA256 "), sent_to_aws.headers
|
|
assert not _carries_caller_key(sent_to_aws), sent_to_aws.headers
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_anthropic_fallback_after_an_aws_hop_still_sends_the_caller_key_to_anthropic(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
router: Final = _anthropic_to_aws_router("bedrock", [{"claude": ["claude-aws", "claude-backup"]}])
|
|
anthropic: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock(
|
|
side_effect=(_anthropic_overloaded(), _anthropic_answer())
|
|
)
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_unavailable())
|
|
|
|
response: Final = await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert response.choices[0].message.content == "served by anthropic"
|
|
assert aws.calls[0].request.headers["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
assert tuple(call.request.headers["x-api-key"] for call in anthropic.calls) == (
|
|
_CALLER_ANTHROPIC_KEY,
|
|
_CALLER_ANTHROPIC_KEY,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_context_window_fallback_that_never_reached_anthropic_still_leaves_its_key_behind(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
router: Final = _anthropic_to_aws_router(
|
|
"bedrock", [], enable_pre_call_checks=True, context_window_fallbacks=[{"claude": ["claude-aws"]}]
|
|
)
|
|
anthropic: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_answer())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "a prompt far longer than the eight tokens claude accepts " * 4}],
|
|
api_key=_CALLER_ANTHROPIC_KEY,
|
|
)
|
|
|
|
assert anthropic.call_count == 0
|
|
assert aws.calls[0].request.headers["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
assert not _carries_caller_key(aws.calls[0].request)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_aws_bearer_sent_to_bedrock_is_kept_on_a_bedrock_mantle_fallback(respx_mock: respx.MockRouter) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "bedrock-primary",
|
|
"litellm_params": {"model": _AWS_FALLBACK_TARGETS["bedrock"][0], **_AWS_DEPLOYMENT_CREDENTIALS},
|
|
},
|
|
{
|
|
"model_name": "mantle",
|
|
"litellm_params": {"model": _AWS_FALLBACK_TARGETS["bedrock_mantle"][0], **_AWS_DEPLOYMENT_CREDENTIALS},
|
|
},
|
|
],
|
|
fallbacks=[{"bedrock-primary": ["mantle"]}],
|
|
num_retries=0,
|
|
)
|
|
bedrock: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_unavailable())
|
|
mantle: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock_mantle"][1]).mock(
|
|
return_value=_aws_answer("bedrock_mantle")
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="bedrock-primary", messages=[{"role": "user", "content": "hi"}], api_key="caller-aws-bearer"
|
|
)
|
|
|
|
assert (
|
|
bedrock.calls[0].request.headers["authorization"],
|
|
mantle.calls[0].request.headers["authorization"],
|
|
) == ("Bearer caller-aws-bearer", "Bearer caller-aws-bearer")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_dict_fallback_target_api_key_is_sent_to_aws_in_place_of_the_caller_key(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
router: Final = _anthropic_to_aws_router(
|
|
"bedrock", [{"claude": [{"model": "claude-aws", "api_key": "target-key"}]}]
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert aws.calls[0].request.headers["authorization"] == "Bearer target-key"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_caller_api_base_keeps_the_caller_key_on_an_aws_fallback(respx_mock: respx.MockRouter) -> None:
|
|
router: Final = _anthropic_to_aws_router("bedrock", [{"claude": ["claude-aws"]}])
|
|
gateway: Final = respx_mock.post(host="caller-gateway.local").mock(
|
|
side_effect=(_anthropic_overloaded(), _aws_answer("bedrock"))
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key=_CALLER_ANTHROPIC_KEY,
|
|
api_base="https://caller-gateway.local",
|
|
)
|
|
|
|
assert gateway.calls[1].request.headers["authorization"] == f"Bearer {_CALLER_ANTHROPIC_KEY}"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("forwarding_scope", "x_app_at_fallback", "expected_headers"),
|
|
(
|
|
pytest.param(["claude"], "cli", (None, False, "trace-1"), id="fallback-group-out-of-scope"),
|
|
pytest.param(["claude", "claude-aws"], "cli", ("cli", False, "trace-1"), id="fallback-group-in-scope"),
|
|
pytest.param(["claude"], "set-by-hook", ("set-by-hook", False, "trace-1"), id="value-replaced-after-stamp"),
|
|
pytest.param(None, "cli", (None, False, "trace-1"), id="no-forwarding-scope-left"),
|
|
),
|
|
)
|
|
async def test_aws_fallback_drops_only_the_proxy_forwarded_headers_its_group_may_not_receive(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
forwarding_scope: list[str] | None,
|
|
x_app_at_fallback: str,
|
|
expected_headers: tuple[str | None, bool, str | None],
|
|
) -> None:
|
|
from litellm.types.router import ForwardedClientHeaders, ModelGroupSettings
|
|
|
|
monkeypatch.setattr(
|
|
litellm, "model_group_settings", ModelGroupSettings(forward_client_headers_to_llm_api=forwarding_scope)
|
|
)
|
|
stamp: Final = ForwardedClientHeaders.of(
|
|
{"x-app": "cli", "x-api-key": _CALLER_ANTHROPIC_KEY}, model_group_scoped=True
|
|
).model_dump()
|
|
router: Final = _anthropic_to_aws_router("bedrock", [{"claude": ["claude-aws"]}])
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key=_CALLER_ANTHROPIC_KEY,
|
|
headers={"x-app": x_app_at_fallback, "x-api-key": _CALLER_ANTHROPIC_KEY, "x-trace": "trace-1"},
|
|
proxy_server_request={"forwarded_client_headers": stamp},
|
|
)
|
|
|
|
sent: Final = aws.calls[0].request.headers
|
|
assert (sent.get("x-app"), "x-api-key" in sent, sent.get("x-trace")) == expected_headers
|
|
assert sent["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_blank_caller_key_still_signs_an_aws_fallback_instead_of_using_the_env_bearer(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "env-bearer")
|
|
router: Final = _anthropic_to_aws_router("bedrock", [{"claude": ["claude-aws"]}])
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(model="claude", messages=[{"role": "user", "content": "hi"}], api_key="")
|
|
|
|
assert aws.calls[0].request.headers["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize("base_field", ("api_base", "base_url", "aws_bedrock_runtime_endpoint"))
|
|
async def test_dict_fallback_target_base_url_without_a_key_keeps_the_caller_key(
|
|
respx_mock: respx.MockRouter, base_field: str
|
|
) -> None:
|
|
router: Final = _anthropic_to_aws_router(
|
|
"bedrock", [{"claude": [{"model": "claude-aws", base_field: "https://target-gateway.local"}]}]
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
gateway: Final = respx_mock.post(host="target-gateway.local").mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert gateway.calls[0].request.headers["authorization"] == f"Bearer {_CALLER_ANTHROPIC_KEY}"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize("carrier", ("request", "deployment"))
|
|
@pytest.mark.parametrize(
|
|
("caller_key", "expected_authorization"),
|
|
(
|
|
pytest.param(_CALLER_ANTHROPIC_KEY, f"Bearer {_CALLER_ANTHROPIC_KEY}", id="caller-key-outranks-the-header"),
|
|
pytest.param(None, "Bearer gateway-token", id="header-without-a-caller-key"),
|
|
),
|
|
)
|
|
async def test_explicit_authorization_header_keeps_its_precedence_on_an_aws_fallback(
|
|
respx_mock: respx.MockRouter, carrier: str, caller_key: str | None, expected_authorization: str
|
|
) -> None:
|
|
explicit: Final = {"extra_headers": {"Authorization": "Bearer gateway-token"}}
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{
|
|
"model_name": "claude-aws",
|
|
"litellm_params": {
|
|
"model": _AWS_FALLBACK_TARGETS["bedrock"][0],
|
|
**_AWS_DEPLOYMENT_CREDENTIALS,
|
|
**(explicit if carrier == "deployment" else {}),
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["claude-aws"]}],
|
|
num_retries=0,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
**({"api_key": caller_key} if caller_key is not None else {}),
|
|
**(explicit if carrier == "request" else {}),
|
|
)
|
|
|
|
assert aws.calls[0].request.headers["authorization"] == expected_authorization
|
|
|
|
|
|
class _MantleStreamCutAfterContent(httpx.AsyncByteStream):
|
|
async def __aiter__(self) -> AsyncIterator[bytes]:
|
|
chunk: Final = {
|
|
"id": "cut",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1700000000,
|
|
"model": "openai.gpt-oss-120b",
|
|
"choices": [{"index": 0, "delta": {"role": "assistant", "content": "Partial"}, "finish_reason": None}],
|
|
}
|
|
yield f"data: {json.dumps(chunk)}\n\n".encode()
|
|
raise httpx.RemoteProtocolError("peer closed connection without sending complete message body")
|
|
|
|
|
|
def _mantle_stream_answer() -> httpx.Response:
|
|
chunks: Final = (
|
|
{
|
|
"id": "backup",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1700000000,
|
|
"model": "openai.gpt-oss-120b",
|
|
"choices": [{"index": 0, "delta": {"role": "assistant", "content": " answer."}, "finish_reason": None}],
|
|
},
|
|
{
|
|
"id": "backup",
|
|
"object": "chat.completion.chunk",
|
|
"created": 1700000000,
|
|
"model": "openai.gpt-oss-120b",
|
|
"choices": [{"index": 0, "delta": {"content": ""}, "finish_reason": "stop"}],
|
|
},
|
|
)
|
|
return httpx.Response(
|
|
200,
|
|
headers={"content-type": "text/event-stream"},
|
|
text="".join(f"data: {json.dumps(chunk)}\n\n" for chunk in chunks) + "data: [DONE]\n\n",
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("mantle_auth", "expected_authorization"),
|
|
(
|
|
pytest.param({}, "AWS4-HMAC-SHA256 ", id="iam"),
|
|
pytest.param({"api_key": "mantle-bearer"}, "Bearer mantle-bearer", id="deployment-bearer"),
|
|
),
|
|
)
|
|
async def test_mid_stream_continuation_from_an_aws_hop_does_not_bring_the_caller_key_back(
|
|
respx_mock: respx.MockRouter, mantle_auth: dict[str, str], expected_authorization: str
|
|
) -> None:
|
|
mantle_params: Final = {
|
|
"model": _AWS_FALLBACK_TARGETS["bedrock_mantle"][0],
|
|
**_AWS_DEPLOYMENT_CREDENTIALS,
|
|
**mantle_auth,
|
|
}
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{"model_name": "mantle-cut", "litellm_params": dict(mantle_params)},
|
|
{
|
|
"model_name": "mantle-backup",
|
|
"litellm_params": dict(mantle_params),
|
|
"model_info": {"supports_assistant_prefill": True},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["mantle-cut"]}, {"mantle-cut": ["mantle-backup"]}],
|
|
num_retries=0,
|
|
enable_mid_stream_fallback_continuation=True,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
mantle: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock_mantle"][1]).mock(
|
|
side_effect=(
|
|
httpx.Response(200, headers={"content-type": "text/event-stream"}, stream=_MantleStreamCutAfterContent()),
|
|
_mantle_stream_answer(),
|
|
)
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY, stream=True
|
|
)
|
|
_ = [chunk async for chunk in response]
|
|
|
|
sent: Final = tuple(call.request.headers["authorization"] for call in mantle.calls)
|
|
assert tuple(header[: len(expected_authorization)] for header in sent) == (expected_authorization,) * 2
|
|
assert not any(_carries_caller_key(call.request) for call in mantle.calls)
|
|
assert json.loads(mantle.calls[1].request.content)["messages"][-1]["role"] == "assistant"
|
|
|
|
|
|
_OPAQUE_CALLER_KEY: Final = "caller-destination-key"
|
|
_SIGV4: Final = "AWS4-HMAC-SHA256 "
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.parametrize(
|
|
("caller_key", "caller_kind"),
|
|
((_CALLER_ANTHROPIC_KEY, "anthropic"), (_OPAQUE_CALLER_KEY, "opaque")),
|
|
ids=("anthropic-caller-key", "opaque-caller-key"),
|
|
)
|
|
@pytest.mark.parametrize(
|
|
("target", "deployment_key", "env_bearers", "expected_authorization"),
|
|
(
|
|
pytest.param(
|
|
"bedrock",
|
|
"gateway-key",
|
|
{},
|
|
{"anthropic": "Bearer gateway-key", "opaque": f"Bearer {_OPAQUE_CALLER_KEY}"},
|
|
id="bedrock-deployment-bearer",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
"gateway-key",
|
|
{},
|
|
{"anthropic": "Bearer gateway-key", "opaque": f"Bearer {_OPAQUE_CALLER_KEY}"},
|
|
id="mantle-deployment-bearer",
|
|
),
|
|
pytest.param(
|
|
"bedrock",
|
|
None,
|
|
{"AWS_BEARER_TOKEN_BEDROCK": "env-bearer"},
|
|
{"anthropic": "Bearer env-bearer", "opaque": f"Bearer {_OPAQUE_CALLER_KEY}"},
|
|
id="bedrock-env-bearer",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
None,
|
|
{"BEDROCK_MANTLE_API_KEY": "env-bearer"},
|
|
{"anthropic": "Bearer env-bearer", "opaque": f"Bearer {_OPAQUE_CALLER_KEY}"},
|
|
id="mantle-env-bearer",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
None,
|
|
{"AWS_BEARER_TOKEN_BEDROCK": "env-bearer"},
|
|
{"anthropic": "Bearer env-bearer", "opaque": f"Bearer {_OPAQUE_CALLER_KEY}"},
|
|
id="mantle-falls-back-to-the-bedrock-env-bearer",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
"",
|
|
{"BEDROCK_MANTLE_API_KEY": "env-bearer"},
|
|
{"anthropic": "Bearer env-bearer", "opaque": f"Bearer {_OPAQUE_CALLER_KEY}"},
|
|
id="mantle-empty-key-still-reads-its-env-bearer",
|
|
),
|
|
pytest.param(
|
|
"bedrock",
|
|
"",
|
|
{"AWS_BEARER_TOKEN_BEDROCK": "env-bearer"},
|
|
{"anthropic": _SIGV4, "opaque": _SIGV4},
|
|
id="bedrock-empty-key-is-iam",
|
|
),
|
|
pytest.param("bedrock_mantle", None, {}, {"anthropic": _SIGV4, "opaque": _SIGV4}, id="mantle-iam-only"),
|
|
),
|
|
)
|
|
async def test_anthropic_fallback_onto_aws_uses_its_own_auth_unless_a_bearer_meets_an_opaque_caller_key(
|
|
_aws_without_env_bearers_or_endpoints: None,
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
caller_key: str,
|
|
caller_kind: str,
|
|
target: str,
|
|
deployment_key: str | None,
|
|
env_bearers: dict[str, str],
|
|
expected_authorization: dict[str, str],
|
|
) -> None:
|
|
for name, value in env_bearers.items():
|
|
monkeypatch.setenv(name, value)
|
|
aws_params: Final = {
|
|
"model": _AWS_FALLBACK_TARGETS[target][0],
|
|
**_AWS_DEPLOYMENT_CREDENTIALS,
|
|
**({"api_key": deployment_key} if deployment_key is not None else {}),
|
|
}
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{"model_name": "claude-aws", "litellm_params": aws_params},
|
|
],
|
|
fallbacks=[{"claude": ["claude-aws"]}],
|
|
num_retries=0,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS[target][1]).mock(return_value=_aws_answer(target))
|
|
|
|
await router.acompletion(model="claude", messages=[{"role": "user", "content": "hi"}], api_key=caller_key)
|
|
|
|
sent: Final = aws.calls[0].request.headers["authorization"]
|
|
expected: Final = expected_authorization[caller_kind]
|
|
assert sent[: len(expected)] == expected, sent
|
|
assert not _carries_caller_key(aws.calls[0].request)
|
|
|
|
|
|
def _gateway_requiring_the_caller_bearer(target: str) -> Callable[[httpx.Request], httpx.Response]:
|
|
def answer(request: httpx.Request) -> httpx.Response:
|
|
if request.headers.get("authorization") != f"Bearer {_CALLER_ANTHROPIC_KEY}":
|
|
return httpx.Response(401, json={"message": "caller bearer required"})
|
|
return _aws_answer(target)
|
|
|
|
return answer
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("target", "endpoint_params", "endpoint_env", "expected_content"),
|
|
(
|
|
pytest.param(
|
|
"bedrock",
|
|
{"aws_bedrock_runtime_endpoint": "https://aws-gateway.local"},
|
|
{},
|
|
"hi",
|
|
id="bedrock-runtime-endpoint",
|
|
),
|
|
pytest.param("bedrock", {"api_base": "https://aws-gateway.local"}, {}, "hi", id="bedrock-api-base"),
|
|
pytest.param(
|
|
"bedrock",
|
|
{},
|
|
{"AWS_BEDROCK_RUNTIME_ENDPOINT": "https://aws-gateway.local"},
|
|
"hi",
|
|
id="bedrock-env-endpoint",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle", {"api_base": "https://aws-gateway.local/v1"}, {}, "served by mantle", id="mantle-api-base"
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
{},
|
|
{"BEDROCK_MANTLE_API_BASE": "https://aws-gateway.local/v1"},
|
|
"served by mantle",
|
|
id="mantle-env-base",
|
|
),
|
|
),
|
|
)
|
|
async def test_iam_deployment_behind_a_custom_endpoint_still_gets_the_caller_bearer(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
target: str,
|
|
endpoint_params: dict[str, str],
|
|
endpoint_env: dict[str, str],
|
|
expected_content: str,
|
|
) -> None:
|
|
for name, value in endpoint_env.items():
|
|
monkeypatch.setenv(name, value)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{
|
|
"model_name": "claude-aws",
|
|
"litellm_params": {
|
|
"model": _AWS_FALLBACK_TARGETS[target][0],
|
|
**_AWS_DEPLOYMENT_CREDENTIALS,
|
|
**endpoint_params,
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["claude-aws"]}],
|
|
num_retries=0,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
gateway: Final = respx_mock.post(host="aws-gateway.local").mock(
|
|
side_effect=_gateway_requiring_the_caller_bearer(target)
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert (response.choices[0].message.content, gateway.call_count) == (expected_content, 1)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_malformed_forwarded_header_stamp_leaves_the_caller_headers_in_place(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
from litellm.types.router import ModelGroupSettings
|
|
|
|
monkeypatch.setattr(
|
|
litellm, "model_group_settings", ModelGroupSettings(forward_client_headers_to_llm_api=["claude"])
|
|
)
|
|
router: Final = _anthropic_to_aws_router("bedrock", [{"claude": ["claude-aws"]}])
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key=_CALLER_ANTHROPIC_KEY,
|
|
headers={"x-app": "cli"},
|
|
proxy_server_request={"forwarded_client_headers": {"digests": "not-a-mapping", "model_group_scoped": True}},
|
|
)
|
|
|
|
sent: Final = aws.calls[0].request.headers
|
|
assert (sent.get("x-app"), sent["authorization"][:17]) == ("cli", "AWS4-HMAC-SHA256 ")
|
|
|
|
|
|
_MIXED_GROUP_AWS_MEMBER: Final = "bedrock/converse/anthropic.claude-haiku-4-5-20251001-v1:0"
|
|
_MIXED_GROUP_AWS_FALLBACK: Final = "bedrock/converse/anthropic.claude-sonnet-4-5-20250929-v1:0"
|
|
|
|
|
|
def _bedrock_member_fails_fallback_answers(request: httpx.Request) -> httpx.Response:
|
|
return _aws_unavailable() if "haiku" in request.url.path else _aws_answer("bedrock")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("fallback_auth", "expected_authorization"),
|
|
(
|
|
pytest.param({}, "AWS4-HMAC-SHA256 ", id="iam-fallback"),
|
|
pytest.param({"api_key": "fallback-bearer"}, "Bearer fallback-bearer", id="bearer-fallback"),
|
|
),
|
|
)
|
|
async def test_mixed_provider_group_keeps_the_caller_key_on_its_own_aws_member_and_drops_it_on_the_fallback(
|
|
respx_mock: respx.MockRouter, fallback_auth: dict[str, str], expected_authorization: str
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment", "order": 1},
|
|
},
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": _MIXED_GROUP_AWS_MEMBER, **_AWS_DEPLOYMENT_CREDENTIALS, "order": 2},
|
|
},
|
|
{
|
|
"model_name": "claude-aws",
|
|
"litellm_params": {"model": _MIXED_GROUP_AWS_FALLBACK, **_AWS_DEPLOYMENT_CREDENTIALS, **fallback_auth},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["claude-aws"]}],
|
|
num_retries=0,
|
|
disable_cooldowns=True,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(
|
|
side_effect=_bedrock_member_fails_fallback_answers
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
sent: Final = tuple(("haiku" in call.request.url.path, call.request.headers["authorization"]) for call in aws.calls)
|
|
assert len(sent) == 2, sent
|
|
assert sent[0] == (True, f"Bearer {_CALLER_ANTHROPIC_KEY}")
|
|
assert (sent[1][0], sent[1][1][: len(expected_authorization)]) == (False, expected_authorization)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("carrier", "field", "value"),
|
|
(
|
|
pytest.param("request", "api_base", None, id="request-api-base-none"),
|
|
pytest.param("request", "api_base", "", id="request-api-base-empty"),
|
|
pytest.param("target", "base_url", None, id="target-base-url-none"),
|
|
pytest.param("target", "api_base", "", id="target-api-base-empty"),
|
|
),
|
|
)
|
|
async def test_unset_endpoint_value_still_sends_the_aws_fallback_to_its_default_endpoint_with_sigv4(
|
|
respx_mock: respx.MockRouter, carrier: str, field: str, value: str | None
|
|
) -> None:
|
|
target: Final = {"model": "claude-aws", field: value} if carrier == "target" else "claude-aws"
|
|
router: Final = _anthropic_to_aws_router("bedrock", [{"claude": [target]}])
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(
|
|
model="claude",
|
|
messages=[{"role": "user", "content": "hi"}],
|
|
api_key=_CALLER_ANTHROPIC_KEY,
|
|
**({field: value} if carrier == "request" else {}),
|
|
)
|
|
|
|
assert aws.calls[0].request.headers["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
assert not _carries_caller_key(aws.calls[0].request)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("target", "endpoint_env", "endpoint_params"),
|
|
(
|
|
pytest.param(
|
|
"bedrock",
|
|
{"BEDROCK_MANTLE_API_BASE": "https://mantle-gateway.local/v1"},
|
|
{},
|
|
id="bedrock-ignores-mantle-env",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
{"AWS_BEDROCK_RUNTIME_ENDPOINT": "https://runtime-gateway.local"},
|
|
{},
|
|
id="mantle-ignores-runtime-env",
|
|
),
|
|
pytest.param(
|
|
"bedrock_mantle",
|
|
{},
|
|
{"aws_bedrock_runtime_endpoint": "https://runtime-gateway.local"},
|
|
id="mantle-ignores-runtime-param",
|
|
),
|
|
),
|
|
)
|
|
async def test_endpoint_settings_the_aws_provider_never_reads_do_not_keep_the_caller_key(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
target: str,
|
|
endpoint_env: dict[str, str],
|
|
endpoint_params: dict[str, str],
|
|
) -> None:
|
|
for name, value in endpoint_env.items():
|
|
monkeypatch.setenv(name, value)
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{
|
|
"model_name": "claude-aws",
|
|
"litellm_params": {
|
|
"model": _AWS_FALLBACK_TARGETS[target][0],
|
|
**_AWS_DEPLOYMENT_CREDENTIALS,
|
|
**endpoint_params,
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["claude-aws"]}],
|
|
num_retries=0,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS[target][1]).mock(return_value=_aws_answer(target))
|
|
|
|
await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert aws.calls[0].request.headers["authorization"].startswith("AWS4-HMAC-SHA256 ")
|
|
assert not _carries_caller_key(aws.calls[0].request)
|
|
|
|
|
|
def _gateway_answering_bedrock_with_the_caller_bearer(request: httpx.Request) -> httpx.Response:
|
|
if request.url.path.endswith("/v1/messages"):
|
|
return _anthropic_overloaded()
|
|
return _gateway_requiring_the_caller_bearer("bedrock")(request)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
@pytest.mark.parametrize(
|
|
("router_defaults", "caller_key", "served_by", "expected_key"),
|
|
(
|
|
pytest.param(
|
|
{"api_base": "https://aws-gateway.local"},
|
|
_CALLER_ANTHROPIC_KEY,
|
|
"gateway",
|
|
_CALLER_ANTHROPIC_KEY,
|
|
id="default-gateway-base",
|
|
),
|
|
pytest.param(
|
|
{"api_key": "router-default-key"}, _CALLER_ANTHROPIC_KEY, "aws", "router-default-key", id="default-bearer"
|
|
),
|
|
pytest.param(
|
|
{"api_key": "router-default-key"},
|
|
_OPAQUE_CALLER_KEY,
|
|
"aws",
|
|
_OPAQUE_CALLER_KEY,
|
|
id="default-bearer-with-an-opaque-caller-key",
|
|
),
|
|
pytest.param(
|
|
{"api_key": 123}, _CALLER_ANTHROPIC_KEY, "aws", _CALLER_ANTHROPIC_KEY, id="non-string-default-key"
|
|
),
|
|
pytest.param(
|
|
{"extra_headers": {"Authorization": "Bearer gateway-token"}},
|
|
_CALLER_ANTHROPIC_KEY,
|
|
"aws",
|
|
_CALLER_ANTHROPIC_KEY,
|
|
id="default-authorization",
|
|
),
|
|
),
|
|
)
|
|
async def test_router_defaults_decide_the_aws_endpoint_and_bearer_of_a_fallback(
|
|
respx_mock: respx.MockRouter,
|
|
router_defaults: dict[str, object],
|
|
caller_key: str,
|
|
served_by: str,
|
|
expected_key: str,
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{
|
|
"model_name": "claude-aws",
|
|
"litellm_params": {"model": _AWS_FALLBACK_TARGETS["bedrock"][0], **_AWS_DEPLOYMENT_CREDENTIALS},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["claude-aws"]}],
|
|
num_retries=0,
|
|
default_litellm_params=router_defaults,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
gateway: Final = respx_mock.post(host="aws-gateway.local").mock(
|
|
side_effect=_gateway_answering_bedrock_with_the_caller_bearer
|
|
)
|
|
aws: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock"][1]).mock(return_value=_aws_answer("bedrock"))
|
|
|
|
await router.acompletion(model="claude", messages=[{"role": "user", "content": "hi"}], api_key=caller_key)
|
|
|
|
served: Final = {"gateway": gateway, "aws": aws}[served_by].calls[-1].request
|
|
assert served.headers["authorization"] == f"Bearer {expected_key}"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_stream_recovery_from_an_external_aws_hop_of_a_mixed_group_keeps_the_caller_key_off_aws(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
mantle_params: Final = {"model": _AWS_FALLBACK_TARGETS["bedrock_mantle"][0], **_AWS_DEPLOYMENT_CREDENTIALS}
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment", "order": 1},
|
|
},
|
|
{"model_name": "claude", "litellm_params": {**mantle_params, "order": 2}},
|
|
{"model_name": "mantle-cut", "litellm_params": dict(mantle_params)},
|
|
{
|
|
"model_name": "mantle-backup",
|
|
"litellm_params": dict(mantle_params),
|
|
"model_info": {"supports_assistant_prefill": True},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["mantle-cut"]}, {"mantle-cut": ["mantle-backup"]}],
|
|
num_retries=0,
|
|
disable_cooldowns=True,
|
|
enable_mid_stream_fallback_continuation=True,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
mantle: Final = respx_mock.post(host=_AWS_FALLBACK_TARGETS["bedrock_mantle"][1]).mock(
|
|
side_effect=(
|
|
_aws_unavailable(),
|
|
httpx.Response(200, headers={"content-type": "text/event-stream"}, stream=_MantleStreamCutAfterContent()),
|
|
_mantle_stream_answer(),
|
|
)
|
|
)
|
|
|
|
response: Final = await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY, stream=True
|
|
)
|
|
_ = [chunk async for chunk in response]
|
|
|
|
sent: Final = tuple(call.request.headers["authorization"] for call in mantle.calls)
|
|
assert len(sent) == 3, sent
|
|
assert sent[0] == f"Bearer {_CALLER_ANTHROPIC_KEY}"
|
|
assert (sent[1].startswith("AWS4-HMAC-SHA256 "), sent[2].startswith("AWS4-HMAC-SHA256 ")) == (True, True)
|
|
assert json.loads(mantle.calls[2].request.content)["messages"][-1]["role"] == "assistant"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_aws_without_env_bearers_or_endpoints")
|
|
async def test_claude_platform_fallback_preserves_the_callers_key_and_workspace(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-4-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{
|
|
"model_name": "platform",
|
|
"litellm_params": {
|
|
"model": "bedrock/claude_platform/claude-sonnet-4-6",
|
|
"workspace_id": "wrkspc_test",
|
|
**_AWS_DEPLOYMENT_CREDENTIALS,
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"claude": ["platform"]}],
|
|
num_retries=0,
|
|
)
|
|
respx_mock.post("https://api.anthropic.com/v1/messages").mock(return_value=_anthropic_overloaded())
|
|
platform: Final = respx_mock.post(host="aws-external-anthropic.us-east-1.api.aws").mock(
|
|
return_value=_anthropic_answer()
|
|
)
|
|
|
|
await router.acompletion(
|
|
model="claude", messages=[{"role": "user", "content": "hi"}], api_key=_CALLER_ANTHROPIC_KEY
|
|
)
|
|
|
|
assert platform.call_count == 1
|
|
assert platform.calls[0].request.headers["x-api-key"] == _CALLER_ANTHROPIC_KEY
|
|
assert platform.calls[0].request.headers["anthropic-workspace-id"] == "wrkspc_test"
|
|
|
|
|
|
_OPENAI_GATEWAY: Final = "https://openai-gateway.local/v1"
|
|
_OPENAI_DEPLOYMENT_KEY: Final = {"api_key": "sk-openai-deployment"}
|
|
|
|
|
|
@pytest.fixture
|
|
def _openai_without_env_or_global_credentials(
|
|
_aws_without_env_bearers_or_endpoints: None, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
for name in ("OPENAI_API_KEY", "OPENAI_BASE_URL", "OPENAI_API_BASE", "ANTHROPIC_API_BASE", "ANTHROPIC_BASE_URL"):
|
|
monkeypatch.delenv(name, raising=False)
|
|
for name in ("api_key", "openai_key", "api_base"):
|
|
monkeypatch.setattr(litellm, name, None)
|
|
|
|
|
|
def _anthropic_to_openai_router(
|
|
openai_params: dict[str, str], fallback_target: object = "gpt", **settings: object
|
|
) -> Router:
|
|
return Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "claude",
|
|
"litellm_params": {"model": "anthropic/claude-haiku-5-5", "api_key": "sk-ant-deployment"},
|
|
},
|
|
{"model_name": "gpt", "litellm_params": {"model": "openai/gpt-5.6", **openai_params}},
|
|
],
|
|
fallbacks=[{"claude": [fallback_target]}, {"claude-latest": [fallback_target]}],
|
|
model_group_alias={"claude-latest": "claude"},
|
|
num_retries=0,
|
|
**settings,
|
|
)
|
|
|
|
|
|
def _anthropic_fails_openai_answers(stream: bool) -> Callable[[httpx.Request], httpx.Response]:
|
|
def answer(request: httpx.Request) -> httpx.Response:
|
|
if request.url.path.endswith("/messages"):
|
|
return _anthropic_overloaded()
|
|
return _mantle_stream_answer() if stream else _fallback_chat_response("gpt-5.6", "served by openai")
|
|
|
|
return answer
|
|
|
|
|
|
def _mock_anthropic_and_openai_hosts(respx_mock: respx.MockRouter, stream: bool) -> respx.Route:
|
|
return respx_mock.route(host__in=("api.anthropic.com", "api.openai.com", "openai-gateway.local")).mock(
|
|
side_effect=_anthropic_fails_openai_answers(stream)
|
|
)
|
|
|
|
|
|
def _openai_calls(route: respx.Route) -> tuple[tuple[str, str], ...]:
|
|
return tuple(
|
|
(call.request.url.host, call.request.headers["authorization"])
|
|
for call in route.calls
|
|
if not call.request.url.path.endswith("/messages")
|
|
)
|
|
|
|
|
|
async def _complete_after_anthropic_fails(router: Router, model: str, stream: bool, **request: object) -> None:
|
|
response: Final = await router.acompletion(
|
|
model=model, messages=[{"role": "user", "content": "hi"}], stream=stream, **request
|
|
)
|
|
if stream:
|
|
_ = [chunk async for chunk in response]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_openai_without_env_or_global_credentials")
|
|
@pytest.mark.parametrize("stream", (False, True), ids=("non-stream", "stream"))
|
|
@pytest.mark.parametrize(
|
|
("openai_params", "router_defaults", "credential_env", "credential_globals", "requested", "expected_key"),
|
|
(
|
|
pytest.param(_OPENAI_DEPLOYMENT_KEY, {}, {}, {}, "claude", "sk-openai-deployment", id="deployment-key"),
|
|
pytest.param(
|
|
_OPENAI_DEPLOYMENT_KEY, {}, {}, {}, "claude-latest", "sk-openai-deployment", id="through-a-group-alias"
|
|
),
|
|
pytest.param({}, {"api_key": "sk-openai-default"}, {}, {}, "claude", "sk-openai-default", id="router-default"),
|
|
pytest.param({}, {}, {"OPENAI_API_KEY": "sk-openai-env"}, {}, "claude", "sk-openai-env", id="env-key"),
|
|
pytest.param(
|
|
{"api_key": ""},
|
|
{},
|
|
{"OPENAI_API_KEY": "sk-openai-env"},
|
|
{},
|
|
"claude",
|
|
"sk-openai-env",
|
|
id="blank-deployment-key-reads-the-env-key",
|
|
),
|
|
pytest.param({}, {}, {}, {"api_key": "sk-openai-global"}, "claude", "sk-openai-global", id="litellm-api-key"),
|
|
pytest.param(
|
|
{}, {}, {}, {"openai_key": "sk-openai-global"}, "claude", "sk-openai-global", id="litellm-openai-key"
|
|
),
|
|
),
|
|
)
|
|
async def test_anthropic_caller_key_fallback_onto_openai_sends_the_key_the_deployment_resolves(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
stream: bool,
|
|
openai_params: dict[str, str],
|
|
router_defaults: dict[str, object],
|
|
credential_env: dict[str, str],
|
|
credential_globals: dict[str, str],
|
|
requested: str,
|
|
expected_key: str,
|
|
) -> None:
|
|
for name, value in credential_env.items():
|
|
monkeypatch.setenv(name, value)
|
|
for name, value in credential_globals.items():
|
|
monkeypatch.setattr(litellm, name, value)
|
|
router: Final = _anthropic_to_openai_router(openai_params, default_litellm_params=router_defaults)
|
|
route: Final = _mock_anthropic_and_openai_hosts(respx_mock, stream)
|
|
|
|
await _complete_after_anthropic_fails(router, requested, stream, api_key=_CALLER_ANTHROPIC_KEY)
|
|
|
|
assert _openai_calls(route) == (("api.openai.com", f"Bearer {expected_key}"),)
|
|
assert not any(_carries_caller_key(call.request) for call in route.calls[1:])
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_openai_without_env_or_global_credentials")
|
|
@pytest.mark.parametrize(
|
|
("caller_key", "openai_params", "fallback_target", "settings", "endpoint_env", "expected_call"),
|
|
(
|
|
pytest.param(
|
|
_OPAQUE_CALLER_KEY,
|
|
_OPENAI_DEPLOYMENT_KEY,
|
|
"gpt",
|
|
{},
|
|
{},
|
|
("api.openai.com", f"Bearer {_OPAQUE_CALLER_KEY}"),
|
|
id="opaque-caller-key",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
{},
|
|
"gpt",
|
|
{},
|
|
{},
|
|
("api.openai.com", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="no-credential-of-its-own",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
{},
|
|
"gpt",
|
|
{"default_litellm_params": {"api_key": 123}},
|
|
{},
|
|
("api.openai.com", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="non-string-default-key",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
{**_OPENAI_DEPLOYMENT_KEY, "api_base": _OPENAI_GATEWAY},
|
|
"gpt",
|
|
{},
|
|
{},
|
|
("openai-gateway.local", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="deployment-api-base",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
_OPENAI_DEPLOYMENT_KEY,
|
|
{"model": "gpt", "base_url": _OPENAI_GATEWAY},
|
|
{},
|
|
{},
|
|
("openai-gateway.local", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="dict-target-base-url",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
{},
|
|
"gpt",
|
|
{"default_litellm_params": {"api_key": "sk-openai-default", "api_base": _OPENAI_GATEWAY}},
|
|
{},
|
|
("openai-gateway.local", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="router-default-api-base",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
_OPENAI_DEPLOYMENT_KEY,
|
|
"gpt",
|
|
{},
|
|
{"OPENAI_BASE_URL": _OPENAI_GATEWAY},
|
|
("openai-gateway.local", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="env-base-url",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
_OPENAI_DEPLOYMENT_KEY,
|
|
"gpt",
|
|
{},
|
|
{"OPENAI_API_BASE": _OPENAI_GATEWAY},
|
|
("openai-gateway.local", f"Bearer {_CALLER_ANTHROPIC_KEY}"),
|
|
id="env-api-base",
|
|
),
|
|
pytest.param(
|
|
_CALLER_ANTHROPIC_KEY,
|
|
_OPENAI_DEPLOYMENT_KEY,
|
|
{"model": "gpt", "api_key": "sk-openai-target"},
|
|
{},
|
|
{},
|
|
("api.openai.com", "Bearer sk-openai-target"),
|
|
id="dict-target-api-key",
|
|
),
|
|
),
|
|
)
|
|
async def test_openai_fallback_keeps_the_caller_key_where_it_may_be_the_intended_credential(
|
|
respx_mock: respx.MockRouter,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
caller_key: str,
|
|
openai_params: dict[str, str],
|
|
fallback_target: object,
|
|
settings: dict[str, object],
|
|
endpoint_env: dict[str, str],
|
|
expected_call: tuple[str, str],
|
|
) -> None:
|
|
for name, value in endpoint_env.items():
|
|
monkeypatch.setenv(name, value)
|
|
router: Final = _anthropic_to_openai_router(openai_params, fallback_target, **settings)
|
|
route: Final = _mock_anthropic_and_openai_hosts(respx_mock, stream=False)
|
|
|
|
await _complete_after_anthropic_fails(router, "claude", False, api_key=caller_key)
|
|
|
|
assert _openai_calls(route) == (expected_call,)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_openai_without_env_or_global_credentials")
|
|
async def test_litellm_api_base_keeps_the_caller_key_on_an_openai_fallback(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "api_base", _OPENAI_GATEWAY)
|
|
router: Final = _anthropic_to_openai_router(_OPENAI_DEPLOYMENT_KEY)
|
|
route: Final = _mock_anthropic_and_openai_hosts(respx_mock, stream=False)
|
|
|
|
await _complete_after_anthropic_fails(router, "claude", False, api_key=_CALLER_ANTHROPIC_KEY)
|
|
|
|
assert _openai_calls(route) == (("openai-gateway.local", f"Bearer {_CALLER_ANTHROPIC_KEY}"),)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_openai_without_env_or_global_credentials")
|
|
@pytest.mark.parametrize(
|
|
("caller_key", "expected"),
|
|
(
|
|
pytest.param(_CALLER_ANTHROPIC_KEY, ("Bearer sk-openai-deployment", None), id="anthropic-caller-key"),
|
|
pytest.param(_OPAQUE_CALLER_KEY, (f"Bearer {_OPAQUE_CALLER_KEY}", _OPAQUE_CALLER_KEY), id="opaque-caller-key"),
|
|
),
|
|
)
|
|
async def test_openai_fallback_drops_the_proxy_forwarded_key_header_only_with_the_left_behind_key(
|
|
respx_mock: respx.MockRouter, caller_key: str, expected: tuple[str, str | None]
|
|
) -> None:
|
|
from litellm.types.router import ForwardedClientHeaders
|
|
|
|
stamp: Final = ForwardedClientHeaders.of({"x-api-key": caller_key}, model_group_scoped=False).model_dump()
|
|
router: Final = _anthropic_to_openai_router(_OPENAI_DEPLOYMENT_KEY)
|
|
route: Final = _mock_anthropic_and_openai_hosts(respx_mock, stream=False)
|
|
|
|
await _complete_after_anthropic_fails(
|
|
router,
|
|
"claude",
|
|
False,
|
|
api_key=caller_key,
|
|
headers={"x-api-key": caller_key},
|
|
proxy_server_request={"forwarded_client_headers": stamp},
|
|
)
|
|
|
|
sent: Final = route.calls[-1].request.headers
|
|
assert (sent["authorization"], sent.get("x-api-key")) == expected
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_openai_without_env_or_global_credentials")
|
|
async def test_explicit_authorization_header_keeps_the_forwarded_caller_key_on_an_openai_fallback(
|
|
respx_mock: respx.MockRouter,
|
|
) -> None:
|
|
from litellm.types.router import ForwardedClientHeaders
|
|
|
|
stamp: Final = ForwardedClientHeaders.of(
|
|
{"x-api-key": _CALLER_ANTHROPIC_KEY}, model_group_scoped=False
|
|
).model_dump()
|
|
router: Final = _anthropic_to_openai_router(_OPENAI_DEPLOYMENT_KEY)
|
|
route: Final = _mock_anthropic_and_openai_hosts(respx_mock, stream=False)
|
|
|
|
await _complete_after_anthropic_fails(
|
|
router,
|
|
"claude",
|
|
False,
|
|
api_key=_CALLER_ANTHROPIC_KEY,
|
|
headers={"Authorization": "Bearer gateway-token", "x-api-key": _CALLER_ANTHROPIC_KEY},
|
|
proxy_server_request={"forwarded_client_headers": stamp},
|
|
)
|
|
|
|
sent: Final = route.calls[-1].request.headers
|
|
assert (sent["authorization"], sent.get("x-api-key")) == ("Bearer gateway-token", _CALLER_ANTHROPIC_KEY)
|
|
|
|
|
|
def _primary_openai_fails(request: httpx.Request) -> httpx.Response:
|
|
if json.loads(request.content)["model"] == "gpt-5.5":
|
|
return httpx.Response(500, json={"error": {"message": "primary unavailable", "type": "server_error"}})
|
|
return _fallback_chat_response("gpt-5.6", "served by openai")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("_openai_without_env_or_global_credentials")
|
|
@pytest.mark.parametrize(
|
|
("requested", "expected_models"),
|
|
(
|
|
pytest.param("gpt", frozenset({"gpt-5.6"}), id="direct-openai-group"),
|
|
pytest.param("gpt-primary", frozenset({"gpt-5.5", "gpt-5.6"}), id="openai-to-openai-fallback"),
|
|
),
|
|
)
|
|
async def test_anthropic_shaped_caller_key_stays_on_openai_without_an_anthropic_origin(
|
|
respx_mock: respx.MockRouter, requested: str, expected_models: frozenset[str]
|
|
) -> None:
|
|
router: Final = Router(
|
|
model_list=[
|
|
{"model_name": "gpt-primary", "litellm_params": {"model": "openai/gpt-5.5", **_OPENAI_DEPLOYMENT_KEY}},
|
|
{"model_name": "gpt", "litellm_params": {"model": "openai/gpt-5.6", **_OPENAI_DEPLOYMENT_KEY}},
|
|
],
|
|
fallbacks=[{"gpt-primary": ["gpt"]}],
|
|
num_retries=0,
|
|
)
|
|
route: Final = respx_mock.post("https://api.openai.com/v1/chat/completions").mock(side_effect=_primary_openai_fails)
|
|
|
|
await _complete_after_anthropic_fails(router, requested, False, api_key=_CALLER_ANTHROPIC_KEY)
|
|
|
|
models: Final = frozenset(json.loads(call.request.content)["model"] for call in route.calls)
|
|
authorizations: Final = frozenset(call.request.headers["authorization"] for call in route.calls)
|
|
assert (models, authorizations) == (expected_models, frozenset({f"Bearer {_CALLER_ANTHROPIC_KEY}"}))
|