HKUDS/DeepTutor · error · GenerationFailure

LLM did not return any code.

Error message

LLM did not return any code.

What it means

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.

Source

Thrown at deeptutor/book/blocks/code.py:94

        none_label = "(无)" if ctx.language == "zh" else "(none)"
        user_prompt = get_book_prompt(prompts, "user_template").format(
            chapter_title=chapter_title,
            chapter_summary=chapter_summary or none_label,
            objectives_inline="; ".join(objectives) or none_label,
            intent=intent,
            language=language,
        )
        data = await llm_json(
            user_prompt=user_prompt,
            system_prompt=get_book_prompt(prompts, "system"),
            max_tokens=900,
            temperature=0.3,
            language=ctx.language,
        )

        code = str(data.get("code") or "").strip()
        if not code:
            raise GenerationFailure("LLM did not return any code.")
        if "<think" in code.lower() or "</think" in code.lower():
            raise GenerationFailure("prompt leak detected in generated code.")

        code_language = str(data.get("language") or language).strip() or language
        syntax_error = _syntax_error(code, code_language)
        if syntax_error:
            # A truncated or malformed snippet is worse than none: the reader
            # copies it, it fails, and nothing said it was never checked. Fail
            # the block so the compiler's retry path gets a second attempt.
            raise GenerationFailure(f"generated code does not parse: {syntax_error}")

        metadata = data.get("_metadata") if isinstance(data.get("_metadata"), dict) else {}
        return (
            {
                "language": code_language,
                "code": code,
                "explanation": str(data.get("explanation") or "").strip(),
                "intent": intent,

View on GitHub (pinned to 3e82f13042)

Solutions

  1. Retry the generation (single empty response is usually transient)
  2. Increase max_tokens / use a stronger model for the book pipeline
  3. Inspect the raw LLM response logged before the failure to see what shape came back
  4. Tighten the block prompt to demand JSON with a non-empty `code` field
Defensive patterns

Strategy: retry

Try / catch

try:
    result = await code_gen._generate(ctx)
except GenerationFailure as e:
    if "did not return any code" in str(e):
        result = await code_gen._generate(ctx)  # retry once
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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'.

Related errors


AI-assisted analysis of HKUDS/DeepTutor@3e82f13042 (2026-08-27). Data as JSON: /api/errors/bd2042331904be61. Report an issue: GitHub.