{"record":{"id":"cc79f7150f9c5a3a","repo":"datawhalechina/hello-agents","slug":"json-error","errorCode":null,"errorMessage":"JSON 校验失败且没有剩余调用次数：{error}","messagePattern":"JSON 校验失败且没有剩余调用次数：(.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"Co-creation-projects/Henry2513-MeetingActionAgent/main.ipynb","lineNumber":321,"sourceCode":"    \"        self.used += 1\\n\",\n    \"        return agent.run(prompt)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"# 调用 Agent 并将响应解析为指定的数据模型。\\n\",\n    \"def run_structured(\\n\",\n    \"    agent,\\n\",\n    \"    prompt: str,\\n\",\n    \"    model_type: type[BaseModel],\\n\",\n    \"    budget: CallBudget,\\n\",\n    \") -> BaseModel:\\n\",\n    \"    schema = json.dumps(model_type.model_json_schema(), ensure_ascii=False)\\n\",\n    \"    full_prompt = f\\\"{prompt}\\\\n\\\\n必须遵循以下 JSON Schema：\\\\n{schema}\\\"\\n\",\n    \"    raw_response = budget.run(agent, full_prompt)\\n\",\n    \"    try:\\n\",\n    \"        return parse_model_response(raw_response, model_type)\\n\",\n    \"    except ValueError as error:\\n\",\n    \"        if budget.remaining <= 0:\\n\",\n    \"            raise RuntimeError(f\\\"JSON 校验失败且没有剩余调用次数：{error}\\\") from error\\n\",\n    \"        repair_prompt = (\\n\",\n    \"            \\\"上一次响应无法通过 JSON 校验。不要改变内容含义，只修复格式。\\\\n\\\"\\n\",\n    \"            f\\\"校验错误：{error}\\\\n\\\"\\n\",\n    \"            f\\\"原响应：\\\\n{raw_response}\\\\n\\\"\\n\",\n    \"            f\\\"目标 Schema：\\\\n{schema}\\\\n\\\"\\n\",\n    \"            \\\"只返回修复后的 JSON。\\\"\\n\",\n    \"        )\\n\",\n    \"        repaired_response = budget.run(agent, repair_prompt)\\n\",\n    \"        return parse_model_response(repaired_response, model_type)\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"# 创建 MinutesAgent 和 ReviewAgent。\\n\",\n    \"def build_agents():\\n\",\n    \"    llm = HelloAgentsLLM()\\n\",\n    \"    minutes_agent = SimpleAgent(name=\\\"MinutesAgent\\\", llm=llm, system_prompt=MINUTES_SYSTEM_PROMPT)\\n\",\n    \"    review_agent = SimpleAgent(name=\\\"ReviewAgent\\\", llm=llm, system_prompt=REVIEW_SYSTEM_PROMPT)\\n\",\n    \"    return minutes_agent, review_agent\\n\"\n   ]","sourceCodeStart":303,"sourceCodeEnd":339,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/Henry2513-MeetingActionAgent/main.ipynb#L303-L339","documentation":"A RuntimeError raised by run_structured when parsing the model response fails (ValueError from extract_json_object or JSONDecodeError, both ValueError subclasses) and the budget has no calls left for a repair attempt. It chains the original error (raise ... from error), so the message embeds the concrete parse failure that could not be repaired in time.","triggerScenarios":"The final budgeted call returns invalid JSON (truncated, prose-wrapped, schema-mismatched after slicing) and budget.remaining is already 0 — commonly because earlier stages (draft + review + prior repairs) consumed the 4-call cap. Note it fires on the parse failure path only; a healthy parse never reaches it even at remaining=0.","commonSituations":"Weak JSON prompting causing a repair loop that exhausts CallBudget before producing valid JSON; max_tokens truncation chopping the closing brace; high temperature producing free-form answers; budget shared across multiple agents so a repair-starved stage hits this on its first failure.","solutions":["Read the chained error (raise ... from error) to see the exact parse failure — fix that root cause (schema in prompt, JSON-only instruction, larger max_tokens) rather than just adding calls.","Raise or split budgets: CallBudget(maximum=6), or one budget per agent stage.","Capture the raw failing response in the except block (log raw_response) so you can see what the model actually returned.","For production, prefer the provider's structured-output/JSON mode over prompt-and-repair loops."],"exampleFix":"# before\nexcept ValueError as error:\n    if budget.remaining <= 0:\n        raise RuntimeError(f\"JSON 校验失败且没有剩余调用次数：{error}\") from error\n\n# after\nexcept ValueError as error:\n    print(f\"parse failed; raw response:\\n{raw_response}\")\n    if budget.remaining <= 0:\n        raise RuntimeError(f\"JSON 校验失败且没有剩余调用次数：{error}\") from error\n# plus: CallBudget(maximum=6) and schema-in-prompt as in run_structured","handlingStrategy":"retry","validationCode":"# preflight: budget must have headroom for one repair before parsing\nif budget.remaining < 2:\n    raise RuntimeError(\"insufficient budget for parse + repair\")","typeGuard":"null","tryCatchPattern":"try:\n    return parse_model_response(raw, model_type)\nexcept ValueError as error:\n    log_raw_response(raw)  # capture evidence\n    if budget.remaining <= 0:\n        raise RuntimeError(\"exhausted; re-run with larger budget\") from error\n    return parse_model_response(budget.run(agent, repair_prompt(raw, error)), model_type)","preventionTips":["Reserve at least one budgeted call for repair per structured request.","Log raw_response on parse failure to diagnose schema vs truncation.","Fix root causes (schema prompt, JSON mode, max_tokens) instead of enlarging the repair loop.","Chain the original error (raise ... from) so the real parse failure stays visible."],"tags":["json","llm","budget","runtimeerror","repair-loop"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}