The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Merge remote-tracking branch 'origin/litellm_internal_staging' into HEAD
mateo-berri7ed2e8acffc9
diff --cc tests/test_litellm/test_router.pyindex 388011b7d5d,84f6344be35..0a30792075c--- a/tests/test_litellm/test_router.py+++ b/tests/test_litellm/test_router.py@@@ -11532,27 -11597,136 +11597,162 @@@ class TestTierParamsTheTargetAcceptsassert accepted == {"reasoning_effort": "max"}+async def test_router_aspeech_without_voice_dispatches_ref_audio_cloning(respx_mock, monkeypatch):+ import base64++ monkeypatch.setenv("MISTRAL_API_KEY", "sk-mistral-test")+ monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)+ audio_bytes = b"RIFFfake-wav-bytes"+ respx_mock.post("https://api.mistral.ai/v1/audio/speech").respond(+ json={"audio_data": base64.b64encode(audio_bytes).decode()}+ )+ router = Router(+ model_list=[+ {+ "model_name": "voxtral-tts",+ "litellm_params": {"model": "mistral/voxtral-mini-tts-2603"},+ }+ ]+ )++ response = await router.aspeech(model="voxtral-tts", input="clone me", ref_audio="ZmFrZQ==")++ request_body = json.loads(respx_mock.calls.last.request.content)+ assert request_body == {"model": "voxtral-mini-tts-2603", "input": "clone me", "ref_audio": "ZmFrZQ=="}+ assert response.content == audio_bytes+++++ class TestPreRoutingTierDrivesFallbacks:+ """#38832: a complexity/auto router picks a tier behind the router name, but fallback+ lookup stayed on the router name, so the tier's configured chain never ran and a+ provider failure on the tier's first hop was returned to the client."""++ class _TierRouter(litellm.Router):+ async def async_pre_routing_hook(+ self, model, request_kwargs, messages=None, input=None, specific_deployment=False+ ):+ from litellm.types.router import PreRoutingHookResponse++ if model == "smart-router":+ return PreRoutingHookResponse(model="tier1", messages=messages)+ return None++ @classmethod+ def _router(cls, fallbacks) -> "litellm.Router":+ return cls._TierRouter(+ model_list=[+ {+ "model_name": "smart-router",+ "litellm_params": {"model": "openai/gpt-4o-mini", "api_key": "sk-x"},+ },+ {+ "model_name": "tier1",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "api_key": "sk-x",+ "mock_response": "litellm.RateLimitError",+ },+ },+ {+ "model_name": "backup-a",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "api_key": "sk-x",+ "mock_response": "from backup-a",+ },+ },+ {+ "model_name": "backup-b",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "api_key": "sk-x",+ "mock_response": "from backup-b",+ },+ },+ {+ "model_name": "failing-backup",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "api_key": "sk-x",+ "mock_response": "litellm.RateLimitError",+ },+ },+ {+ "model_name": "plain",+ "litellm_params": {+ "model": "openai/gpt-4o-mini",+ "api_key": "sk-x",+ "mock_response": "litellm.RateLimitError",+ },+ },+ ],+ fallbacks=fallbacks,+ num_retries=0,+ )++ @pytest.mark.asyncio+ async def test_the_selected_tier_fallback_chain_runs(self):+ router = self._router([{"tier1": ["backup-a"]}])++ response = await router.acompletion(+ model="smart-router", messages=[{"role": "user", "content": "hi"}]+ )++ assert response.choices[0].message.content == "from backup-a"++ @pytest.mark.asyncio+ async def test_a_chain_keyed_on_the_router_name_is_not_used(self):+ """The router name has no chain of its own, so nothing should rescue this call."""+ router = self._router([{"tier2": ["backup-a"]}])++ with pytest.raises(litellm.RateLimitError):+ await router.acompletion(model="smart-router", messages=[{"role": "user", "content": "hi"}])++ @pytest.mark.asyncio+ async def test_a_chain_keyed_on_the_router_name_rescues_when_no_tier_chain_exists(self):+ """The documented contract: configs keyed on the requested name keep working behind auto-routers."""+ router = self._router([{"smart-router": ["backup-a"]}])++ response = await router.acompletion(model="smart-router", messages=[{"role": "user", "content": "hi"}])++ assert response.choices[0].message.content == "from backup-a"++ @pytest.mark.asyncio+ async def test_the_tier_chain_wins_over_the_router_name_chain(self):+ router = self._router([{"tier1": ["backup-a"]}, {"smart-router": ["backup-b"]}])++ response = await router.acompletion(model="smart-router", messages=[{"role": "user", "content": "hi"}])++ assert response.choices[0].message.content == "from backup-a"++ @pytest.mark.asyncio+ async def test_a_request_without_a_pre_routing_hook_still_uses_its_own_group(self):+ router = self._router([{"tier1": ["backup-a"]}])++ response = await router.acompletion(+ model="tier1", messages=[{"role": "user", "content": "hi"}]+ )++ assert response.choices[0].message.content == "from backup-a"++ @pytest.mark.asyncio+ async def test_a_caller_cannot_pick_the_chain_by_sending_the_selection(self):+ """The metadata bucket carries caller-supplied keys, so only the hook may set the tier."""+ router = self._router([{"tier1": ["backup-a"]}])++ with pytest.raises(litellm.RateLimitError):+ await router.acompletion(+ model="plain",+ messages=[{"role": "user", "content": "hi"}],+ metadata={"pre_routing_selected_model": "tier1"},+ )++ @pytest.mark.asyncio+ async def test_each_fallback_hop_resolves_its_own_chain(self):+ """The second hop must key off the group it is running, not the tier that failed."""+ router = self._router([{"tier1": ["failing-backup"]}, {"failing-backup": ["backup-b"]}])++ response = await router.acompletion(model="smart-router", messages=[{"role": "user", "content": "hi"}])++ assert response.choices[0].message.content == "from backup-b"