fix: prevent error when max_fallbacks exceeds available models (#20071)

This commit is contained in:
Abdullah Habib Biswas 2026-02-01 04:46:07 +05:30 • committed by GitHub
parent 28073af6b4
commit 6b755cfce2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 2 deletions

View file

@ -113,8 +113,16 @@ async def run_async_fallback(
The most recent exception if all fallback model groups fail.
"""
### BASE CASE ### MAX FALLBACK DEPTH REACHED
if fallback_depth >= max_fallbacks:
### BASE CASE ### MAX FALLBACK DEPTH REACHED
if fallback_depth >= max_fallbacks:
raise original_exception
### CHECK IF MODEL GROUP LIST EXHAUSTED
if original_model_group in fallback_model_group:
fallback_group_length = len(fallback_model_group) - 1
else:
fallback_group_length = len(fallback_model_group)
if fallback_depth >= fallback_group_length:
raise original_exception
error_from_fallbacks = original_exception

View file

@ -336,3 +336,45 @@ async def test_chat_completion_bad_and_good_model():
f"Iteration {iteration + 1}: {'✓' if success else '✗'} ({time.time() - start_time:.2f}s)"
)
assert success, "Not all good model requests succeeded"
@pytest.mark.asyncio
async def test_router_fallback_exhaustion():
"""
Test for Bug 19985:
"""
from litellm import Router
import pytest
# Setup: Only ONE fallback model available
model_list = [
{
"model_name": "gpt-3.5-turbo",
"litellm_params": {"model": "openai/fake", "api_key": "bad-key"},
},
{
"model_name": "bad-model-1",
"litellm_params": {"model": "azure/fake", "api_key": "bad-key"},
}
]
# max_fallbacks=10 is much larger than the 1 fallback provided in the list
router = Router(
model_list=model_list,
fallbacks=[{"gpt-3.5-turbo": ["bad-model-1"]}],
max_fallbacks=10
)
try:
# This will fail and attempt to fallback
await router.acompletion(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "test"}]
)
except Exception as e:
# The success criteria is that we DON'T get an IndexError
assert not isinstance(e, IndexError), f"Expected API error, but got IndexError: {e}"
# Also ensure we actually hit a fallback attempt
print(f"Caught expected exception: {type(e).__name__}")