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]

litellm_fix(test): fix router silent experiment tests to properly mock async functions (#20140)

shin-bot-litellme35e6504fc83
diff --git a/tests/test_litellm/test_router_silent_experiment.py b/tests/test_litellm/test_router_silent_experiment.py
index 3afb9444391..a23ea80f7ce 100644
--- a/tests/test_litellm/test_router_silent_experiment.py
+++ b/tests/test_litellm/test_router_silent_experiment.py
@@ -93,15 +93,12 @@ async def test_router_silent_experiment_acompletion():
router = Router(model_list=model_list)
- # Mock litellm.acompletion
- mock_acompletion = MagicMock()
- # Create a future that resolves to a ModelResponse
+ # Use AsyncMock for async function mocking
mock_response = litellm.ModelResponse(choices=[{"message": {"content": "hello"}}])
- future = asyncio.Future()
- future.set_result(mock_response)
- mock_acompletion.return_value = future
+ mock_acompletion = AsyncMock(return_value=mock_response)
- with patch("litellm.acompletion", mock_acompletion):
+ # Patch at the litellm.router module level where it's imported and used
+ with patch.object(litellm, "acompletion", mock_acompletion):
response = await router.acompletion(
model="primary-model",
messages=[{"role": "user", "content": "hi"}],
@@ -177,11 +174,11 @@ def test_router_silent_experiment_completion():
router = Router(model_list=model_list)
# Mock litellm.completion
- mock_completion = MagicMock()
mock_response = litellm.ModelResponse(choices=[{"message": {"content": "hello"}}])
- mock_completion.return_value = mock_response
+ mock_completion = MagicMock(return_value=mock_response)
- with patch("litellm.completion", mock_completion):
+ # Patch at the litellm module level
+ with patch.object(litellm, "completion", mock_completion):
response = router.completion(
model="primary-model",
messages=[{"role": "user", "content": "hi"}],