{"record":{"id":"e548cb3fda655254","repo":"HKUDS/DeepTutor","slug":"generated-code-does-not-parse-syntax-error","errorCode":null,"errorMessage":"generated code does not parse: {syntax_error}","messagePattern":"generated code does not parse: (.+?)","errorType":"exception","errorClass":"GenerationFailure","httpStatus":null,"severity":"error","filePath":"deeptutor/book/blocks/code.py","lineNumber":104,"sourceCode":"            system_prompt=get_book_prompt(prompts, \"system\"),\n            max_tokens=900,\n            temperature=0.3,\n            language=ctx.language,\n        )\n\n        code = str(data.get(\"code\") or \"\").strip()\n        if not code:\n            raise GenerationFailure(\"LLM did not return any code.\")\n        if \"<think\" in code.lower() or \"</think\" in code.lower():\n            raise GenerationFailure(\"prompt leak detected in generated code.\")\n\n        code_language = str(data.get(\"language\") or language).strip() or language\n        syntax_error = _syntax_error(code, code_language)\n        if syntax_error:\n            # A truncated or malformed snippet is worse than none: the reader\n            # copies it, it fails, and nothing said it was never checked. Fail\n            # the block so the compiler's retry path gets a second attempt.\n            raise GenerationFailure(f\"generated code does not parse: {syntax_error}\")\n\n        metadata = data.get(\"_metadata\") if isinstance(data.get(\"_metadata\"), dict) else {}\n        return (\n            {\n                \"language\": code_language,\n                \"code\": code,\n                \"explanation\": str(data.get(\"explanation\") or \"\").strip(),\n                \"intent\": intent,\n            },\n            [],\n            {**metadata, \"syntax_checked\": _CHECKABLE.get(code_language.lower()) is not None},\n        )\n\n\n__all__ = [\"CodeGenerator\"]\n","sourceCodeStart":86,"sourceCodeEnd":120,"githubUrl":"https://github.com/HKUDS/DeepTutor/blob/3e82f130422a813cdd73c10b21a44e9325f5821a/deeptutor/book/blocks/code.py#L86-L120","documentation":"The generated snippet was syntax-checked with `_syntax_error(code, language)` and failed to parse, so the block refuses to publish broken/truncated code and raises GenerationFailure carrying the parser's message, letting the compiler's retry path try again.","triggerScenarios":"The LLM returns code with a syntax error in the declared language — truncation mid-statement, mismatched fences, wrong language tag (e.g. Python snippet labeled 'bash' so it's checked with the wrong parser), or genuine model mistakes.","commonSituations":"Long snippets cut off by max_tokens; language field defaulting incorrectly; models mixing syntax across languages; unavailable parser causing fallback strictness.","solutions":["Retry the block (the compiler's retry path exists precisely for this)","Increase max_tokens so snippets aren't truncated mid-statement","Verify the `language` value in the response matches the actual snippet","Use a stronger model or lower temperature for long code blocks"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"from deeptutor.book.blocks.code import _syntax_error\nif _syntax_error(snippet, language):\n    regenerate_or_reject(snippet)","typeGuard":"def parses_clean(code: str, language: str) -> bool:\n    return _syntax_error(code, language) is None","tryCatchPattern":"try:\n    result = await code_gen._generate(ctx)\nexcept GenerationFailure as e:\n    if \"does not parse\" in str(e):\n        result = await code_gen._generate(ctx)\n    else:\n        raise","preventionTips":["Set generous max_tokens for code blocks","Ensure the language label matches the snippet's real language","Keep the compiler's retry path enabled for code blocks"],"tags":["syntax-error","code-generation","validation","retry"],"backgroundTag":"generated-code-syntax-error","analyzedSha":"3e82f130422a813cdd73c10b21a44e9325f5821a","analyzedAt":"2026-08-27T06:57:25.364Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}