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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(logging): stop deepcopying results redaction cannot redact
Marty Sullivan132bee892a0d
diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.pyindex 836af24fb3f..e91acaa97ca 100644--- a/litellm/litellm_core_utils/redact_messages.py+++ b/litellm/litellm_core_utils/redact_messages.py@@ -258,6 +258,19 @@ def perform_redaction(model_call_details: dict, result, redact_streaming_respons# For async objects, return a simple redacted response without deepcopyreturn {"text": "redacted-by-litellm"}+ # Only the shapes handled below can be redacted; every other type falls through+ # to the placeholder return at the end of this block, which discards the copy.+ # Deciding that before copying keeps the deepcopy off objects it cannot help:+ # binary/HTTP response bodies (batch output, file content) hold an unpicklable+ # `_thread.lock` and raise TypeError here, which aborts success logging and every+ # spend callback with it, and a large batch body would be copied only to be thrown+ # away.+ if not (+ isinstance(result, (litellm.ModelResponse, litellm.ResponsesAPIResponse, litellm.EmbeddingResponse))+ or (isinstance(result, dict) and ("choices" in result or "output" in result))+ ):+ return {"text": "redacted-by-litellm"}+_result: Final = copy.deepcopy(result)if isinstance(_result, litellm.ModelResponse):if hasattr(_result, "choices") and _result.choices is not None:diff --git a/tests/test_litellm/litellm_core_utils/test_redact_messages.py b/tests/test_litellm/litellm_core_utils/test_redact_messages.pyindex e1ffabb3515..88d2a95dd4f 100644--- a/tests/test_litellm/litellm_core_utils/test_redact_messages.py+++ b/tests/test_litellm/litellm_core_utils/test_redact_messages.py@@ -5,6 +5,7 @@ Covers the proxy flow where headers arrive in litellm_params["metadata"]["headerbut litellm_params["litellm_metadata"] is None."""+import threadingfrom types import SimpleNamespaceimport pytest@@ -682,6 +683,50 @@ class TestPerformRedaction:assert response_obj.choices[0].message.content == "secret content"+ def test_unredactable_result_is_not_deepcopied(self):+ """A result shape no branch can redact must not be deepcopied.++ Binary/HTTP response bodies (batch output, file content, audio) hold an+ unpicklable ``_thread.lock``. Copying one raises TypeError inside+ ``Logging.success_handler``, which aborts every success callback with it - so the+ spend row for a completed batch is never written. The copy is also pointless:+ an unrecognized shape returns the placeholder and the copy is discarded.++ The lock is the assertion. If a deepcopy is ever reintroduced ahead of the type+ check, this raises instead of returning.+ """++ class _BinaryResponseBody:+ def __init__(self) -> None:+ self.text = "batch output bytes"+ self._client_lock = threading.Lock()++ body = _BinaryResponseBody()++ redacted = perform_redaction({"litellm_params": {}}, body)++ assert redacted == {"text": "redacted-by-litellm"}++ def test_recognized_shapes_still_redact_a_copy(self):+ """The type gate must not change behaviour for shapes that were already handled."""+ original = litellm.ModelResponse(+ choices=[litellm.Choices(message=litellm.Message(content="secret content", role="assistant"))]+ )++ redacted = perform_redaction({"litellm_params": {}}, original)++ assert redacted.choices[0].message.content == "redacted-by-litellm"+ assert original.choices[0].message.content == "secret content"++ embedding = litellm.EmbeddingResponse(data=[{"embedding": [1.0, 2.0]}])+ assert perform_redaction({"litellm_params": {}}, embedding).data == []++ as_dict = {"choices": [{"message": {"role": "assistant", "content": "secret content"}}]}+ assert (+ perform_redaction({"litellm_params": {}}, as_dict)["choices"][0]["message"]["content"]+ == "redacted-by-litellm"+ )+class TestRedactStreamingResponsesForCustomLogger:def _model_call_details(self):