Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(logging): keep partial logging payloads intact when nothing needs truncating

mateo-berri3d22ee8f5927
diff --git a/litellm/integrations/custom_logger.py b/litellm/integrations/custom_logger.py
index e164f042818..70d2f3ae5c3 100644
--- a/litellm/integrations/custom_logger.py
+++ b/litellm/integrations/custom_logger.py
@@ -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:
"""
diff --git a/tests/logging_callback_tests/test_standard_logging_payload.py b/tests/logging_callback_tests/test_standard_logging_payload.py
index c125bd53904..0e1ee57689f 100644
--- a/tests/logging_callback_tests/test_standard_logging_payload.py
+++ b/tests/logging_callback_tests/test_standard_logging_payload.py
@@ -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 (