{"record":{"id":"37ea0944c85c3d98","repo":"datawhalechina/hello-agents","slug":"problem-answer","errorCode":null,"errorMessage":"缺少必需字段: problem 或 answer","messagePattern":"缺少必需字段: problem 或 answer","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"code/chapter12/data_generation/aime_generator.py","lineNumber":216,"sourceCode":"            # 方法：先将字符串中的单个反斜杠替换为双反斜杠（但保留已经转义的）\n            # 这样LaTeX的 \\frac 会变成 \\\\frac，在JSON中是合法的\n\n            # 使用正则表达式：找到所有未转义的反斜杠（不是\\\\的\\）\n            # 并将其替换为\\\\\n            fixed_json_str = re.sub(r'(?<!\\\\)\\\\(?![\"\\\\/bfnrtu])', r'\\\\\\\\', json_str)\n\n            try:\n                problem_data = json.loads(fixed_json_str)\n            except json.JSONDecodeError:\n                # 如果还是失败，打印错误信息并抛出\n                print(f\"❌ JSON解析失败:\")\n                print(f\"原始响应: {response[:500]}...\")\n                print(f\"提取的JSON: {json_str[:500]}...\")\n                raise\n\n        # 验证必需字段\n        if \"problem\" not in problem_data or \"answer\" not in problem_data:\n            raise ValueError(\"缺少必需字段: problem 或 answer\")\n\n        # 验证答案范围\n        answer = int(problem_data.get(\"answer\", 0))\n        if not (0 <= answer <= 999):\n            print(f\"⚠️ 答案超出范围: {answer}，调整为0-999范围内\")\n            answer = max(0, min(999, answer))\n            problem_data[\"answer\"] = answer\n\n        # 确保有默认值\n        problem_data.setdefault(\"solution\", \"No solution provided\")\n        problem_data.setdefault(\"topic\", \"Uncategorized\")\n\n        return problem_data\n\n    def _get_default_problem(self) -> Dict[str, Any]:\n        \"\"\"获取默认题目（生成失败时使用）\"\"\"\n        return {\n            \"problem\": \"生成失败，请重新生成\",","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/code/chapter12/data_generation/aime_generator.py#L198-L234","documentation":"ValueError raised by the AIME-style problem generator after parsing the LLM's JSON output: the parsed object lacks the required 'problem' or 'answer' key. It fires only after json.loads succeeded, so the model returned syntactically valid JSON with the wrong schema.","triggerScenarios":"The LLM returns {\"question\": ..., \"ans\": ...} or nested/wrapped objects instead of the flat {problem, answer, ...} schema; the model wraps data in markdown or prose that survived the earlier extraction but shifted keys; weak model or truncated prompt not showing the required schema.","commonSituations":"Switching to a smaller/cheaper model that ignores the JSON schema; prompt template drift after refactoring; temperature too high producing creative key names; few-shot examples out of sync with the validation code.","solutions":["Inspect the printed '原始响应'/'提取的JSON' output to see exactly which keys the model produced","Strengthen the prompt: show an explicit JSON schema and an example with keys problem/answer, and state that other keys are optional but these two are mandatory","Add a repair pass: on missing keys, re-ask the model with the invalid JSON and the error before giving up","Pin a stronger model or lower temperature for this generation step"],"exampleFix":"# before\nif \"problem\" not in problem_data or \"answer\" not in problem_data:\n    raise ValueError(\"缺少必需字段: problem 或 answer\")\n\n# after: one retry with schema feedback, then fail\nif \"problem\" not in problem_data or \"answer\" not in problem_data:\n    problem_data = retry_with_schema_feedback(response)\n    if \"problem\" not in problem_data or \"answer\" not in problem_data:\n        raise ValueError(\"缺少必需字段: problem 或 answer\")","handlingStrategy":"validation","validationCode":"REQUIRED = ('problem', 'answer')\n\ndef is_valid_problem(d) -> bool:\n    return isinstance(d, dict) and all(k in d for k in REQUIRED)","typeGuard":"def is_valid_problem(d) -> bool:\n    return (\n        isinstance(d, dict)\n        and isinstance(d.get('problem'), str)\n        and str(d.get('answer', '')).lstrip('-').isdigit()\n    )","tryCatchPattern":"try:\n    problem_data = parse_and_validate(response)\nexcept ValueError:\n    problem_data = regenerate_with_schema_feedback(response)  # one repair pass\n    if not is_valid_problem(problem_data):\n        raise","preventionTips":["Include an explicit JSON schema plus a gold example in the generation prompt","Validate parsed dicts with a small type-guard before persisting","Log raw model responses on validation failure to detect prompt drift early"],"tags":["llm","json","data-generation","validation"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}