litellm/tests/unit/tracing/test_preview.py
Joshua Valluru 7e7988877a feat(tracing): commit Lens ledger export with accepted spend deliveries
With general_settings.lens_ledger_export on, each accepted spend delivery
writes a Lens ledger record to a Postgres outbox in the same transaction,
keyed by its delivery id. The amount and request id come from the accounting
payload, and correlation ids, redaction and content come from the existing
Lens row builder. Large bodies go to a separate payload outbox when Lens
advertises a payload store, and the record keeps a marked UTF-8 preview.

A leased exporter drains both outboxes outside any transaction, with its own
backoff, parking and cleanup for cost events and for bodies. The legacy Lens
callback stays the producer when the flag is off or Lens does not advertise
ledger version 1, and committed records keep draining after a rollback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:01:23 -08:00

51 lines
1.8 KiB
Python

import json
import pytest
from litellm.tracing.preview import MIN_PREVIEW_BYTES, preview
def test_content_within_budget_is_unchanged() -> None:
assert preview('[{"content":"hi"}]', 1024) == '[{"content":"hi"}]'
@pytest.mark.parametrize("unit", ["a", "é", "雪", "🦀"], ids=["ascii", "two_byte", "three_byte", "four_byte"])
def test_plain_text_is_cut_on_a_character_boundary_within_budget(unit: str) -> None:
content = unit * 1000
result = preview(content, 300)
kept = result.split("…[Lens preview: ")[0]
assert len(result.encode()) <= 300
assert kept and content.startswith(kept)
assert result.endswith(f"{len(content.encode()) - len(kept.encode())} bytes omitted]")
def test_json_messages_keep_their_structure_and_mark_the_cut_leaf() -> None:
long = "雪" * 2000
content = json.dumps([{"role": "system", "content": "short"}, {"role": "user", "content": long}])
result = preview(content, 1024)
parsed = json.loads(result)
assert len(result.encode()) <= 1024
assert parsed[0] == {"role": "system", "content": "short"}
kept = parsed[1]["content"].split("…[Lens preview: ")[0]
assert kept and long.startswith(kept)
assert parsed[1]["content"].endswith(f"{len(long.encode()) - len(kept.encode())} bytes omitted]")
def test_json_whose_structure_overflows_becomes_one_marked_string() -> None:
content = json.dumps([{"i": index} for index in range(400)])
result = preview(content, 300)
assert len(result.encode()) <= 300
text = json.loads(result)
assert isinstance(text, str)
assert content.startswith(text.split("…[Lens preview: ")[0])
def test_budget_below_minimum_is_raised_to_the_minimum() -> None:
result = preview("x" * 10_000, 1)
assert MIN_PREVIEW_BYTES // 2 < len(result.encode()) <= MIN_PREVIEW_BYTES