{"record":{"id":"036366f40cf52f81","repo":"rohitg00/ai-engineering-from-scratch","slug":"constitution-must-be-a-mapping-with-a-rules-key","errorCode":null,"errorMessage":"constitution must be a mapping with a 'rules' key","messagePattern":"constitution must be a mapping with a 'rules' key","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/86-constitutional-rules-engine/code/main.py","lineNumber":133,"sourceCode":"        return (m is not None), (m.group(0) if m else None)\n    if \"max_words\" in node:\n        wc = _word_count(text)\n        return (wc <= int(node[\"max_words\"])), (f\"word count {wc}\" if wc > int(node[\"max_words\"]) else None)\n    if \"min_words\" in node:\n        wc = _word_count(text)\n        return (wc >= int(node[\"min_words\"])), (f\"word count {wc}\" if wc < int(node[\"min_words\"]) else None)\n    raise ValueError(f\"unknown predicate: {list(node.keys())}\")\n\n\nclass Engine:\n    def __init__(self, rules: list[dict[str, Any]] | None = None, path: Path | None = None) -> None:\n        if rules is not None:\n            self._rules = rules\n        else:\n            target = path or DEFAULT_RULES_PATH\n            data = load_yaml(target.read_text())\n            if not isinstance(data, dict) or \"rules\" not in data:\n                raise ValueError(\"constitution must be a mapping with a 'rules' key\")\n            self._rules = data[\"rules\"]\n        for r in self._rules:\n            if r.get(\"severity\") not in SEVERITY_ORDER:\n                raise ValueError(f\"rule {r.get('name')} has bad severity {r.get('severity')}\")\n            if \"name\" not in r or \"explanation\" not in r or \"must\" not in r:\n                raise ValueError(f\"rule {r.get('name')} missing required field\")\n\n    def rules(self) -> list[dict[str, Any]]:\n        return list(self._rules)\n\n    def evaluate(self, text: str) -> EngineReport:\n        report = EngineReport(text=text)\n        for rule in self._rules:\n            name = str(rule[\"name\"])\n            sev = str(rule[\"severity\"])\n            expl = str(rule[\"explanation\"])\n            applies, _ = _eval_predicate(rule.get(\"applies_when\"), text)\n            if not applies:","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/86-constitutional-rules-engine/code/main.py#L115-L151","documentation":"Error \"constitution must be a mapping with a 'rules' key\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/86-constitutional-rules-engine/code/main.py:133 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}