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(guardrails): log mixed-stream passthrough and cover flush path
Sameer Kankuteb4f4a931f752
diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.pyindex 3ee28ec42a7..fc414ab7b54 100644--- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py+++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py@@ -1174,12 +1174,23 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail):else:if all_chunks:# Flush buffered chunks and switch to transparent passthrough for this stream shape.+ # NOTE: these buffered chunks are emitted unmasked because this+ # stream mixed chunk types and cannot be safely reconstructed.+ verbose_proxy_logger.warning(+ "Presidio apply_to_output: mixed stream detected (ModelResponseStream + unknown event). "+ "Flushing %d buffered chunks without PII masking and switching to transparent passthrough.",+ len(all_chunks),+ )for buffered_chunk in all_chunks:yield buffered_chunkall_chunks = []passthrough_due_to_unknown_stream_shape = Trueyield chunkif passthrough_due_to_unknown_stream_shape:+ verbose_proxy_logger.warning(+ "Presidio apply_to_output: streaming response contained unknown event objects "+ "(e.g. /v1/responses events). Output PII masking was skipped for this response."+ )returnif not all_chunks:verbose_proxy_logger.warning(diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.pyindex a8c57e97b4a..565bf83c6a2 100644--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py@@ -2162,6 +2162,60 @@ async def test_apply_to_output_streaming_unknown_events_passthrough():]+async def test_apply_to_output_streaming_mixed_chunks_flushes_and_warns():+ """+ Regression test for mixed stream shape:+ a buffered ModelResponseStream chunk followed by unknown responses-style+ events should be preserved, and masking skip should be visible via warnings.+ """+ guardrail = _OPTIONAL_PresidioPIIMasking(+ mock_testing=True,+ apply_to_output=True,+ )++ class FakeResponsesEvent:+ def __init__(self, event_type: str):+ self.type = event_type++ model_chunk = ModelResponseStream(+ id="chatcmpl-mixed-1",+ choices=[],+ created=1,+ model="gpt-4",+ object="chat.completion.chunk",+ system_fingerprint=None,+ )+ response_completed = FakeResponsesEvent("response.completed")++ async def mock_stream():+ yield model_chunk+ yield response_completed++ mock_user_api_key = UserAPIKeyAuth(api_key="test-key")+ received = []+ with patch(+ "litellm.proxy.guardrails.guardrail_hooks.presidio.verbose_proxy_logger"+ ) as mock_logger:+ async for chunk in guardrail.async_post_call_streaming_iterator_hook(+ user_api_key_dict=mock_user_api_key,+ response=mock_stream(),+ request_data={},+ ):+ received.append(chunk)++ # Preserve original ordering across mixed stream types.+ assert received == [model_chunk, response_completed]++ # Two warnings are expected:+ # 1) mixed stream detected + unmasked flush+ # 2) passthrough mode skipped output masking+ assert mock_logger.warning.call_count == 2+ warning_messages = [call.args[0] for call in mock_logger.warning.call_args_list]+ assert any("mixed stream detected" in msg for msg in warning_messages)+ assert any("unknown event objects" in msg for msg in warning_messages)++# ---------------------------------------------------------------------------# Fix 4: apply_guardrail unmask path for input_type="response"# ---------------------------------------------------------------------------