perf: skip redundant redaction when global redaction enabled

When turn_off_message_logging is enabled globally, skip per-callback
redaction functions that would re-process already-redacted data.

17% faster async_success_handler when global redaction is ON.
This commit is contained in:
Ryan Crabbe 2026-01-22 12:52:12 -08:00 • committed by Alexsander Hamir
parent 8216ec4389
commit e3b4ef7cad
3 changed files with 25 additions and 7 deletions

View file

@ -718,7 +718,9 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac
return OLD_LITELLM_METADATA_FIELD
def redact_standard_logging_payload_from_model_call_details(
self, model_call_details: Dict
self,
model_call_details: Dict,
global_redaction_applied: bool = False,
) -> Dict:
"""
Only redacts messages and responses when self.turn_off_message_logging is True
@ -730,6 +732,10 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac
This is useful for logging payloads that contain sensitive information.
"""
# skip redundant redaction if global redaction was already applied
if global_redaction_applied:
return model_call_details
from copy import copy
from litellm import Choices, Message, ModelResponse

View file

@ -68,6 +68,7 @@ from litellm.litellm_core_utils.model_param_helper import ModelParamHelper
from litellm.litellm_core_utils.redact_messages import (
redact_message_input_output_from_custom_logger,
redact_message_input_output_from_logging,
should_redact_message_logging,
)
from litellm.llms.base_llm.ocr.transformation import OCRResponse
from litellm.llms.base_llm.search.transformation import SearchResponse
@ -2410,10 +2411,10 @@ class Logging(LiteLLMLoggingBaseClass):
global_callbacks=litellm._async_success_callback,
)
_model_call_details = self.model_call_details if hasattr(self, "model_call_details") else {}
global_redaction_applied = should_redact_message_logging(_model_call_details)
result = redact_message_input_output_from_logging(
model_call_details=(
self.model_call_details if hasattr(self, "model_call_details") else {}
),
model_call_details=_model_call_details,
result=result,
)
@ -2439,7 +2440,10 @@ class Logging(LiteLLMLoggingBaseClass):
)
elif isinstance(callback, CustomLogger):
result = redact_message_input_output_from_custom_logger(
result=result, litellm_logging_obj=self, custom_logger=callback
result=result,
litellm_logging_obj=self,
custom_logger=callback,
global_redaction_applied=global_redaction_applied,
)
self.model_call_details, result = await callback.async_logging_hook(
kwargs=self.model_call_details,
@ -2494,7 +2498,8 @@ class Logging(LiteLLMLoggingBaseClass):
##################################
# call redaction hook for custom logger
model_call_details = callback.redact_standard_logging_payload_from_model_call_details(
model_call_details=model_call_details
model_call_details=model_call_details,
global_redaction_applied=global_redaction_applied,
)
##################################
if self.stream is True:

View file

@ -30,8 +30,15 @@ else:
def redact_message_input_output_from_custom_logger(
litellm_logging_obj: LiteLLMLoggingObject, result, custom_logger: CustomLogger
litellm_logging_obj: LiteLLMLoggingObject,
result,
custom_logger: CustomLogger,
global_redaction_applied: bool = False,
):
# skip redundant redaction if global redaction was already applied
if global_redaction_applied:
return result
if (
hasattr(custom_logger, "message_logging")
and custom_logger.message_logging is not True