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]

fix(content_filter): log guardrail_information on streaming post-call (#26448)

michelligabriele9c00f9776b10
diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py
index 10b17a09ef9..3ef99e4ad60 100644
--- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py
+++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py
@@ -1866,83 +1866,122 @@ class ContentFilterGuardrail(CustomGuardrail):
For BLOCK action: Raises HTTPException immediately when blocked content is detected.
For MASK action: Content is buffered to handle patterns split across chunks.
+
+ At stream end (including when BLOCK raises HTTPException), write a
+ standard_logging_guardrail_information entry into request_data["metadata"]
+ so the post-call log row reaches standard_logging_object.guardrail_information
+ and the UI Request Lifecycle panel. Mirrors apply_guardrail's finally-block
+ contract.
"""
accumulated_full_text = ""
yielded_masked_text_len = 0
buffer_size = 50 # Increased buffer to catch patterns split across many chunks
+ start_time = datetime.now()
+ detections: List[ContentFilterDetection] = []
+ masked_entity_count: Dict[str, int] = {}
+ status: GuardrailStatus = "success"
+ exception_str: str = ""
+
verbose_proxy_logger.info(
f"ContentFilterGuardrail: Starting robust streaming masking for model {request_data.get('model')}"
)
- async for item in response:
- if isinstance(item, ModelResponseStream) and item.choices:
- delta_content = ""
- is_final = False
- for choice in item.choices:
- if hasattr(choice, "delta") and choice.delta:
- content = getattr(choice.delta, "content", None)
- if content and isinstance(content, str):
- delta_content += content
- if getattr(choice, "finish_reason", None):
- is_final = True
-
- accumulated_full_text += delta_content
-
- # Check for blocking or apply masking
- # Add a space at the end if it's the final chunk to trigger word boundaries (\b)
- text_to_check = accumulated_full_text
- if is_final:
- text_to_check += " "
-
- try:
- masked_text = self._filter_single_text(text_to_check)
- if is_final and masked_text.endswith(" "):
- masked_text = masked_text[:-1]
- except HTTPException:
- raise
- except Exception as e:
- verbose_proxy_logger.error(
- f"ContentFilterGuardrail: Error in masking: {e}"
- )
- masked_text = text_to_check # Fallback to current text
+ try:
+ async for item in response:
+ if isinstance(item, ModelResponseStream) and item.choices:
+ delta_content = ""
+ is_final = False
+ for choice in item.choices:
+ if hasattr(choice, "delta") and choice.delta:
+ content = getattr(choice.delta, "content", None)
+ if content and isinstance(content, str):
+ delta_content += content
+ if getattr(choice, "finish_reason", None):
+ is_final = True
+
+ accumulated_full_text += delta_content
+
+ # Check for blocking or apply masking
+ # Add a space at the end if it's the final chunk to trigger word boundaries (\b)
+ text_to_check = accumulated_full_text
+ if is_final:
+ text_to_check += " "
+
+ try:
+ # Reset before each scan: _filter_single_text scans the
+ # whole accumulated buffer every chunk, so previous-chunk
+ # matches are guaranteed to be re-found. Keeping only the
+ # latest scan's detections avoids N× duplication in the
+ # final log row. BLOCK still records correctly because
+ # handlers append to detections before raising.
+ detections.clear()
+ masked_text = self._filter_single_text(
+ text_to_check, detections=detections
+ )
+ if is_final and masked_text.endswith(" "):
+ masked_text = masked_text[:-1]
+ except HTTPException:
+ raise
+ except Exception as e:
+ verbose_proxy_logger.error(
+ f"ContentFilterGuardrail: Error in masking: {e}"
+ )
+ masked_text = text_to_check # Fallback to current text
- # Determine how much can be safely yielded
- if is_final:
- safe_to_yield_len = len(masked_text)
- else:
- safe_to_yield_len = max(0, len(masked_text) - buffer_size)
-
- if safe_to_yield_len > yielded_masked_text_len:
- new_masked_content = masked_text[
- yielded_masked_text_len:safe_to_yield_len
- ]
- # Modify the chunk to contain only the new masked content
- if (
- item.choices
- and hasattr(item.choices[0], "delta")
- and item.choices[0].delta
- ):
- item.choices[0].delta.content = new_masked_content
- yielded_masked_text_len = safe_to_yield_len
+ # Determine how much can be safely yielded
+ if is_final:
+ safe_to_yield_len = len(masked_text)
+ else:
+ safe_to_yield_len = max(0, len(masked_text) - buffer_size)
+
+ if safe_to_yield_len > yielded_masked_text_len:
+ new_masked_content = masked_text[
+ yielded_masked_text_len:safe_to_yield_len
+ ]
+ # Modify the chunk to contain only the new masked content
+ if (
+ item.choices
+ and hasattr(item.choices[0], "delta")
+ and item.choices[0].delta
+ ):
+ item.choices[0].delta.content = new_masked_content
+ yielded_masked_text_len = safe_to_yield_len
+ yield item
+ else:
+ # Hold content by yielding empty content chunk (keeps metadata/structure)
+ if (
+ item.choices
+ and hasattr(item.choices[0], "delta")
+ and item.choices[0].delta
+ ):
+ item.choices[0].delta.content = ""
yield item
else:
- # Hold content by yielding empty content chunk (keeps metadata/structure)
- if (
- item.choices
- and hasattr(item.choices[0], "delta")
- and item.choices[0].delta
- ):
- item.choices[0].delta.content = ""
+ # Not a ModelResponseStream or no choices - yield as is
yield item
- else:
- # Not a ModelResponseStream or no choices - yield as is
- yield item
- # Any remaining content (should have been handled by is_final, but just in case)
- if yielded_masked_text_len < len(accumulated_full_text):
- # We already reached the end of the generator
- pass
+ # Any remaining content (should have been handled by is_final, but just in case)
+ if yielded_masked_text_len < len(accumulated_full_text):
+ # We already reached the end of the generator
+ pass
+ except HTTPException:
+ status = "guardrail_intervened"
+ raise
+ except Exception as e:
+ status = "guardrail_failed_to_respond"
+ exception_str = str(e)
+ raise e
+ finally:
+ self._count_masked_entities(detections, masked_entity_count)
+ self._log_guardrail_information(
+ request_data=request_data,
+ detections=detections,
+ status=status,
+ start_time=start_time,
+ masked_entity_count=masked_entity_count,
+ exception_str=exception_str,
+ )
@staticmethod
def get_config_model():
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py
index a1d3eb152bb..f7377926efc 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/content_filter/test_content_filter.py
@@ -501,6 +501,285 @@ class TestContentFilterGuardrail:
assert exc_info.value.status_code == 403
assert "us_ssn" in str(exc_info.value.detail)
+ @pytest.mark.asyncio
+ async def test_streaming_hook_logs_guardrail_information_clean(self):
+ """
+ Streaming post-call: when no detections fire, still write a success log row
+ into request_data["metadata"]["standard_logging_guardrail_information"].
+
+ Regression test for: UI-created ContentFilterGuardrail in mode=post_call
+ never produced a `standard_logging_object.guardrail_information` entry on
+ streaming responses (Branch 1 dispatcher + iterator hook had no log write).
+ """
+ from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
+
+ patterns = [
+ ContentFilterPattern(
+ pattern_type="prebuilt",
+ pattern_name="email",
+ action=ContentFilterAction.MASK,
+ ),
+ ]
+
+ guardrail = ContentFilterGuardrail(
+ guardrail_name="test-streaming-logging-clean",
+ patterns=patterns,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def mock_stream():
+ yield ModelResponseStream(
+ id="chunk1",
+ choices=[
+ StreamingChoices(
+ delta=Delta(content="Hello world, nothing sensitive here."),
+ index=0,
+ finish_reason="stop",
+ )
+ ],
+ model="gpt-4",
+ )
+
+ user_api_key_dict = MagicMock()
+ request_data = {
+ "messages": [{"role": "user", "content": "Hi"}],
+ "model": "gpt-4o",
+ "metadata": {},
+ }
+
+ async for _ in guardrail.async_post_call_streaming_iterator_hook(
+ user_api_key_dict=user_api_key_dict,
+ response=mock_stream(),
+ request_data=request_data,
+ ):
+ pass
+
+ assert "standard_logging_guardrail_information" in request_data["metadata"]
+ entries = request_data["metadata"]["standard_logging_guardrail_information"]
+ assert isinstance(entries, list)
+ assert len(entries) == 1
+ entry = entries[0]
+ assert entry["guardrail_name"] == "test-streaming-logging-clean"
+ assert entry["guardrail_provider"] == "litellm_content_filter"
+ assert entry["guardrail_status"] == "success"
+ assert entry["guardrail_response"] == []
+
+ @pytest.mark.asyncio
+ async def test_streaming_hook_logs_guardrail_information_mask(self):
+ """
+ Streaming post-call with a MASK pattern: writes a success row with the
+ detection in guardrail_response and a non-zero masked_entity_count.
+ """
+ from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
+
+ patterns = [
+ ContentFilterPattern(
+ pattern_type="prebuilt",
+ pattern_name="email",
+ action=ContentFilterAction.MASK,
+ ),
+ ]
+
+ guardrail = ContentFilterGuardrail(
+ guardrail_name="test-streaming-logging-mask",
+ patterns=patterns,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def mock_stream():
+ yield ModelResponseStream(
+ id="chunk1",
+ choices=[
+ StreamingChoices(
+ delta=Delta(content="Contact me at [email protected] please."),
+ index=0,
+ finish_reason="stop",
+ )
+ ],
+ model="gpt-4",
+ )
+
+ user_api_key_dict = MagicMock()
+ request_data = {
+ "messages": [{"role": "user", "content": "Hi"}],
+ "model": "gpt-4o",
+ "metadata": {},
+ }
+
+ full_content = ""
+ async for chunk in guardrail.async_post_call_streaming_iterator_hook(
+ user_api_key_dict=user_api_key_dict,
+ response=mock_stream(),
+ request_data=request_data,
+ ):
+ if chunk.choices and chunk.choices[0].delta.content:
+ full_content += chunk.choices[0].delta.content
+
+ # Content was masked, not blocked
+ assert "[email protected]" not in full_content
+ assert "[EMAIL_REDACTED]" in full_content
+
+ # And the log row was written
+ entries = request_data["metadata"]["standard_logging_guardrail_information"]
+ assert len(entries) == 1
+ entry = entries[0]
+ assert entry["guardrail_status"] == "success"
+ assert entry["guardrail_provider"] == "litellm_content_filter"
+
+ detections = entry["guardrail_response"]
+ assert isinstance(detections, list)
+ # At least one pattern detection for 'email' should be recorded
+ pattern_detections = [d for d in detections if d.get("type") == "pattern"]
+ assert pattern_detections, f"expected pattern detections, got {detections}"
+ assert any(d.get("pattern_name") == "email" for d in pattern_detections)
+ # And masked_entity_count for email should be >= 1
+ assert entry["masked_entity_count"].get("email", 0) >= 1
+
+ @pytest.mark.asyncio
+ async def test_streaming_hook_logs_guardrail_information_block(self):
+ """
+ Streaming post-call with a BLOCK pattern: the hook raises HTTPException,
+ but the `finally` block still writes a log row with
+ guardrail_status == "guardrail_intervened".
+ """
+ from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
+
+ patterns = [
+ ContentFilterPattern(
+ pattern_type="prebuilt",
+ pattern_name="us_ssn",
+ action=ContentFilterAction.BLOCK,
+ ),
+ ]
+
+ guardrail = ContentFilterGuardrail(
+ guardrail_name="test-streaming-logging-block",
+ patterns=patterns,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ async def mock_stream():
+ yield ModelResponseStream(
+ id="chunk1",
+ choices=[
+ StreamingChoices(delta=Delta(content="SSN: 123-45-6789"), index=0)
+ ],
+ model="gpt-4",
+ )
+
+ user_api_key_dict = MagicMock()
+ request_data = {
+ "messages": [{"role": "user", "content": "Hi"}],
+ "model": "gpt-4o",
+ "metadata": {},
+ }
+
+ with pytest.raises(HTTPException):
+ async for _ in guardrail.async_post_call_streaming_iterator_hook(
+ user_api_key_dict=user_api_key_dict,
+ response=mock_stream(),
+ request_data=request_data,
+ ):
+ pass
+
+ # Even though HTTPException bubbled up, the log row must be written.
+ assert "standard_logging_guardrail_information" in request_data["metadata"]
+ entries = request_data["metadata"]["standard_logging_guardrail_information"]
+ assert len(entries) == 1
+ entry = entries[0]
+ assert entry["guardrail_name"] == "test-streaming-logging-block"
+ assert entry["guardrail_status"] == "guardrail_intervened"
+
+ @pytest.mark.asyncio
+ async def test_streaming_hook_logs_no_duplicate_detections_across_chunks(self):
+ """
+ Multi-chunk regression: _filter_single_text re-scans the whole accumulated
+ buffer on every chunk, so without per-chunk reset the same detection would
+ be appended once per chunk after it first appears, inflating
+ masked_entity_count and guardrail_response in the log.
+ """
+ from litellm.types.utils import Delta, ModelResponseStream, StreamingChoices
+
+ patterns = [
+ ContentFilterPattern(
+ pattern_type="prebuilt",
+ pattern_name="email",
+ action=ContentFilterAction.MASK,
+ ),
+ ]
+
+ guardrail = ContentFilterGuardrail(
+ guardrail_name="test-streaming-logging-no-duplicates",
+ patterns=patterns,
+ event_hook=GuardrailEventHooks.post_call,
+ )
+
+ # The email straddles chunks 1 and 2; chunks 3 and 4 keep streaming after
+ # the match has already been found, exercising the re-scan path.
+ async def mock_stream():
+ yield ModelResponseStream(
+ id="c1",
+ choices=[
+ StreamingChoices(
+ delta=Delta(content="Contact me at test@"), index=0
+ )
+ ],
+ model="gpt-4",
+ )
+ yield ModelResponseStream(
+ id="c2",
+ choices=[
+ StreamingChoices(
+ delta=Delta(content="example.com please."), index=0
+ )
+ ],
+ model="gpt-4",
+ )
+ yield ModelResponseStream(
+ id="c3",
+ choices=[StreamingChoices(delta=Delta(content=" Thanks!"), index=0)],
+ model="gpt-4",
+ )
+ yield ModelResponseStream(
+ id="c4",
+ choices=[
+ StreamingChoices(
+ delta=Delta(content=""), index=0, finish_reason="stop"
+ )
+ ],
+ model="gpt-4",
+ )
+
+ user_api_key_dict = MagicMock()
+ request_data = {
+ "messages": [{"role": "user", "content": "Hi"}],
+ "model": "gpt-4o",
+ "metadata": {},
+ }
+
+ async for _ in guardrail.async_post_call_streaming_iterator_hook(
+ user_api_key_dict=user_api_key_dict,
+ response=mock_stream(),
+ request_data=request_data,
+ ):
+ pass
+
+ entries = request_data["metadata"]["standard_logging_guardrail_information"]
+ assert len(entries) == 1
+ entry = entries[0]
+ assert entry["guardrail_status"] == "success"
+
+ detections = entry["guardrail_response"]
+ pattern_detections = [d for d in detections if d.get("type") == "pattern"]
+ # Exactly one email detection — not one per chunk after the match appeared.
+ assert len(pattern_detections) == 1, (
+ f"expected exactly 1 email detection, got {len(pattern_detections)}: "
+ f"{detections}"
+ )
+ assert pattern_detections[0].get("pattern_name") == "email"
+ # masked_entity_count for email is the real count, not N×.
+ assert entry["masked_entity_count"].get("email") == 1
+
def test_init_with_plain_dicts(self):
"""
Test initialization with plain dicts (DB format).