litellm/tests/test_litellm/test_lowest_latency_zero_tokens.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

131 lines
3.9 KiB
Python

#### What this tests ####
# This tests the router's handling of zero completion tokens in lowest latency routing
import time
import pytest
import litellm
from litellm.caching.caching import DualCache
from litellm.router_strategy.lowest_latency import LowestLatencyLoggingHandler
def test_zero_completion_tokens_no_division_error():
"""
Test that log_success_event handles zero completion tokens without ZeroDivisionError
This tests the fix for issue #12641 where responses with zero completion tokens
(e.g., from Gemini with long contexts) caused ZeroDivisionError
"""
test_cache = DualCache()
lowest_latency_logger = LowestLatencyLoggingHandler(router_cache=test_cache)
deployment_id = "1234"
kwargs = {
"litellm_params": {
"metadata": {
"model_group": "gemini-2.5-flash",
"deployment": "gemini/gemini-2.5-flash",
},
"model_info": {"id": deployment_id},
}
}
# Create a ModelResponse with zero completion tokens (as reported in issue)
response_obj = litellm.ModelResponse(
id="9p13aIGDDNmPmLAP5-23mQQ",
created=1752669685,
model="gemini-2.5-flash",
object="chat.completion",
choices=[
litellm.Choices(
finish_reason="stop",
index=0,
message=litellm.Message(
content=None, role="assistant", tool_calls=None
),
)
],
usage=litellm.Usage(
completion_tokens=0, # This causes the ZeroDivisionError
prompt_tokens=245537,
total_tokens=245537,
),
)
start_time = time.time()
time.sleep(0.1) # Simulate some response time
end_time = time.time()
# This should not raise ZeroDivisionError
try:
lowest_latency_logger.log_success_event(
response_obj=response_obj,
kwargs=kwargs,
start_time=start_time,
end_time=end_time,
)
except ZeroDivisionError:
pytest.fail(
"log_success_event raised ZeroDivisionError with zero completion tokens"
)
# Verify the deployment was logged (even with zero completion tokens)
cached_value = test_cache.get_cache(
key=f"{kwargs['litellm_params']['metadata']['model_group']}_map"
)
assert cached_value is not None
assert deployment_id in cached_value
def test_zero_completion_tokens_with_time_to_first_token():
"""
Test that time_to_first_token calculation also handles zero completion tokens
"""
test_cache = DualCache()
lowest_latency_logger = LowestLatencyLoggingHandler(router_cache=test_cache)
deployment_id = "1234"
kwargs = {
"litellm_params": {
"metadata": {
"model_group": "gemini-2.5-flash",
"deployment": "gemini/gemini-2.5-flash",
},
"model_info": {"id": deployment_id},
"stream": True,
},
"completion_start_time": time.time() + 0.05, # Simulate time to first token
}
# Create a ModelResponse with zero completion tokens
response_obj = litellm.ModelResponse(
usage=litellm.Usage(
completion_tokens=0, prompt_tokens=100000, total_tokens=100000
)
)
start_time = time.time()
time.sleep(0.1)
end_time = time.time()
# This should not raise ZeroDivisionError
try:
lowest_latency_logger.log_success_event(
response_obj=response_obj,
kwargs=kwargs,
start_time=start_time,
end_time=end_time,
)
except ZeroDivisionError:
pytest.fail(
"log_success_event raised ZeroDivisionError with zero completion tokens in streaming"
)
if __name__ == "__main__":
test_zero_completion_tokens_no_division_error()
test_zero_completion_tokens_with_time_to_first_token()
print("All tests passed!")