From 0606a5765422f9e10a908afc2ee265d7cba24bfb Mon Sep 17 00:00:00 2001 From: Dantuluri Surya Narayana Raju Date: Sun, 17 May 2026 20:54:57 +0530 Subject: [PATCH] test: rewrite regression test to call shorten_message_to_fit_limit directly --- tests/litellm_utils_tests/test_utils.py | 40 ++++++------------------- 1 file changed, 9 insertions(+), 31 deletions(-) diff --git a/tests/litellm_utils_tests/test_utils.py b/tests/litellm_utils_tests/test_utils.py index 26073f24604..43f95505ffb 100644 --- a/tests/litellm_utils_tests/test_utils.py +++ b/tests/litellm_utils_tests/test_utils.py @@ -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: