From 631aefea18e407922122b7ffc10a6d672801a3a5 Mon Sep 17 00:00:00 2001 From: Harshit28j Date: Tue, 10 Mar 2026 16:45:22 +0530 Subject: [PATCH] fix: req changes on feedback from greptile --- .../guardrails/guardrail_hooks/presidio.py | 8 ++++ .../guardrail_hooks/test_presidio.py | 45 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/litellm/proxy/guardrails/guardrail_hooks/presidio.py b/litellm/proxy/guardrails/guardrail_hooks/presidio.py index 507a0b89fc9..ddeba2100c3 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/presidio.py +++ b/litellm/proxy/guardrails/guardrail_hooks/presidio.py @@ -1150,6 +1150,14 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail): continue if not all_chunks: + # All chunks were Anthropic native SSE bytes — output + # masking cannot be applied to raw bytes. Log a warning + # so operators know PII masking was skipped for this stream. + verbose_proxy_logger.warning( + "Presidio apply_to_output: streaming response contained only " + "bytes chunks (Anthropic native SSE). Output PII masking was " + "skipped for this response." + ) return assembled_model_response = stream_chunk_builder( diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py index 296faffa1a5..32a8c1b1070 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py @@ -2185,3 +2185,48 @@ async def test_apply_guardrail_masks_on_request(): assert "" in result["texts"][0] assert "John Smith" not in result["texts"][0] + + +@pytest.mark.asyncio +async def test_apply_to_output_streaming_bytes_only_logs_warning(): + """ + Regression test: when apply_to_output=True and the stream contains only + bytes chunks (Anthropic native SSE), output masking is skipped. + A warning must be logged so operators are aware. + """ + guardrail = _OPTIONAL_PresidioPIIMasking( + mock_testing=True, + apply_to_output=True, + ) + + byte_chunks = [ + b'data: {"type":"content_block_delta","delta":{"text":"Hello"}}\n\n', + b'data: {"type":"content_block_delta","delta":{"text":" world"}}\n\n', + ] + + async def mock_stream(): + for b in byte_chunks: + yield b + + mock_user_api_key = UserAPIKeyAuth(api_key="test-key") + + collected = [] + 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={}, + ): + collected.append(chunk) + + # All bytes should be yielded through + assert len(collected) == len(byte_chunks) + for original, received in zip(byte_chunks, collected): + assert original == received + + # Warning must be logged about skipped masking + mock_logger.warning.assert_called_once() + warning_msg = mock_logger.warning.call_args[0][0] + assert "Output PII masking was skipped" in warning_msg