litellm/tests/local_testing/test_router_fallback_handlers.py
yuneng-jiang 6a0d03914c
test: drop the cwd-relative sys.path.insert calls from the test suite (#37802)
* test: drop the cwd-relative sys.path.insert calls from the test suite

TQ003 stands at 1,077 across 1,058 files, and 1,015 of them are the same shape:
sys.path.insert(0, os.path.abspath("../..")) and its deeper siblings. The
argument resolves against the working directory rather than the file, so from
the repo root, where every job runs pytest, it inserts the directory two levels
above the checkout. It has never pointed at litellm. The package is installed
into the environment anyway, which is what actually makes the import work, and
what the rule's message has said all along.

Removing them leaves 1,634 imports of sys and os with no remaining reference,
and those go too, except where another test module imports the name back out of
the file. The rest of TQ003 is 62 call sites that resolve against __file__ or a
variable, which are a different question and are left alone.

Collection is identical either way: 45,871 tests and the same 51 pre-existing
collection errors before and after, and ruff reports no new undefined name.

* test: drop the duplicate imports the sys.path sweep exposed to F811

* test(pre-call-utils): restore the os import the new bedrock tests need
2026-08-22 09:25:58 -07:00

309 lines
9.7 KiB
Python

import asyncio
import os
import time
import traceback
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
import litellm
from litellm import Router
from litellm.integrations.custom_logger import CustomLogger
from typing import Any, Dict, List
from litellm.router_utils.fallback_event_handlers import (
run_async_fallback,
log_success_fallback_event,
log_failure_fallback_event,
)
from tests.fake_openai_endpoint import FAKE_OPENAI_API_BASE
# Helper function to create a Router instance
def create_test_router():
return Router(
model_list=[
{
"model_name": "gpt-3.5-turbo",
"litellm_params": {
"model": "gpt-3.5-turbo",
"api_key": os.getenv("OPENAI_API_KEY"),
},
},
{
"model_name": "gpt-4",
"litellm_params": {
"model": "gpt-4",
"api_key": os.getenv("OPENAI_API_KEY"),
},
},
],
fallbacks=[{"gpt-3.5-turbo": ["gpt-4"]}],
)
def create_test_router_2():
return Router(
model_list=[
{
"model_name": "gpt-3.5-turbo",
"litellm_params": {
"model": "gpt-3.5-turbo",
"api_key": os.getenv("OPENAI_API_KEY"),
},
},
{
"model_name": "gpt-4",
"litellm_params": {
"model": "gpt-4",
"api_key": "very-fake-key",
},
},
{
"model_name": "fake-openai-endpoint-2",
"litellm_params": {
"model": "openai/fake-openai-endpoint-2",
"api_key": "working-key-since-this-is-fake-endpoint",
"api_base": FAKE_OPENAI_API_BASE,
},
},
],
)
@pytest.mark.parametrize(
"function_name",
["_acompletion", "_atext_completion", "_aembedding"],
)
@pytest.mark.asyncio
async def test_run_async_fallback(function_name):
"""
Basic test - given a list of fallback models, run the original function with the fallback models
"""
router = create_test_router()
original_function = getattr(router, function_name)
litellm.set_verbose = True
fallback_model_group = ["gpt-4"]
original_model_group = "gpt-3.5-turbo"
original_exception = litellm.exceptions.InternalServerError(
message="Simulated error",
llm_provider="openai",
model="gpt-3.5-turbo",
)
request_kwargs = {
"mock_response": "hello this is a test for run_async_fallback",
"metadata": {"previous_models": ["gpt-3.5-turbo"]},
}
if function_name == "_aembedding":
request_kwargs["input"] = "hello this is a test for run_async_fallback"
elif function_name == "_atext_completion":
request_kwargs["prompt"] = "hello this is a test for run_async_fallback"
elif function_name == "_acompletion":
request_kwargs["messages"] = [{"role": "user", "content": "Hello, world!"}]
result = await run_async_fallback(
litellm_router=router,
original_function=original_function,
num_retries=1,
fallback_model_group=fallback_model_group,
original_model_group=original_model_group,
original_exception=original_exception,
max_fallbacks=5,
fallback_depth=0,
**request_kwargs,
)
assert result is not None
if function_name == "_acompletion":
assert isinstance(result, litellm.ModelResponse)
elif function_name == "_atext_completion":
assert isinstance(result, litellm.TextCompletionResponse)
elif function_name == "_aembedding":
assert isinstance(result, litellm.EmbeddingResponse)
class CustomTestLogger(CustomLogger):
def __init__(self):
super().__init__()
self.success_fallback_events = []
self.failure_fallback_events = []
async def log_success_fallback_event(
self, original_model_group, kwargs, original_exception
):
print(
"in log_success_fallback_event for original_model_group: ",
original_model_group,
)
self.success_fallback_events.append(
(original_model_group, kwargs, original_exception)
)
async def log_failure_fallback_event(
self, original_model_group, kwargs, original_exception
):
print(
"in log_failure_fallback_event for original_model_group: ",
original_model_group,
)
self.failure_fallback_events.append(
(original_model_group, kwargs, original_exception)
)
@pytest.mark.asyncio
async def test_log_success_fallback_event():
"""
Tests that successful fallback events are logged correctly
"""
original_model_group = "gpt-3.5-turbo"
kwargs = {"messages": [{"role": "user", "content": "Hello, world!"}]}
original_exception = litellm.exceptions.InternalServerError(
message="Simulated error",
llm_provider="openai",
model="gpt-3.5-turbo",
)
logger = CustomTestLogger()
litellm.callbacks = [logger]
# This test mainly checks if the function runs without errors
await log_success_fallback_event(original_model_group, kwargs, original_exception)
await asyncio.sleep(0.5)
assert len(logger.success_fallback_events) == 1
assert len(logger.failure_fallback_events) == 0
assert logger.success_fallback_events[0] == (
original_model_group,
kwargs,
original_exception,
)
@pytest.mark.asyncio
async def test_log_failure_fallback_event():
"""
Tests that failed fallback events are logged correctly
"""
original_model_group = "gpt-3.5-turbo"
kwargs = {"messages": [{"role": "user", "content": "Hello, world!"}]}
original_exception = litellm.exceptions.InternalServerError(
message="Simulated error",
llm_provider="openai",
model="gpt-3.5-turbo",
)
logger = CustomTestLogger()
litellm.callbacks = [logger]
# This test mainly checks if the function runs without errors
await log_failure_fallback_event(original_model_group, kwargs, original_exception)
await asyncio.sleep(0.5)
assert len(logger.failure_fallback_events) == 1
assert len(logger.success_fallback_events) == 0
assert logger.failure_fallback_events[0] == (
original_model_group,
kwargs,
original_exception,
)
@pytest.mark.asyncio
@pytest.mark.parametrize("function_name", ["_acompletion", "_atext_completion"])
async def test_failed_fallbacks_raise_most_recent_exception(function_name):
"""
Tests that if all fallbacks fail, the most recent occuring exception is raised
meaning the exception from the last fallback model is raised
"""
router = create_test_router()
original_function = getattr(router, function_name)
fallback_model_group = ["gpt-4"]
original_model_group = "gpt-3.5-turbo"
original_exception = litellm.exceptions.InternalServerError(
message="Simulated error",
llm_provider="openai",
model="gpt-3.5-turbo",
)
request_kwargs: Dict[str, Any] = {
"metadata": {"previous_models": ["gpt-3.5-turbo"]}
}
if function_name == "_aembedding":
request_kwargs["input"] = "hello this is a test for run_async_fallback"
elif function_name == "_atext_completion":
request_kwargs["prompt"] = "hello this is a test for run_async_fallback"
elif function_name == "_acompletion":
request_kwargs["messages"] = [{"role": "user", "content": "Hello, world!"}]
with pytest.raises(litellm.exceptions.RateLimitError):
await run_async_fallback(
litellm_router=router,
original_function=original_function,
num_retries=1,
fallback_model_group=fallback_model_group,
original_model_group=original_model_group,
original_exception=original_exception,
mock_response="litellm.RateLimitError",
max_fallbacks=5,
fallback_depth=0,
**request_kwargs,
)
@pytest.mark.asyncio
@pytest.mark.parametrize("function_name", ["_acompletion", "_atext_completion"])
async def test_multiple_fallbacks(function_name):
"""
Tests that if multiple fallbacks passed:
- fallback 1 = bad configured deployment / failing endpoint
- fallback 2 = working deployment / working endpoint
Assert that:
- a success response is received from the working endpoint (fallback 2)
"""
router_2 = create_test_router_2()
original_function = getattr(router_2, function_name)
fallback_model_group = ["gpt-4", "fake-openai-endpoint-2"]
original_model_group = "gpt-3.5-turbo"
original_exception = Exception("Simulated error")
request_kwargs: Dict[str, Any] = {
"metadata": {"previous_models": ["gpt-3.5-turbo"]}
}
if function_name == "_aembedding":
request_kwargs["input"] = "hello this is a test for run_async_fallback"
elif function_name == "_atext_completion":
request_kwargs["prompt"] = "hello this is a test for run_async_fallback"
elif function_name == "_acompletion":
request_kwargs["messages"] = [{"role": "user", "content": "Hello, world!"}]
result = await run_async_fallback(
litellm_router=router_2,
original_function=original_function,
num_retries=1,
fallback_model_group=fallback_model_group,
original_model_group=original_model_group,
original_exception=original_exception,
max_fallbacks=5,
fallback_depth=0,
**request_kwargs,
)
print(result)
print(result._hidden_params)
assert result._hidden_params["api_base"] == FAKE_OPENAI_API_BASE