Skip to content

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]

Fix test_async_fallbacks_streaming to use mock_response instead of real API

yuneng-jiangbcd887ea612c
diff --git a/tests/local_testing/test_router_fallbacks.py b/tests/local_testing/test_router_fallbacks.py
index b5a910d6e93..c586fa8c93b 100644
--- a/tests/local_testing/test_router_fallbacks.py
+++ b/tests/local_testing/test_router_fallbacks.py
@@ -500,55 +500,25 @@ async def test_dynamic_fallbacks_async():
@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 = [
- { # list of model deployments
- "model_name": "azure/gpt-3.5-turbo", # openai model name
- "litellm_params": { # params for litellm completion/embedding call
- "model": "azure/gpt-4.1-mini",
- "api_key": "bad-key",
- "api_version": os.getenv("AZURE_API_VERSION"),
- "api_base": os.getenv("AZURE_API_BASE"),
- },
- "tpm": 240000,
- "rpm": 1800,
- },
- { # list of model deployments
- "model_name": "azure/gpt-3.5-turbo-context-fallback", # openai model name
- "litellm_params": { # params for litellm completion/embedding call
- "model": "azure/gpt-4.1-mini",
- "api_key": os.getenv("AZURE_API_KEY"),
- "api_version": os.getenv("AZURE_API_VERSION"),
- "api_base": os.getenv("AZURE_API_BASE"),
- },
- "tpm": 240000,
- "rpm": 1800,
- },
{
- "model_name": "azure/gpt-3.5-turbo", # openai model name
- "litellm_params": { # params for litellm completion/embedding call
- "model": "azure/chatgpt-functioncalling",
- "api_key": "bad-key",
- "api_version": os.getenv("AZURE_API_VERSION"),
- "api_base": os.getenv("AZURE_API_BASE"),
+ "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", # openai model name
- "litellm_params": { # params for litellm completion/embedding call
- "model": "gpt-4o-mini",
- "api_key": os.getenv("OPENAI_API_KEY"),
- },
- "tpm": 1000000,
- "rpm": 9000,
- },
- {
- "model_name": "gpt-4o-mini-context", # openai model name
- "litellm_params": { # params for litellm completion/embedding call
+ "model_name": "gpt-4o-mini",
+ "litellm_params": {
"model": "gpt-4o-mini",
- "api_key": os.getenv("OPENAI_API_KEY"),
+ "api_key": "fake-key",
},
"tpm": 1000000,
"rpm": 9000,
@@ -558,27 +528,22 @@ async def test_async_fallbacks_streaming():
router = Router(
model_list=model_list,
fallbacks=[{"azure/gpt-3.5-turbo": ["gpt-4o-mini"]}],
- context_window_fallbacks=[
- {"azure/gpt-3.5-turbo-context-fallback": ["gpt-4o-mini-context"]},
- {"gpt-4o-mini": ["gpt-4o-mini-context"]},
- ],
set_verbose=False,
)
customHandler = MyCustomHandler()
litellm.callbacks = [customHandler]
user_message = "Hello, how are you?"
- messages = [{"content": user_message, "role": "user"}]
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",
)
- print(f"customHandler.previous_models: {customHandler.previous_models}")
- await asyncio.sleep(
- 0.05
- ) # allow a delay as success_callbacks are on a separate thread
- assert customHandler.previous_models == 3 # 1 init call + 2 retries (fallback not counted as previous)
+ 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