fix(spend-logs): keep main's stored tail for a negative prompt length limit

At a negative MAX_STRING_LENGTH_PROMPT_IN_DB the shared truncation helper stored one more character of each value than main, since it dropped main's clamp of the end slice to the limit minus the start
This commit is contained in:
mateo-berri 2026-10-17 22:42:34 -07:00
parent 506339246c
commit 7cfab05960
2 changed files with 7 additions and 4 deletions

View file

@ -1205,7 +1205,7 @@ def _truncated_for_db(value: str, max_length: int) -> str:
f"{value[:start_chars]}"
f"... ({LITELLM_TRUNCATED_PAYLOAD_FIELD} skipped {len(value) - start_chars - end_chars} chars. "
f"{LITELLM_TRUNCATION_DB_SAFEGUARD_NOTE}) ..."
f"{value[-end_chars:]}"
f"{value[-min(end_chars, max_length - start_chars) :]}"
)

View file

@ -668,14 +668,17 @@ def test_large_request_no_truncation_threshold():
assert "skipped" in truncated_content
@pytest.mark.parametrize("max_string_length_prompt_in_db", [0, 1])
def test_a_limit_under_two_still_stores_the_whole_value_after_the_marker(max_string_length_prompt_in_db: int):
@pytest.mark.parametrize(
("max_string_length_prompt_in_db", "stored_tail"),
[(0, "claude-sonnet-5-5"), (1, "claude-sonnet-5-5"), (-1, "laude-sonnet-5-5"), (-2, "aude-sonnet-5-5")],
)
def test_a_limit_under_two_keeps_mains_tail_after_the_marker(max_string_length_prompt_in_db: int, stored_tail: str):
sanitized: Final = _sanitize_request_body_for_spend_logs_payload(
{"model": "claude-sonnet-5-5"}, max_string_length_prompt_in_db=max_string_length_prompt_in_db
)
assert sanitized["model"].startswith("...")
assert sanitized["model"].endswith("claude-sonnet-5-5")
assert sanitized["model"].endswith(f") ...{stored_tail}")
def test_small_request_no_truncation():