mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-06 02:48:13 +00:00
fix: prevent error when max_fallbacks exceeds available models (#20071)
This commit is contained in:
parent
28073af6b4
commit
6b755cfce2
2 changed files with 52 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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__}")
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue