{"record":{"id":"3908b0b2734be66a","repo":"rohitg00/ai-engineering-from-scratch","slug":"unsafe-node-ast-dump-node","errorCode":null,"errorMessage":"unsafe node: {ast.dump(node)}","messagePattern":"unsafe node: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/49-lm-eval-harness/code/main.py","lineNumber":146,"sourceCode":"    ast.USub: operator.neg,\n    ast.Pow: operator.pow,\n}\n\n\ndef safe_arith_eval(expr: str) -> float:\n    \"\"\"Evaluate a small arithmetic expression without exposing eval.\"\"\"\n    tree = ast.parse(expr, mode=\"eval\")\n    return _safe_eval(tree.body)\n\n\ndef _safe_eval(node: ast.AST) -> float:\n    if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):\n        return node.value\n    if isinstance(node, ast.BinOp) and type(node.op) in _ARITH_OPS:\n        return _ARITH_OPS[type(node.op)](_safe_eval(node.left), _safe_eval(node.right))\n    if isinstance(node, ast.UnaryOp) and type(node.op) in _ARITH_OPS:\n        return _ARITH_OPS[type(node.op)](_safe_eval(node.operand))\n    raise ValueError(f\"unsafe node: {ast.dump(node)}\")\n\n\ndef normalize(s: str) -> str:\n    return re.sub(r\"\\s+\", \" \", s.strip().lower())\n\n\ndef metric_exact_match(prediction: str, targets: List[str]) -> float:\n    norm_pred = normalize(prediction)\n    return 1.0 if any(normalize(t) == norm_pred for t in targets) else 0.0\n\n\ndef metric_substring_contains(prediction: str, targets: List[str]) -> float:\n    norm_pred = normalize(prediction)\n    return 1.0 if any(normalize(t) in norm_pred for t in targets) else 0.0\n\n\ndef metric_multiple_choice(prediction: str, targets: List[str]) -> float:\n    pred = prediction.strip()[:1].upper()","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/49-lm-eval-harness/code/main.py#L128-L164","documentation":"Error \"unsafe node: {ast.dump(node)}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/19-capstone-projects/49-lm-eval-harness/code/main.py:146 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"}