{"record":{"id":"bd2042331904be61","repo":"HKUDS/DeepTutor","slug":"llm-did-not-return-any-code","errorCode":null,"errorMessage":"LLM did not return any code.","messagePattern":"LLM did not return any code\\.","errorType":"exception","errorClass":"GenerationFailure","httpStatus":null,"severity":"error","filePath":"deeptutor/book/blocks/code.py","lineNumber":94,"sourceCode":"        none_label = \"(无)\" if ctx.language == \"zh\" else \"(none)\"\n        user_prompt = get_book_prompt(prompts, \"user_template\").format(\n            chapter_title=chapter_title,\n            chapter_summary=chapter_summary or none_label,\n            objectives_inline=\"; \".join(objectives) or none_label,\n            intent=intent,\n            language=language,\n        )\n        data = await llm_json(\n            user_prompt=user_prompt,\n            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,","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/HKUDS/DeepTutor/blob/3e82f130422a813cdd73c10b21a44e9325f5821a/deeptutor/book/blocks/code.py#L76-L112","documentation":"The code book block asked the LLM for a `{\"code\": ...}` payload and the returned `code` field was absent, null, or whitespace-only. The block refuses to emit an empty snippet and fails the generation.","triggerScenarios":"Calling the code block's `_generate` when the LLM response JSON has no usable `code` key — e.g. the model returned only prose, refused, or the JSON parse produced an unexpected shape that defaulted to empty.","commonSituations":"Weak/overloaded model endpoints returning explanatory text instead of code; truncation from low max_tokens; structured-output parsing quirks; prompts that let the model answer 'no code needed'.","solutions":["Retry the generation (single empty response is usually transient)","Increase max_tokens / use a stronger model for the book pipeline","Inspect the raw LLM response logged before the failure to see what shape came back","Tighten the block prompt to demand JSON with a non-empty `code` field"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    result = await code_gen._generate(ctx)\nexcept GenerationFailure as e:\n    if \"did not return any code\" in str(e):\n        result = await code_gen._generate(ctx)  # retry once\n    else:\n        raise","preventionTips":["Use a model/prompt that reliably emits JSON with a non-empty code field","Raise max_tokens to avoid empty/truncated responses","Retry once before surfacing to users"],"tags":["llm-output","code-generation","empty-response"],"backgroundTag":"llm-empty-response","analyzedSha":"3e82f130422a813cdd73c10b21a44e9325f5821a","analyzedAt":"2026-08-27T06:57:25.364Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}