From 8288e351ef203bb4ea28577aaf7b7374771697e5 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Tue, 24 Feb 2026 09:14:10 +0530 Subject: [PATCH] Fix: test_sentence[dt_8] --- .../litellm_content_filter/content_filter.py | 21 ++++++++++++++++++- .../sg_mas_human_oversight.yaml | 8 +++---- .../sg_mas_transparency_explainability.yaml | 3 ++- 3 files changed, 26 insertions(+), 6 deletions(-) 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 d569e69e56..d0ad9eb404 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 @@ -214,6 +214,10 @@ class ContentFilterGuardrail(CustomGuardrail): self.category_keywords: Dict[str, Tuple[str, str, ContentFilterAction]] = ( {} ) # keyword -> (category, severity, action) + # Always-block keywords are checked before exceptions (cannot be bypassed) + self.always_block_category_keywords: Dict[ + str, Tuple[str, str, ContentFilterAction] + ] = {} # Store conditional categories (identifier_words + block_words) self.conditional_categories: Dict[str, Dict[str, Any]] = ( {} @@ -452,7 +456,7 @@ class ContentFilterGuardrail(CustomGuardrail): keyword = keyword_data["keyword"].lower() severity = keyword_data.get("severity", "high") if self._should_apply_severity(severity, severity_threshold): - self.category_keywords[keyword] = ( + self.always_block_category_keywords[keyword] = ( category_name, severity, category_action, @@ -1064,6 +1068,20 @@ class ContentFilterGuardrail(CustomGuardrail): """ text_lower = text.lower() + # Always-block keywords are matched first — exceptions do not apply to them. + for keyword, (category, severity, action) in self.always_block_category_keywords.items(): + keyword_pattern_str = self._keyword_to_regex_pattern(keyword) + if " " in keyword: + keyword_found = bool(re.search(keyword_pattern_str, text_lower)) + else: + keyword_pattern = r"\b" + keyword_pattern_str + r"\b" + keyword_found = bool(re.search(keyword_pattern, text_lower)) + if keyword_found: + verbose_proxy_logger.debug( + f"Always-block keyword '{keyword}' found in category '{category}'" + ) + return (keyword, category, severity, action) + # First check if any exception applies for exception in exceptions: if exception in text_lower: @@ -1571,6 +1589,7 @@ class ContentFilterGuardrail(CustomGuardrail): len(self.compiled_patterns) + len(self.blocked_words) + len(self.category_keywords) + + len(self.always_block_category_keywords) ) def _get_policy_templates(self) -> Optional[str]: diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_human_oversight.yaml b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_human_oversight.yaml index dfca04f901..0e9e336dbe 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_human_oversight.yaml +++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_human_oversight.yaml @@ -74,16 +74,16 @@ always_block_keywords: exceptions: - "human in the loop" - "human on the loop" - - "human review" - - "human oversight" + - "add human review" + - "with human review" - "manual review" - "escalation" - "appeals process" - "override mechanism" - - "explain" + - "explain what" + - "explain how" - "what is" - "research" - "improve oversight" - - "add human review" - "compliance" - "governance" diff --git a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_transparency_explainability.yaml b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_transparency_explainability.yaml index 19b999729b..0a08abd86e 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_transparency_explainability.yaml +++ b/litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/policy_templates/sg_mas_transparency_explainability.yaml @@ -73,7 +73,8 @@ exceptions: - "interpretability" - "model card" - "audit trail" - - "explain" + - "explain what" + - "explain how" - "what is" - "SHAP" - "LIME"