BerriAI/litellm · error · ValueError

Unknown pattern_type: {pattern_config.pattern_type}

Error message

Unknown pattern_type: {pattern_config.pattern_type}

What it means

ValueError raised in _add_pattern when a patterns[] entry's pattern_type is neither the literal "prebuilt" nor "regex". The dispatcher matches those two strings exactly (case-sensitive), so any other value — including different casing like "Regex" or invented types like "keyword", "builtin" — falls into the else branch and is rejected.

Source

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

        Args:
            pattern_config: ContentFilterPattern configuration
        """
        try:
            extra_config: _PatternExtraLookup = {"keyword_pattern": None, "allow_word_numbers": False}
            if pattern_config.pattern_type == "prebuilt":
                if not pattern_config.pattern_name:
                    raise ValueError("pattern_name is required for prebuilt patterns")
                compiled = get_compiled_pattern(pattern_config.pattern_name)
                pattern_name = pattern_config.pattern_name
                extra_config = self._lookup_pattern_extra(pattern_name)
            elif pattern_config.pattern_type == "regex":
                if not pattern_config.pattern:
                    raise ValueError("pattern is required for regex patterns")
                compiled = re.compile(pattern_config.pattern, re.IGNORECASE)
                pattern_name = pattern_config.name or "custom_regex"
            else:
                raise ValueError(f"Unknown pattern_type: {pattern_config.pattern_type}")

            keyword_pattern: Final = extra_config["keyword_pattern"]
            keyword_regex: Final = re.compile(keyword_pattern, re.IGNORECASE) if keyword_pattern else None

            self.compiled_patterns.append(
                {
                    "regex": compiled,
                    "pattern_name": pattern_name,
                    "action": pattern_config.action,
                    "keyword_regex": keyword_regex,
                    "allow_word_numbers": extra_config["allow_word_numbers"],
                }
            )
            verbose_proxy_logger.debug("Added pattern: %s with action %s", pattern_name, pattern_config.action)
        except Exception as e:
            verbose_proxy_logger.error("Error adding pattern %s: %s", pattern_config, e)
            raise

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Set pattern_type to exactly 'prebuilt' or 'regex' (lowercase).
  2. For keyword-style blocking, use pattern_type: regex with a word-boundary regex, or the guardrail's blocked_words/blocked_words_file mechanism instead.
  3. Add a config lint that validates pattern_type against the allowed literal set before deploy.

Example fix

# before
patterns:
  - pattern_type: Keyword
    pattern_name: ssn
    action: BLOCK

# after
patterns:
  - pattern_type: prebuilt
    pattern_name: ssn
    action: BLOCK
Defensive patterns

Strategy: type-guard

Validate before calling

# Lint pattern_type against the closed set the dispatcher accepts
ALLOWED_PATTERN_TYPES = {"prebuilt", "regex"}

def lint_pattern_types(patterns: list[dict]) -> list[str]:
    return [
        f"unknown pattern_type {p.get('pattern_type')!r}: must be one of {sorted(ALLOWED_PATTERN_TYPES)} — {p}"
        for p in patterns
        if p.get("pattern_type") not in ALLOWED_PATTERN_TYPES
    ]

Type guard

from typing import Literal, TypedDict

PatternType = Literal["prebuilt", "regex"]

class PatternConfig(TypedDict):
    pattern_type: PatternType
    action: str
    pattern_name: str | None
    pattern: str | None
    name: str | None

def narrow_pattern_type(value: str) -> PatternType | None:
    """Return the value only if the dispatcher accepts it (exact, lowercase)."""
    return value if value in ("prebuilt", "regex") else None

Prevention

When it happens

Trigger: Config yaml has a patterns entry with a misspelled or unsupported pattern_type, e.g. {pattern_type: keyword, keyword: '...'} or {pattern_type: Prebuilt, pattern_name: ssn}; _add_pattern hits the else branch during initialization.

Common situations: Typo or wrong casing in pattern_type; assumption that more pattern types exist (keyword, builtin, llm); config migrated from another tool with different type names.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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