HKUDS/DeepTutor · error · GenerationFailure

generated code does not parse

Error message

generated code does not parse: {syntax_error}

What it means

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.

Solutions

  1. Retry the block (the compiler's retry path exists precisely for this)
  2. Increase max_tokens so snippets aren't truncated mid-statement
  3. Verify the `language` value in the response matches the actual snippet
  4. Use a stronger model or lower temperature for long code blocks
Defensive patterns

Strategy: retry

Validate before calling

from deeptutor.book.blocks.code import _syntax_error
if _syntax_error(snippet, language):
    regenerate_or_reject(snippet)

Type guard

def parses_clean(code: str, language: str) -> bool:
    return _syntax_error(code, language) is None

Try / catch

try:
    result = await code_gen._generate(ctx)
except GenerationFailure as e:
    if "does not parse" in str(e):
        result = await code_gen._generate(ctx)
    else:
        raise

Prevention

When it happens

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

Common situations: Long snippets cut off by max_tokens; language field defaulting incorrectly; models mixing syntax across languages; unavailable parser causing fallback strictness.

Related errors


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

Appendix: source

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

            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,
            },
            [],
            {**metadata, "syntax_checked": _CHECKABLE.get(code_language.lower()) is not None},
        )


__all__ = ["CodeGenerator"]

View on GitHub (pinned to 3e82f13042)