From 06377b9047776e0266ccaecf7ab41ce41522efb5 Mon Sep 17 00:00:00 2001 From: Ruiming Zhao Date: Tue, 11 Aug 2026 19:58:35 -0700 Subject: [PATCH] fix(guardrails): sync content filter logging --- .../litellm_content_filter/content_filter.py | 3 ++- .../content_filter/test_content_filter.py | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) 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 722f96ef814..aff2ccf6322 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 @@ -20,7 +20,7 @@ from fastapi import HTTPException from litellm import Router from litellm._logging import verbose_proxy_logger from litellm.constants import DEFAULT_MAX_RECURSE_DEPTH -from litellm.integrations.custom_guardrail import CustomGuardrail +from litellm.integrations.custom_guardrail import CustomGuardrail, log_guardrail_information from litellm.proxy._types import UserAPIKeyAuth from litellm.types.utils import ( CallTypes, @@ -1864,6 +1864,7 @@ class ContentFilterGuardrail(CustomGuardrail): if filtered_arguments != arguments: self._set_tool_call_arguments(tool_call, filtered_arguments) + @log_guardrail_information async def apply_guardrail( self, inputs: "GenericGuardrailAPIInputs", 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 62d25f1b9c0..933099e2e0f 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 @@ -5,6 +5,7 @@ Tests for the Content Filter Guardrail import json import os import sys +from types import SimpleNamespace from unittest.mock import MagicMock import pytest @@ -232,6 +233,27 @@ class TestContentFilterGuardrail: assert "[EMAIL_REDACTED]" in result[0] assert "test@example.com" not in result[0] + @pytest.mark.asyncio + async def test_apply_guardrail_syncs_information_to_logging_obj(self): + guardrail = ContentFilterGuardrail(guardrail_name="test-logging-sync") + request_data = {"metadata": {}} + logging_obj = SimpleNamespace( + litellm_params={"metadata": {}}, + model_call_details={}, + ) + + await guardrail.apply_guardrail( + inputs={"texts": ["A normal request"]}, + request_data=request_data, + input_type="request", + logging_obj=logging_obj, + ) + + entries = logging_obj.litellm_params["metadata"]["standard_logging_guardrail_information"] + assert len(entries) == 1 + assert entries[0]["guardrail_name"] == "test-logging-sync" + assert entries[0]["guardrail_status"] == "success" + @pytest.mark.asyncio async def test_apply_guardrail_blocked_word_mask(self): """