BerriAI/litellm · error · HTTPException

Content blocked: {pattern_name} pattern detected

Error message

Content blocked: {pattern_name} pattern detected

What it means

HTTPException(400) raised by _handle_pattern_match when a compiled regex pattern (prebuilt catalog entries like SSN/credit card, or custom regex entries) matches the text with action BLOCK. The pattern_name in the message identifies which configured pattern fired; for MASK the handler would redact with pattern_redaction_format instead of raising.

Source

Thrown at litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py:1310

        pattern_name: str,
        action: ContentFilterAction,
        text: str,
        spans: list[tuple[int, int]],
        detections: list[ContentFilterDetection] | None,
    ) -> str:
        """Handle regex pattern match detection and action."""
        if detections is not None:
            pattern_detection: Final[PatternDetection] = {
                "type": "pattern",
                "pattern_name": pattern_name,
                "action": action.value,
            }
            detections.append(pattern_detection)

        if action == ContentFilterAction.BLOCK:
            error_msg: Final = f"Content blocked: {pattern_name} pattern detected"
            verbose_proxy_logger.warning(error_msg)
            raise HTTPException(
                status_code=400,
                detail={"error": error_msg, "pattern": pattern_name},
            )
        elif action == ContentFilterAction.MASK:
            redaction_tag: Final = self.pattern_redaction_format.format(pattern_name=pattern_name.upper())
            text = self._mask_spans(text, spans, redaction_tag)
            verbose_proxy_logger.info("Masked all %s matches in content", pattern_name)

        return text

    def _handle_blocked_word_match(
        self,
        keyword: str,
        action: ContentFilterAction,
        description: str | None,
        text: str,
        detections: list[ContentFilterDetection] | None,
    ) -> str:

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Check the 400 detail's pattern field to see which pattern matched, and inspect the input for the offending content.
  2. If the content is legitimate (e.g. test data, synthetic numbers), change that pattern's action from BLOCK to MASK so matches are redacted instead of rejected.
  3. Tighten or replace an over-broad regex that produces false positives.
  4. Handle the 400 client-side as a deterministic policy rejection — no retry.

Example fix

# before — hard block on SSN-like content
patterns:
  - pattern_type: prebuilt
    pattern_name: ssn
    action: BLOCK

# after — redact instead of block
patterns:
  - pattern_type: prebuilt
    pattern_name: ssn
    action: MASK
Defensive patterns

Strategy: try-catch

Validate before calling

# Client-side pre-screen: run the same regexes before sending to the API
import re

PATTERNS = {"ssn": re.compile(r"\d{3}-\d{2}-\d{4}", re.IGNORECASE)}

def would_block(text: str, blocklist: set[str] = {"ssn"}) -> str | None:
    for name in blocklist:
        if PATTERNS[name].search(text):
            return name
    return None

# if p := would_block(prompt): scrub or reject locally before the API call

Type guard

from fastapi import HTTPException

def is_pattern_block(exc: HTTPException) -> bool:
    d = exc.detail if isinstance(exc.detail, dict) else {}
    return exc.status_code == 400 and isinstance(d.get("pattern"), str) and "pattern detected" in str(d.get("error", ""))

Try / catch

try:
    resp = await client.chat.completions.create(**params)
except HTTPException as e:
    if is_pattern_block(e):
        pattern = e.detail["pattern"]
        raise PolicyRejectedError(
            user_message=f"Content matched the '{pattern}' filter and was blocked",
        ) from e
    raise

Prevention

When it happens

Trigger: A guarded request's text matches a regex from litellm_params.patterns (or a pattern inherited from a policy template) whose action is BLOCK — e.g. a 9-digit SSN-like string triggering the 'ssn' prebuilt pattern.

Common situations: See trigger scenarios.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/aa11122940b099f9. Report an issue: GitHub.