From e28f50803052fcb4e095934a020e0df77c648181 Mon Sep 17 00:00:00 2001 From: yucheng Date: Thu, 29 Oct 2026 16:53:52 -0700 Subject: [PATCH] refactor(logging): build the scrubbed error information without mutation --- litellm/integrations/opentelemetry.py | 1 - litellm/litellm_core_utils/redact_messages.py | 15 +++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/litellm/integrations/opentelemetry.py b/litellm/integrations/opentelemetry.py index 7de1a1fc080..ad5976bdf7f 100644 --- a/litellm/integrations/opentelemetry.py +++ b/litellm/integrations/opentelemetry.py @@ -2274,7 +2274,6 @@ class OpenTelemetry(OTELGenAISemconvMixin, CustomLogger): exception: Final = kwargs.get("exception") scrubbed: Final = scrubbed_exception_text(kwargs) - # Record the exception using OTEL's standard method if exception is not None and scrubbed is not None: span.record_exception( exception, attributes={"exception.message": scrubbed[0], "exception.stacktrace": scrubbed[1]} diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py index b770c35eb98..49cac4fbc0c 100644 --- a/litellm/litellm_core_utils/redact_messages.py +++ b/litellm/litellm_core_utils/redact_messages.py @@ -524,14 +524,13 @@ def scrubbed_error_information( ) -> StandardLoggingPayloadErrorInformation: """``error_information`` with the request's ``texts`` scrubbed out of its message and traceback. The type, status code and every other field carry no prompt and are left as they are.""" - scrubbed: Final = error_information.copy() - error_message: Final = scrubbed.get("error_message") - if isinstance(error_message, str): - scrubbed["error_message"] = scrub_request_text(error_message, texts) - traceback_text: Final = scrubbed.get("traceback") - if isinstance(traceback_text, str): - scrubbed["traceback"] = scrub_request_text(traceback_text, texts) - return scrubbed + error_message: Final = error_information.get("error_message") + traceback_text: Final = error_information.get("traceback") + return { + **error_information, + "error_message": error_message if error_message is None else scrub_request_text(error_message, texts), + "traceback": traceback_text if traceback_text is None else scrub_request_text(traceback_text, texts), + } def _with_scrubbed_text(