fix(ollama): keep the repair warning's argument preview unless message logging is off

Backport of #49183 to lts/1.108.x.
Cherry-picked from 5ef6d62cca (fix-ollama-chat-stream-tool-call-index).
Adapted to the line. The repair warning lives in common_utils.py here.
This commit is contained in:
mateo-berri 2026-11-06 21:19:18 -08:00
parent 2360392892
commit 2280aa224b
2 changed files with 34 additions and 4 deletions

View file

@ -20,6 +20,7 @@ from pydantic import TypeAdapter
import litellm
from litellm import verbose_logger
from litellm.constants import REDACTED_BY_LITELLM
from litellm.router_utils.batch_utils import InMemoryFile
from litellm.types.llms.openai import (
AllMessageValues,
@ -2660,6 +2661,15 @@ def merge_consecutive_system_messages(
_DECODED_JSON: Final = TypeAdapter(object)
_ARGUMENT_PREVIEW_CHARS: Final = 200
def _logged_arguments(arguments: str) -> str:
if litellm.turn_off_message_logging:
return REDACTED_BY_LITELLM
if len(arguments) <= _ARGUMENT_PREVIEW_CHARS:
return arguments
return f"{arguments[:_ARGUMENT_PREVIEW_CHARS]}..."
def _attempt_json_repair(s: str) -> object | None:
@ -2757,10 +2767,11 @@ def parse_tool_call_arguments(
repaired: Final = _attempt_json_repair(arguments)
if repaired is not None:
verbose_logger.warning(
"Repaired truncated tool call arguments for tool '%s' (%s). Original length: %d chars",
"Repaired truncated tool call arguments for tool '%s' (%s). Original (%d chars): %s",
tool_name or "<unknown>",
context or "unknown context",
len(arguments),
_logged_arguments(arguments),
)
return repaired

View file

@ -10,6 +10,8 @@ from typing import Final, cast
import pytest
import litellm
from litellm.constants import REDACTED_BY_LITELLM
from litellm.litellm_core_utils.prompt_templates.common_utils import (
ENCRYPTED_REASONING_SIGNATURE_PREFIX,
TOOL_RESULT_IMAGE_BOUNDARY,
@ -2442,12 +2444,29 @@ def test_schema_size_counts_every_written_object_once(schema, properties, branch
assert schema_grammar_branch_count(schema) == branches
def test_parse_tool_call_arguments_repair_warning_leaves_out_argument_content(
def test_parse_tool_call_arguments_repair_warning_previews_the_first_200_characters(
caplog: pytest.LogCaptureFixture,
) -> None:
arguments: Final = '{"patient": "Jane Doe", "note": "' + "x" * 300 + '"'
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
result: Final = parse_tool_call_arguments('{"patient": "Jane Doe"', tool_name="lookup", context="test")
result: Final = parse_tool_call_arguments(arguments, tool_name="lookup", context="test")
assert result == {"patient": "Jane Doe", "note": "x" * 300}
assert (
f"Repaired truncated tool call arguments for tool 'lookup' (test). Original ({len(arguments)} chars): "
f"{arguments[:200]}..." in caplog.text
)
assert arguments not in caplog.text
def test_parse_tool_call_arguments_repair_warning_redacts_arguments_when_message_logging_is_off(
caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setattr(litellm, "turn_off_message_logging", True)
arguments: Final = '{"patient": "Jane Doe"'
with caplog.at_level(logging.WARNING, logger="LiteLLM"):
result: Final = parse_tool_call_arguments(arguments, tool_name="lookup", context="test")
assert result == {"patient": "Jane Doe"}
assert "Repaired truncated tool call arguments for tool 'lookup'" in caplog.text
assert f"Original ({len(arguments)} chars): {REDACTED_BY_LITELLM}" in caplog.text
assert "Jane Doe" not in caplog.text