{"record":{"id":"6b97d0deb6caed90","repo":"HKUDS/DeepTutor","slug":"figure-failed-validation-after-repair-residual-e","errorCode":null,"errorMessage":"figure failed validation after repair: {residual_error}","messagePattern":"figure failed validation after repair: (.+?)","errorType":"exception","errorClass":"GenerationFailure","httpStatus":null,"severity":"error","filePath":"deeptutor/book/blocks/figure.py","lineNumber":113,"sourceCode":"                    changed=False,\n                    review_notes=\"Passed local validation.\",\n                )\n            else:\n                review = await pipeline.run_repair(\n                    user_input=user_input,\n                    analysis=analysis,\n                    code=code,\n                    error=validation_error,\n                )\n        except Exception as exc:\n            logger.warning(f\"FigureGenerator failed: {exc}\", exc_info=True)\n            raise GenerationFailure(f\"figure generation failed: {exc}\") from exc\n\n        final_code = review.optimized_code or code\n        render_type = analysis.render_type\n        final_ok, residual_error = validate_visualization(final_code, render_type)\n        if not final_ok:\n            raise GenerationFailure(f\"figure failed validation after repair: {residual_error}\")\n        lang_tag = {\n            \"svg\": \"svg\",\n            \"mermaid\": \"mermaid\",\n            \"chartjs\": \"javascript\",\n        }.get(render_type, \"svg\")\n\n        return (\n            {\n                \"render_type\": render_type,\n                \"code\": {\"language\": lang_tag, \"content\": final_code},\n                \"description\": analysis.description,\n                \"chart_type\": analysis.chart_type,\n            },\n            [],\n            {\n                \"review_changed\": review.changed,\n                \"review_notes\": review.review_notes,\n            },","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/HKUDS/DeepTutor/blob/3e82f130422a813cdd73c10b21a44e9325f5821a/deeptutor/book/blocks/figure.py#L95-L131","documentation":"After the FigureGenerator produces (possibly repaired) code, the block re-validates it with validate_visualization(final_code, render_type). If validation still fails after the repair pass, this GenerationFailure is raised with the residual validation error — meaning the generated SVG/Mermaid/Chart.js code is structurally invalid.","triggerScenarios":"Calling figure _generate when the final optimized/repaired code still fails validate_visualization for its render_type (svg, mermaid, or chartjs) — e.g. malformed SVG XML, invalid Mermaid syntax, or Chart.js config with a JS syntax error.","commonSituations":"LLM produces syntactically broken Mermaid/SVG that the single repair pass can't fix; render_type misdetected (chartjs code validated as svg); stricter validator rules after a dependency upgrade; long/complex figures exceeding model capability.","solutions":["Read the residual_error text — it pinpoints the syntax problem (e.g. Mermaid node id, unbalanced XML tag)","Retry generation, optionally with a stronger model or a simpler figure prompt","Verify render_type detection matches the actual code language so the right validator runs","If persistent, manually supply or edit the figure code instead of relying on generation"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":"def looks_like_render_type(code: str) -> str:\n    c = code.strip()\n    if c.startswith('<svg'):\n        return 'svg'\n    if 'graph ' in c or 'flowchart' in c:\n        return 'mermaid'\n    return 'chartjs'","tryCatchPattern":"attempts = 2\nfor i in range(attempts):\n    try:\n        return await figure_block.generate(ctx)\n    except GenerationFailure as exc:\n        if 'failed validation after repair' in str(exc) and i < attempts - 1:\n            ctx = simplify_request(ctx)  # smaller/simpler figure\n            continue\n        raise","preventionTips":["Keep figure prompts simple enough for one-shot valid SVG/Mermaid","Cross-check detected render_type against actual code language","On repeated validation failures fall back to a hand-authored figure"],"tags":["figure","validation","mermaid","svg","repair"],"backgroundTag":"generated-code-validation-failed","analyzedSha":"3e82f130422a813cdd73c10b21a44e9325f5821a","analyzedAt":"2026-08-27T06:57:25.364Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}