rohitg00/ai-engineering-from-scratch · error · ValueError
rule {r.get('name')} missing substring or regex
Error message
rule {r.get('name')} missing substring or regex What it means
Error "rule {r.get('name')} missing substring or regex" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/19-capstone-projects/83-prompt-injection-detector/code/main.py:135
score = sum(1 for w in words if w in _COMMON_WORDS)
if score >= 2:
return text + " " + candidate
return text
class Detector:
def __init__(self, rules: Iterable[dict[str, object]] | None = None) -> None:
rules_list = list(rules) if rules is not None else all_rules()
self.substring_rules: list[dict[str, object]] = []
self.regex_rules: list[dict[str, object]] = []
for r in rules_list:
if "substring" in r:
self.substring_rules.append(r)
elif "regex" in r:
compiled = re.compile(str(r["regex"]), re.IGNORECASE | re.DOTALL)
self.regex_rules.append({**r, "_compiled": compiled})
else:
raise ValueError(f"rule {r.get('name')} missing substring or regex")
def analyze(self, prompt: str) -> Verdict:
normalized = normalize(prompt)
haystacks = (prompt.lower(), normalized)
scores_by_category: dict[str, float] = {}
fired: list[str] = []
for r in self.substring_rules:
needle = str(r["substring"]).lower()
if any(needle in h for h in haystacks):
cat = str(r["category"])
score = float(r["score"])
scores_by_category[cat] = max(scores_by_category.get(cat, 0.0), score)
fired.append(str(r["name"]))
for r in self.regex_rules:
compiled: re.Pattern = r["_compiled"]
if any(compiled.search(h) for h in haystacks):
cat = str(r["category"])
score = float(r["score"])View on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/19-capstone-projects/83-prompt-injection-detector/code/main.py:135 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/bf1995a865a9c1fc.
Report an issue: GitHub.