BerriAI/litellm · error · ValueError
pattern is required for regex patterns
Error message
pattern is required for regex patterns
What it means
ValueError raised in _add_pattern when a patterns[] entry declares pattern_type: "regex" but omits pattern. For regex patterns the `pattern` field is the regex source that gets re.compile()'d (case-insensitive); without it there is nothing to match.
Source
Thrown at litellm/proxy/guardrails/guardrail_hooks/litellm_content_filter/content_filter.py:744
def _add_pattern(self, pattern_config: ContentFilterPattern) -> None:
"""
Add a pattern to the compiled patterns list.
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)View on GitHub (pinned to 77b7c6c40c)
Solutions
- Add the pattern field with the regex source, e.g. pattern: '\\d{3}-\\d{2}-\\d{4}'.
- Verify the regex compiles case-insensitively (it is compiled with re.IGNORECASE) — test it locally with re.compile first.
- Use name (optional) for a human-readable label; only pattern drives matching.
- Quote YAML strings containing backslashes so the escape sequences survive parsing.
Example fix
# before
patterns:
- pattern_type: regex
name: internal-account
action: BLOCK
# after
patterns:
- pattern_type: regex
name: internal-account
pattern: 'ACCT-\\d{6}'
action: BLOCK Defensive patterns
Strategy: validation
Validate before calling
# Lint pattern entries: regex requires a compilable pattern
import re
def lint_regex_patterns(patterns: list[dict]) -> list[str]:
problems = []
for p in patterns:
if p.get("pattern_type") == "regex":
pat = p.get("pattern")
if not pat:
problems.append(f"regex pattern missing 'pattern': {p}")
else:
try:
re.compile(pat, re.IGNORECASE)
except re.error as e:
problems.append(f"invalid regex {pat!r}: {e}")
return problems Type guard
def is_valid_regex_pattern(entry: dict) -> bool:
"""True when entry is a regex pattern with a non-empty, compilable source."""
if entry.get("pattern_type") != "regex":
return False
pat = entry.get("pattern")
if not isinstance(pat, str) or not pat:
return False
import re
try:
re.compile(pat, re.IGNORECASE)
return True
except re.error:
return False Prevention
- Single-quote YAML regex strings so backslashes survive (pattern: '\\d{3}-\\d{2}-\\d{4}').
- Test every regex with re.compile(pat, re.IGNORECASE) before it reaches the proxy.
- Use name for the human label; pattern is what matches.
When it happens
Trigger: Config yaml contains litellm_params.patterns with an entry like {pattern_type: regex, action: MASK, name: my-rule} that lacks the pattern field.
Common situations: Example config copy-paste where the regex string was removed; author wrote the regex into `name` or `pattern_name` instead of `pattern`; YAML quoting issues silently dropped a multiline regex.
Related errors
- pattern_name is required for prebuilt patterns
- Content Filter: guardrail_name is required
- Unknown pattern_type: {pattern_config.pattern_type}
- Invalid format: file must contain 'blocked_words' key with l
- Error loading blocked words file {file_path}: {e}
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/1c4af7bc0e15e2ab.
Report an issue: GitHub.