test: rewrite regression test to call shorten_message_to_fit_limit directly

This commit is contained in:
Dantuluri Surya Narayana Raju 2026-05-17 20:54:57 +05:30
parent 42ee47d5ac
commit 0606a57654

View file

@ -288,40 +288,18 @@ def test_trimming_with_model_cost_max_input_tokens(model):
def test_shorten_message_content_never_grows():
"""Regression test: shorten_message_to_fit_limit must never make content longer.
Bug: when half_length==0, content[-0:] == content[0:] (entire string) due to
Python's -0 == 0 identity, so trimmed = '' + '..' + full_content was longer
than the original. This caused the loop to grow content on every subsequent
iteration instead of shrinking it.
"""
from litellm.utils import get_token_count
from litellm.constants import MAX_TOKEN_TRIMMING_ATTEMPTS
"""Regression test: shorten_message_to_fit_limit must never make content longer."""
from litellm.utils import shorten_message_to_fit_limit
content = "hello world this is a moderately long message"
msg_copy = {"role": "user", "content": content}
max_len_seen = len(content)
tokens_needed = 1
model = None
message = {"role": "user", "content": content}
original_len = len(content)
for _ in range(MAX_TOKEN_TRIMMING_ATTEMPTS):
total_tokens = get_token_count([msg_copy], model)
if total_tokens <= tokens_needed:
break
ratio = tokens_needed / total_tokens
new_length = max(0, int(len(msg_copy["content"]) * ratio) - 1)
half_length = new_length // 2
if half_length == 0:
trimmed = msg_copy["content"][:new_length]
else:
c = msg_copy["content"]
trimmed = c[:half_length] + ".." + c[-half_length:]
assert len(trimmed) <= max_len_seen, (
f"Content grew from {max_len_seen} to {len(trimmed)} chars — "
"half_length==0 guard is missing"
)
max_len_seen = len(trimmed)
msg_copy["content"] = trimmed
result = shorten_message_to_fit_limit(message, tokens_needed=1, model=None)
assert len(result["content"]) <= original_len, (
f"Content grew from {original_len} to {len(result['content'])} chars — "
"half_length==0 guard is missing or broken"
)
def test_trimming_with_untokenizable_field(caplog: pytest.LogCaptureFixture) -> None: