From 811768a38d08d0e7c852ebff7557d4cf745c5fc1 Mon Sep 17 00:00:00 2001 From: taltal-beep Date: Sun, 11 Oct 2026 09:57:31 +0300 Subject: [PATCH] test(utils): cover trim_messages return_response_tokens when trimming fails Also say in the docstring which limit response_tokens is measured against. --- litellm/utils.py | 5 +++-- tests/unit/test_utils.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index 97590fafe64..899f1cf80f3 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -7700,8 +7700,9 @@ def trim_messages( Returns: Trimmed messages. If return_response_tokens is True, a `(messages, response_tokens)` tuple instead, - whether or not anything had to be trimmed. `response_tokens` is None when there is no token limit to - measure against (unknown model and no `max_tokens`). + whether or not anything had to be trimmed. `response_tokens` is what is left of the limit the messages + were trimmed to (`max_tokens`, or the model's max input tokens times `trim_ratio`). It is None when + there is no token limit to measure against (unknown model and no `max_tokens`) or trimming failed. """ # Initialize max_tokens # if users pass in max tokens, trim to this amount diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 29f8180fb5e..e4c9849ac4e 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -7626,6 +7626,21 @@ def test_trim_messages_return_response_tokens_is_none_when_no_token_limit_is_kno assert response_tokens is None +def test_trim_messages_return_response_tokens_returns_a_tuple_when_trimming_fails(monkeypatch): + def failing_token_count(*args, **kwargs): + raise RuntimeError("tokenizer unavailable") + + monkeypatch.setattr(litellm.utils, "get_token_count", failing_token_count) + messages = [{"role": "user", "content": "hi"}] + + assert trim_messages(messages, max_tokens=1000) == messages + + trimmed_messages, response_tokens = trim_messages(messages, max_tokens=1000, return_response_tokens=True) + + assert trimmed_messages == messages + assert response_tokens is None + + @pytest.mark.parametrize("model", ["gpt-5.4-mini", "claude-sonnet-4-6"]) def test_trimming_with_model_cost_max_input_tokens(model): messages = [