Skip to content

fix(markdown): preserve LaTeX formulas in VLM responses - #5225

Open
matt8s wants to merge 9 commits into
docling-project:mainfrom
matt8s:fix/markdown-latex-math
Open

matt8s wants to merge 9 commits into
docling-project:mainfrom
matt8s:fix/markdown-latex-math

Conversation

@matt8s

@matt8s matt8s commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Markdown VLM responses currently enter the ordinary Markdown backend. LaTeX escapes and underscores can become prose formatting, and an internal HTML conversion can flatten formulas.

Add MarkdownBackendOptions.enable_latex_math, defaulting to False. Enable it for Markdown responses in VlmPipeline. Reuse the shared math extension and retain the backend's GFM dialect.

The backend creates typed formulas with literal LaTeX bodies. Display formulas keep their scope through emphasis, links, headings, lists and rich table cells. Document-local references carry formulas and hard breaks through the internal HTML conversion while preserving HTML formatting, real links and pictures. Code spans remain literal.

Resolves #5224.

Checklist:

  • Documentation: backend option, supported delimiters, GFM pipe escaping and VLM activation.
  • Examples: an opt-in conversion example in the supported-formats guide.
  • Tests: constructed Markdown and HTML inputs in the existing test modules.

Review evidence:

Tests check literal formula bodies, inline/display scope, tree ownership, cell reading order, hard breaks, formatting, links and exports. Controls cover ordinary Markdown, currency, code, optional imports, cached parser modes and concurrent conversions. The VLM test uses a synthetic response and does not load a model. No reference data changed.

The existing GFM pipe-escape layer still applies: write \\| in a cell to supply LaTeX \|. Recognition inside opaque raw HTML blocks and the existing delimiter policy are unchanged.

At 916a9fc, the isolated branch passed 550 tests, with one Chromium-dependent test skipped:

python -m pytest -n 4 --dist loadfile -q \
  tests/test_backend_markdown.py tests/test_markdown_math.py \
  tests/test_lightonocr3_vlm.py tests/test_chandra_vlm.py \
  tests/test_mineru2_pro.py tests/test_vlm_inline_math_exports.py \
  tests/test_backend_optional_dependencies.py \
  tests/test_vlm_pipeline_optional_dependencies.py \
  tests/test_backend_html.py tests/test_backend_markdown_images.py \
  tests/test_dots_parser.py tests/test_nemotron_parse_v2.py \
  tests/test_nemotron_parse_v3_5.py tests/test_nemotron_formula_exports.py

make validate and the applicable hooks on the actual committed file list passed. The documentation build passed. Validation used Python 3.12.14, docling-core 2.106.0 and locked Marko 2.2.4.

The full suite was not run on this isolated branch. Broader local testing found a legacy PowerPoint reference mismatch that also occurs on unchanged upstream. The default 1000-line check has pre-existing violations; the configured changed-file CI hooks pass.

Default-mode follow-up

Commit df25ae5 limits hard-break placeholders to documents with typed formulas. Six new cases compare math-free text, orig, and exact Markdown export with baseline behavior, with the option both enabled and disabled. The affected five-file suite passed 202 tests, and make validate passed. See the review-thread reply for the command and before/after comparison.

Upstream compatibility follow-up

Merged upstream 896f2fd additively at a79e92b and retained both sets of Markdown tests. The existing math AST helpers moved to markdown_math.py in 821390a; the combined backend stays below the 1,500-line limit, with lazy loading through the existing optional-dependency guard. The helper move passed 166 affected tests, including optional-dependency checks. After merging upstream, the Markdown backend, math utility and VLM math-export suites passed 183 tests. make validate passed for both changes.

Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @matt8s, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working markdown issue related to markdown backend vlm-pipeline labels Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit 5956cc9bb4. The analysis read the diff only. It did not run code from this pull request.

This PR adds the option MarkdownBackendOptions.enable_latex_math, which is off by default. When it is on, the Markdown backend keeps LaTeX math as FormulaItem objects with their literal bodies. The formulas also survive the internal HTML round trip. VlmPipeline turns the option on for Markdown responses.

CI safety: no concern found. A maintainer must still approve CI.

1 related pull request(s) in the same code
PR State Signal Assessment
#4848 open changes the same code related: Both PRs change HTML block handling in md_backend.py and markdown_html.py, but they solve different problems. PR 4848 removes the internal HTML round trip and deletes markdown_html.py. This PR adds formula support to that module. The two PRs will conflict, but only one fix does not replace the other.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI first review (advisory): no blocker found

This review covers the full diff at 916a9fc84a. It read the code and did not run it.

The change reaches beyond the opt-in flag. _MarkdownTextSerializer now rewrites every newline in non-code text during the HTML round trip, even when enable_latex_math is False. The reviewer must check whether the default Markdown behavior changes for documents that contain an HTML block.

A maintainer makes the merge decision. This review never approves the pull request.

Comment thread docling/backend/utils/markdown_html.py Resolved
@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 7, 2026
@codecov

codecov Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 8, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI first review (advisory): no blocker found

This review covers the changes since 916a9fc84a at df25ae57b5. It read the code and did not run it.

The incremental change disables hard-break replacement during HTML round trips when the document contains no formulas. I checked docling/backend/utils/markdown_html.py and the new tests in tests/test_backend_markdown.py for regressions in math-free Markdown conversions.

Earlier findings

  • fixed (question): Hard-break handling in the HTML round trip is not gated by enable_latex_math

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 8, 2026
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 8, 2026
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 8, 2026
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Nov 29, 2026
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
Signed-off-by: Matthew Emerson Spotnitz, PhD <mespotnitz@gmail.com>
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) and removed ai:ci-safe AI triage found no CI-safety concern (advisory) labels Dec 3, 2026

This branch has not been deployed

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

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working markdown issue related to markdown backend vlm-pipeline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown VLM responses lose LaTeX bodies and formula scope

1 participant