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]

Ensure message redaction works for responses API logging (#12291)

Jugal D. Bhatt6f27385edfc3
diff --git a/litellm/integrations/datadog/datadog.py b/litellm/integrations/datadog/datadog.py
index fb6fee6dc6a..cd78ce4a7a1 100644
--- a/litellm/integrations/datadog/datadog.py
+++ b/litellm/integrations/datadog/datadog.py
@@ -576,4 +576,4 @@ class DataDogLogger(
start_time_utc: Optional[datetimeObj],
end_time_utc: Optional[datetimeObj],
) -> Optional[dict]:
- pass
+ pass
\ No newline at end of file
diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py
index 79aeeff144c..a93eb307601 100644
--- a/litellm/litellm_core_utils/redact_messages.py
+++ b/litellm/litellm_core_utils/redact_messages.py
@@ -53,29 +53,43 @@ def perform_redaction(model_call_details: dict, result):
and "complete_streaming_response" in model_call_details
):
_streaming_response = model_call_details["complete_streaming_response"]
- for choice in _streaming_response.choices:
- if isinstance(choice, litellm.Choices):
- choice.message.content = "redacted-by-litellm"
- elif isinstance(choice, litellm.utils.StreamingChoices):
- choice.delta.content = "redacted-by-litellm"
-
- # Redact result
- if result is not None and isinstance(result, litellm.ModelResponse):
- _result = copy.deepcopy(result)
- if hasattr(_result, "choices") and _result.choices is not None:
- for choice in _result.choices:
+ if hasattr(_streaming_response, "choices"):
+ for choice in _streaming_response.choices:
if isinstance(choice, litellm.Choices):
choice.message.content = "redacted-by-litellm"
elif isinstance(choice, litellm.utils.StreamingChoices):
choice.delta.content = "redacted-by-litellm"
- return _result
- if result is not None and isinstance(result, litellm.EmbeddingResponse):
+ elif hasattr(_streaming_response, "output"):
+ # Handle ResponsesAPIResponse format
+ for output_item in _streaming_response.output:
+ if hasattr(output_item, "content") and isinstance(output_item.content, list):
+ for content_part in output_item.content:
+ if hasattr(content_part, "text"):
+ content_part.text = "redacted-by-litellm"
+
+ # Redact result
+ if result is not None:
_result = copy.deepcopy(result)
- if hasattr(_result, "data") and _result.data is not None:
- _result.data = []
+ if isinstance(_result, litellm.ModelResponse):
+ if hasattr(_result, "choices") and _result.choices is not None:
+ for choice in _result.choices:
+ if isinstance(choice, litellm.Choices):
+ choice.message.content = "redacted-by-litellm"
+ elif isinstance(choice, litellm.utils.StreamingChoices):
+ choice.delta.content = "redacted-by-litellm"
+ elif isinstance(_result, litellm.ResponsesAPIResponse):
+ if hasattr(_result, "output"):
+ for output_item in _result.output:
+ if hasattr(output_item, "content") and isinstance(output_item.content, list):
+ for content_part in output_item.content:
+ if hasattr(content_part, "text"):
+ content_part.text = "redacted-by-litellm"
+ elif isinstance(_result, litellm.EmbeddingResponse):
+ if hasattr(_result, "data") and _result.data is not None:
+ _result.data = []
+ else:
+ return {"text": "redacted-by-litellm"}
return _result
- else:
- return {"text": "redacted-by-litellm"}
def should_redact_message_logging(model_call_details: dict) -> bool:
diff --git a/tests/logging_callback_tests/test_datadog.py b/tests/logging_callback_tests/test_datadog.py
index 2a7549b4738..4b56684ade3 100644
--- a/tests/logging_callback_tests/test_datadog.py
+++ b/tests/logging_callback_tests/test_datadog.py
@@ -576,4 +576,4 @@ def test_get_datadog_tags():
# Test with None request_tags
standard_logging_obj["request_tags"] = None
tags_none_request = DataDogLogger._get_datadog_tags(standard_logging_obj)
- assert "request_tag:" not in tags_none_request
+ assert "request_tag:" not in tags_none_request
\ No newline at end of file
diff --git a/tests/logging_callback_tests/test_logging_redaction_e2e_test.py b/tests/logging_callback_tests/test_logging_redaction_e2e_test.py
index 5a5b720b2e0..8a383164a7e 100644
--- a/tests/logging_callback_tests/test_logging_redaction_e2e_test.py
+++ b/tests/logging_callback_tests/test_logging_redaction_e2e_test.py
@@ -118,3 +118,85 @@ async def test_global_redaction_off_with_dynamic_params(turn_off_message_logging
== "hello"
)
assert standard_logging_payload["messages"][0]["content"] == "hi"
+
+
+async def test_redaction_responses_api():
+ """Test redaction with ResponsesAPIResponse format"""
+ litellm.turn_off_message_logging = True
+ test_custom_logger = TestCustomLogger()
+ litellm.callbacks = [test_custom_logger]
+
+ # Mock a ResponsesAPIResponse-style response
+ mock_response = {
+ "output": [{"text": "This is a test response"}],
+ "model": "gpt-3.5-turbo",
+ "usage": {"input_tokens": 5, "output_tokens": 5, "total_tokens": 10}
+ }
+
+ response = await litellm.aresponses(
+ model="gpt-3.5-turbo",
+ input="hi",
+ mock_response=mock_response,
+ )
+
+ await asyncio.sleep(1)
+ standard_logging_payload = test_custom_logger.logged_standard_logging_payload
+ assert standard_logging_payload is not None
+
+ # Verify redaction in ResponsesAPIResponse format
+ assert standard_logging_payload["response"] == {"text": "redacted-by-litellm"}
+ assert standard_logging_payload["messages"][0]["content"] == "redacted-by-litellm"
+ print(
+ "logged standard logging payload for ResponsesAPIResponse",
+ json.dumps(standard_logging_payload, indent=2),
+ )
+
+
+async def test_redaction_responses_api_stream():
+ """Test redaction with ResponsesAPIResponse format"""
+ litellm.turn_off_message_logging = True
+ test_custom_logger = TestCustomLogger()
+ litellm.callbacks = [test_custom_logger]
+
+ # Mock a ResponsesAPIResponse-style response with streaming chunks
+ mock_response = [
+ {
+ "output": [{"text": "This"}],
+ "model": "gpt-3.5-turbo",
+ },
+ {
+ "output": [{"text": " is"}],
+ "model": "gpt-3.5-turbo",
+ },
+ {
+ "output": [{"text": " a test response"}],
+ "model": "gpt-3.5-turbo",
+ "usage": {"input_tokens": 5, "output_tokens": 5, "total_tokens": 10}
+ }
+ ]
+
+ response = await litellm.aresponses(
+ model="gpt-3.5-turbo",
+ input="hi",
+ mock_response=mock_response,
+ stream=True,
+ )
+
+ # Consume the stream
+ chunks = []
+ async for chunk in response:
+ chunks.append(chunk)
+
+ await asyncio.sleep(1)
+ standard_logging_payload = test_custom_logger.logged_standard_logging_payload
+ assert standard_logging_payload is not None
+
+ # Verify redaction in ResponsesAPIResponse format
+ assert standard_logging_payload["response"] == {"text": "redacted-by-litellm"}
+ assert standard_logging_payload["messages"][0]["content"] == "redacted-by-litellm"
+ print(
+ "logged standard logging payload for ResponsesAPIResponse stream",
+ json.dumps(standard_logging_payload, indent=2),
+ )