mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-05 08:07:05 +00:00
* test: enforce PT012 so a pytest.raises block cannot hide dead assertions `with pytest.raises(...)` stops at the first statement that raises. Anything sequenced after it inside the block never runs, so an assertion written there is never checked and the test still reports green. Two sites were doing exactly that, and both assertions turned out to be wrong once they started running. tests/llm_translation/test_prompt_factory.py asserted the bedrock rejection names "requires at least one non-system message", which holds. tests/proxy_unit_tests/test_proxy_server.py asserted the prisma startup failure mentions "httpx.ConnectError", which never appears: the failure is an httpx.ConnectError whose message is "All connection attempts failed", so that test now asserts the type. Its DATABASE_URL override moves to monkeypatch, since the old restore sat below the assertion and leaked the invalid URL into every later DB test the moment the assertion started being able to fail. The remaining 72 sites are rewritten without changing what they exercise: setup that cannot raise moves above the block, a nested `patch` moves outside it, and bodies with real control flow (a stream drain, an if/else on sync_mode, a retry loop) move into a local closure the block calls. Fixing PT012 unmasked two B017s, since ruff only reports a blind pytest.raises(Exception) once the block holds a single statement. tests/proxy_unit_tests/test_auth_checks.py narrows to the ProxyException can_key_call_model actually raises. tests/local_testing/test_completion_cost.py was asserting vertex_ai/medlm-medium has no cost entry, which stopped being true at some point; that dead first half is gone and the rest of the test, which checks medlm pricing resolves above zero, now runs instead of being skipped. * chore(ci): ratchet TQ004 to 768 after the prisma test moved to monkeypatch
176 lines
5.4 KiB
Python
176 lines
5.4 KiB
Python
#### What this tests ####
|
|
# This tests mock request calls to litellm
|
|
|
|
import os
|
|
import sys
|
|
import traceback
|
|
|
|
import pytest
|
|
|
|
sys.path.insert(
|
|
0, os.path.abspath("../..")
|
|
) # Adds the parent directory to the system path
|
|
import litellm
|
|
import time
|
|
|
|
|
|
def test_mock_request():
|
|
try:
|
|
model = "gpt-3.5-turbo"
|
|
messages = [{"role": "user", "content": "Hey, I'm a mock request"}]
|
|
response = litellm.mock_completion(model=model, messages=messages, stream=False)
|
|
print(response)
|
|
print(type(response))
|
|
except Exception:
|
|
traceback.print_exc()
|
|
|
|
|
|
# test_mock_request()
|
|
def test_streaming_mock_request():
|
|
try:
|
|
model = "gpt-3.5-turbo"
|
|
messages = [{"role": "user", "content": "Hey, I'm a mock request"}]
|
|
response = litellm.mock_completion(model=model, messages=messages, stream=True)
|
|
complete_response = ""
|
|
for chunk in response:
|
|
complete_response += chunk["choices"][0]["delta"]["content"] or ""
|
|
if complete_response == "":
|
|
raise Exception("Empty response received")
|
|
except Exception:
|
|
traceback.print_exc()
|
|
|
|
|
|
# test_streaming_mock_request()
|
|
|
|
|
|
@pytest.mark.asyncio()
|
|
async def test_async_mock_streaming_request():
|
|
generator = await litellm.acompletion(
|
|
messages=[{"role": "user", "content": "Why is LiteLLM amazing?"}],
|
|
mock_response="LiteLLM is awesome",
|
|
stream=True,
|
|
model="gpt-3.5-turbo",
|
|
)
|
|
complete_response = ""
|
|
async for chunk in generator:
|
|
print(chunk)
|
|
complete_response += chunk["choices"][0]["delta"]["content"] or ""
|
|
|
|
assert (
|
|
complete_response == "LiteLLM is awesome"
|
|
), f"Unexpected response got {complete_response}"
|
|
|
|
|
|
def test_mock_request_n_greater_than_1():
|
|
try:
|
|
model = "gpt-3.5-turbo"
|
|
messages = [{"role": "user", "content": "Hey, I'm a mock request"}]
|
|
response = litellm.mock_completion(model=model, messages=messages, n=5)
|
|
print("response: ", response)
|
|
|
|
assert len(response.choices) == 5
|
|
for choice in response.choices:
|
|
assert choice.message.content == "This is a mock request"
|
|
|
|
except Exception:
|
|
traceback.print_exc()
|
|
|
|
|
|
@pytest.mark.asyncio()
|
|
async def test_async_mock_streaming_request_n_greater_than_1():
|
|
generator = await litellm.acompletion(
|
|
messages=[{"role": "user", "content": "Why is LiteLLM amazing?"}],
|
|
mock_response="LiteLLM is awesome",
|
|
stream=True,
|
|
model="gpt-3.5-turbo",
|
|
n=5,
|
|
)
|
|
complete_response = ""
|
|
async for chunk in generator:
|
|
print(chunk)
|
|
# complete_response += chunk["choices"][0]["delta"]["content"] or ""
|
|
|
|
# assert (
|
|
# complete_response == "LiteLLM is awesome"
|
|
# ), f"Unexpected response got {complete_response}"
|
|
|
|
|
|
def test_mock_request_with_mock_timeout():
|
|
"""
|
|
Allow user to set 'mock_timeout = True', this allows for testing if fallbacks/retries are working on timeouts.
|
|
"""
|
|
start_time = time.time()
|
|
with pytest.raises(litellm.Timeout):
|
|
response = litellm.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hey, I'm a mock request"}],
|
|
timeout=3,
|
|
mock_timeout=True,
|
|
)
|
|
end_time = time.time()
|
|
assert end_time - start_time >= 3, f"Time taken: {end_time - start_time}"
|
|
|
|
|
|
def test_router_mock_request_with_mock_timeout():
|
|
"""
|
|
Allow user to set 'mock_timeout = True', this allows for testing if fallbacks/retries are working on timeouts.
|
|
"""
|
|
start_time = time.time()
|
|
router = litellm.Router(
|
|
model_list=[
|
|
{
|
|
"model_name": "gpt-3.5-turbo",
|
|
"litellm_params": {
|
|
"model": "gpt-3.5-turbo",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
],
|
|
)
|
|
with pytest.raises(litellm.Timeout):
|
|
router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hey, I'm a mock request"}],
|
|
timeout=3,
|
|
mock_timeout=True,
|
|
)
|
|
end_time = time.time()
|
|
assert end_time - start_time >= 3, f"Time taken: {end_time - start_time}"
|
|
|
|
|
|
def test_router_mock_request_with_mock_timeout_with_fallbacks():
|
|
"""
|
|
Allow user to set 'mock_timeout = True', this allows for testing if fallbacks/retries are working on timeouts.
|
|
"""
|
|
litellm.set_verbose = True
|
|
start_time = time.time()
|
|
router = litellm.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.1-nano",
|
|
"litellm_params": {
|
|
"model": "gpt-4.1-nano",
|
|
"api_key": os.getenv("OPENAI_API_KEY"),
|
|
},
|
|
},
|
|
],
|
|
fallbacks=[{"gpt-3.5-turbo": ["gpt-4.1-nano"]}],
|
|
)
|
|
response = router.completion(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": "Hey, I'm a mock request"}],
|
|
timeout=3,
|
|
num_retries=1,
|
|
mock_timeout=True,
|
|
)
|
|
print(response)
|
|
end_time = time.time()
|
|
assert end_time - start_time >= 3, f"Time taken: {end_time - start_time}"
|
|
assert "gpt-4.1-nano" in response.model, "Model should be gpt-4.1-nano"
|