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 (