fix(logging): keep partial logging payloads intact when nothing needs truncating
Some checks failed
ai-gateway image / ai-gateway release image (push) Has been cancelled
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled

This commit is contained in:
mateo-berri 2026-09-12 18:20:33 -07:00
parent 6db93a930f
commit 3d22ee8f59
2 changed files with 12 additions and 8 deletions

View file

@ -833,16 +833,12 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac
the original fields.
"""
max_str_length: Final = 10_000
error_str, messages, response = (
self._truncate_field(field_value=standard_logging_object.get(field), max_length=max_str_length)
candidates: Final = {
field: self._truncate_field(field_value=standard_logging_object.get(field), max_length=max_str_length)
for field in ("error_str", "messages", "response")
)
return {
**standard_logging_object,
"error_str": standard_logging_object["error_str"] if error_str is None else error_str,
"messages": standard_logging_object["messages"] if messages is None else messages,
"response": standard_logging_object["response"] if response is None else response,
}
truncated_fields: Final = {field: text for field, text in candidates.items() if text is not None}
return {**standard_logging_object, **truncated_fields}
def _truncate_field(self, field_value: object, max_length: int) -> str | None:
"""

View file

@ -634,6 +634,14 @@ def test_truncate_standard_logging_payload():
assert len(str(truncated["error_str"])) < 10_500
def test_truncate_standard_logging_payload_keeps_a_partial_payload_intact():
"""A payload built with only some of its fields comes back with exactly those keys and values"""
_custom_logger = CustomLogger()
partial_payload = StandardLoggingPayload(request_tags=["tag"], metadata=StandardLoggingMetadata())
assert _custom_logger.truncate_standard_logging_payload_content(partial_payload) == partial_payload
def test_strip_trailing_slash():
common_api_base = "https://api.test.com"
assert (