Skip to content

paper_orchestrator strips opening triple-backtick fence but not the language tag #149

Open

Description

@ngo-software

backend/app/modules/paper/service.py:533-536:

content = resp.text.strip()
if content.startswith("```"):
    lines = content.split("\n")
    content = "\n".join(lines[1:-1]) if lines[-1].strip() == "```" else "\n".join(lines[1:])

For an LLM response of:

```latex
\section{Introduction}
...
```

lines[0] is ```latex and lines[1:-1] correctly skips it. Good.

But for:

```
\section{Introduction}
...

(no closing fence — the model ran out of tokens), the else branch joins lines[1:], which is fine.

The bug is the first code path when there's no closing fence: lines[-1].strip() == "```" is False, so we take "\n".join(lines[1:]), which discards line 0 — but we've already stripped the language tag. The actual text the model meant to emit (starting at \section{Introduction}) is preserved. So far so good.

The real issue is what happens inside the section content: if the model is overzealous and wraps individual algorithm blocks in nested triple-backticks (a documented behaviour for minimax with high temperature), this naive strip-leading-fence does nothing to the embedded fences, which then end up verbatim in the LaTeX source — and pdflatex chokes on them.

Repro of the failure mode:

\section{Method}
Our method works as follows:

\begin{algorithm}[H]
...
\end{algorithm}

The two ``` lines get written verbatim into sections/method.tex via write_paper_file(paper_id, f"sections/{section['id']}.tex", content) (line 538). pdflatex reports ! Undefined control sequence at the backtick line, the compile fails, and the user gets the simplified fpdf PDF fallback — which is fine for preview but breaks the "Stable release baseline: Paper generation path" claim in the README (line 364).

Suggested fix: a re.sub(r"```(?:[a-zA-Z]+)?\n?", "", content) pass that strips all fences, not just the outermost ones, before writing the section file.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions