From c69ff49b009cccc29b3beb809556197be25ec421 Mon Sep 17 00:00:00 2001 From: Abarnaa Sree <211104332+AbarnaaSree@users.noreply.github.com> Date: Sat, 19 Sep 2026 12:01:06 +0530 Subject: [PATCH] Fix streaming guardrail output masking across chunks --- litellm/proxy/utils.py | 1 + .../guardrail_hooks/test_presidio.py | 79 ++++++++++++++++++- .../proxy_logging/test_streaming_hooks.py | 78 +++++++++++++++++- 3 files changed, 156 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index a3f9924ee55..b1f34985c6e 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -3727,6 +3727,7 @@ class ProxyLogging: and resolved_callback.uses_apply_guardrail_interface() and getattr(resolved_callback, "use_native_lifecycle_hooks", False) is not True and not resolved_callback.mask_response_content + and not getattr(resolved_callback, "apply_to_output", False) ) else kind ) 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 33614d2eeca..878a92717b3 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_presidio.py @@ -4,6 +4,7 @@ Tests PII detection and masking for different message formats """ import asyncio +import json from contextlib import asynccontextmanager from unittest.mock import MagicMock, patch @@ -18,7 +19,7 @@ from litellm.proxy.guardrails.guardrail_hooks.presidio import ( ) from litellm.exceptions import GuardrailRaisedException from litellm.types.guardrails import LitellmParams, PiiAction, PiiEntityType -from litellm.types.utils import Choices, Message, ModelResponse +from litellm.types.utils import Choices, Message, ModelResponse, ModelResponseStream from litellm.exceptions import BlockedPiiEntityError @@ -2262,7 +2263,83 @@ async def test_apply_to_output_streaming_mixed_chunks_flushes_and_warns(): assert any("mixed stream detected" in msg for msg in warning_messages) assert any("unknown event objects" in msg for msg in warning_messages) +@pytest.mark.asyncio +async def test_apply_to_output_streaming_masks_email_split_across_chunks(): + """ + Regression test for PII split across streaming chunk boundaries. + The complete sensitive value is "user@example.com", but the upstream + stream delivers it as "user@exa" + "mple.com". The streaming guardrail + must evaluate the reconstructed response rather than each chunk + independently. + """ + guardrail = _OPTIONAL_PresidioPIIMasking( + mock_testing=True, + apply_to_output=True, + ) + + analyzed_texts = [] + + async def mock_check_pii( + text, + output_parse_pii, + presidio_config, + request_data, + ): + analyzed_texts.append(text) + return text.replace("user@example.com", "") + + guardrail.check_pii = mock_check_pii + + chunks = [ + ModelResponseStream( + id="chatcmpl-split-1", + choices=[ + Choices( + index=0, + delta=Message(content="Please contact user@exa"), + ) + ], + created=1, + model="gpt-4o-mini", + object="chat.completion.chunk", + ), + ModelResponseStream( + id="chatcmpl-split-2", + choices=[ + Choices( + index=0, + delta=Message(content="mple.com for support."), + ) + ], + created=2, + model="gpt-4o-mini", + object="chat.completion.chunk", + ), + ] + + async def mock_stream(): + for chunk in chunks: + yield chunk + + received = [ + chunk + async for chunk in guardrail.async_post_call_streaming_iterator_hook( + user_api_key_dict=UserAPIKeyAuth(api_key="test-key"), + response=mock_stream(), + request_data={}, + ) + ] + + reconstructed = "".join( + chunk.choices[0].delta.content or "" + for chunk in received + if isinstance(chunk, ModelResponseStream) + ) + + assert analyzed_texts == ["Please contact user@example.com for support."] + assert "" in reconstructed + assert "user@example.com" not in reconstructed # --------------------------------------------------------------------------- # Fix 4: apply_guardrail unmask path for input_type="response" # --------------------------------------------------------------------------- diff --git a/tests/test_litellm/proxy/utils/proxy_logging/test_streaming_hooks.py b/tests/test_litellm/proxy/utils/proxy_logging/test_streaming_hooks.py index 6fb000b4fa7..0b04d21baf2 100644 --- a/tests/test_litellm/proxy/utils/proxy_logging/test_streaming_hooks.py +++ b/tests/test_litellm/proxy/utils/proxy_logging/test_streaming_hooks.py @@ -27,9 +27,10 @@ from litellm.llms.anthropic.experimental_pass_through.messages.streaming_iterato BaseAnthropicMessagesStreamingIterator, ) from litellm.proxy._types import UserAPIKeyAuth +from litellm.proxy.guardrails.guardrail_hooks.presidio import _OPTIONAL_PresidioPIIMasking from litellm.proxy.utils import ProxyLogging from litellm.types.guardrails import GuardrailEventHooks -from litellm.types.utils import Usage +from litellm.types.utils import Choices, Message, ModelResponseStream, Usage @pytest.fixture(autouse=True) @@ -377,6 +378,81 @@ async def test_async_post_call_streaming_iterator_hook_with_override_chains_call assert out == ["a*", "b*"] +@pytest.mark.asyncio +async def test_async_post_call_streaming_iterator_hook_presidio_output_masking_keeps_split_pii_buffered( + proxy_logging, make_user_api_key_auth, monkeypatch +): + """ + Regression test for #41611: output PII split across SSE chunks must be + evaluated by Presidio's native streaming iterator as a reconstructed + response, rather than by the unified per-chunk guardrail path. + """ + guardrail = _OPTIONAL_PresidioPIIMasking( + mock_testing=True, + apply_to_output=True, + ) + + analyzed_texts: list[str] = [] + + async def mock_check_pii(text, output_parse_pii, presidio_config, request_data): + analyzed_texts.append(text) + return text.replace("user@example.com", "") + + guardrail.check_pii = mock_check_pii + + monkeypatch.setattr(litellm, "callbacks", [guardrail]) + + chunks = [ + ModelResponseStream( + id="chatcmpl-proxy-split-1", + choices=[ + Choices( + index=0, + delta=Message(content="Please contact user@exa"), + ) + ], + created=1, + model="gpt-4o-mini", + object="chat.completion.chunk", + ), + ModelResponseStream( + id="chatcmpl-proxy-split-2", + choices=[ + Choices( + index=0, + delta=Message(content="mple.com for support."), + ) + ], + created=2, + model="gpt-4o-mini", + object="chat.completion.chunk", + ), + ] + + async def upstream(): + for chunk in chunks: + yield chunk + + received = [ + chunk + async for chunk in proxy_logging.async_post_call_streaming_iterator_hook( + response=upstream(), + user_api_key_dict=make_user_api_key_auth(), + request_data={}, + ) + ] + + reconstructed = "".join( + chunk.choices[0].delta.content or "" + for chunk in received + if isinstance(chunk, ModelResponseStream) + ) + + assert analyzed_texts == ["Please contact user@example.com for support."] + assert "" in reconstructed + assert "user@example.com" not in reconstructed + + @pytest.mark.asyncio async def test_async_post_call_streaming_iterator_hook_upstream_error_raises(proxy_logging, make_user_api_key_auth, mock_callbacks_disabled): async def gen():