Skip to content

fix(html): keep one caption for a figure with several images - #4745

Merged
PeterStaar-IBM merged 3 commits into
docling-project:mainfrom
ryanmeowy:fix/html-figure-caption-duplicate
Nov 17, 2026
Merged

PeterStaar-IBM merged 3 commits into
docling-project:mainfrom
ryanmeowy:fix/html-figure-caption-duplicate

Conversation

@ryanmeowy

@ryanmeowy ryanmeowy commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Issue resolved by this Pull Request:
Resolves #4744

Checklist:

  • Documentation has been updated, if necessary. (Not needed. The reader is internal.)
  • Examples have been added, if necessary. (Not needed.)
  • Tests have been added, if necessary. tests/test_backend_html.py covers a figure with several images in six shapes, and the alt fallback of the images that follow the first one.

Review evidence:

Behavior before. _emit_image() read the <figcaption> of the parent figure for every <img>, so a figure with two images produced the caption twice:

<figure><img src="a.png"/><img src="b.png"/><figcaption>Figure 1: two panels</figcaption></figure>
texts: ['before', 'Figure 1: two panels', 'Figure 1: two panels']

Behavior after. The first image that produces a picture claims the caption of the figure, and the figure records that its caption was claimed. A figure keeps exactly one caption, also when the first image is not rendered (a hidden image produces no picture) or when it sits inside the <figcaption>:

Figure Before After
<img/><img/><figcaption>cap ['cap', 'cap'] ['cap']
<img/><figcaption>cap ['cap'] ['cap']
first <img style="display:none"/>, second <img/> ['cap'] ['cap']
first <img aria-hidden="true"/>, second <img/> ['cap'] ['cap']
<figcaption><img/>cap</figcaption><img/> ['cap'] ['cap']
<p><img/><img/></p><figcaption>cap ['cap', 'cap'] ['cap']

Document tree: the caption stays a DocItemLabel.CAPTION text item and becomes the caption of the first picture of the figure; the other pictures have no caption. The images after the first fall back to their own alt text:

<figure><img src="a.png" alt="panel A"/><img src="b.png" alt="panel B"/><figcaption>Figure 1: two panels</figcaption></figure>
captions: ['Figure 1: two panels', 'panel B']

Export: one caption per figure instead of one per image.

Checks.

Check Command Result
New tests, red first pytest tests/test_backend_html.py -k "keeps_one_caption or falls_back_to_its_alt" on main at the parent commit 3 failed, 4 passed
New tests, after the fix same selection 7 passed
HTML backend family pytest tests/test_backend_html.py tests/test_backend_mhtml.py tests/test_backend_epub.py tests/test_backend_email.py tests/test_input_doc.py tests/test_backend_markdown.py 250 passed, 1 skipped
Format and lint ruff format --check and ruff check on the two changed files All checks passed
Type check ty check 609 diagnostics, same as main at this commit

No reference-data changes: a scan of the HTML, XHTML and MHTML sources under tests/ and docs/ finds no figure with more than one image, and the new tests build their own HTML.

Not run: conversion of real-world HTML corpora with multi-image figures.

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Oct 10, 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)(?:\(.+\))?(!)?:

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

github-actions Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

This pull request prevents duplicate <figcaption> text when an HTML <figure> contains multiple images. The first rendered picture claims the figure caption, and following images use their alt text.

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

1 related pull request(s) in the same code
PR State Signal Assessment
#4820 closed also references issue #4744 related: PR 4820 addressed duplicate figure captions, but it is closed and not merged.

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 d57ac3e99c. It read the code and did not run it.

The main risk is that figure caption tracking relies on element identities during single-pass traversal. Check that multi-image figures assign the figcaption to the first rendered picture and preserve individual alt text on subsequent pictures.

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 Oct 13, 2026
vagenas
vagenas previously approved these changes Oct 14, 2026

@chrikrah chrikrah 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.

@ryanmeowy this is one line short of mergeable. The backend creates _captioned_figure_ids in __init__ and fills it in convert(). A second convert() on the same backend then finds every figure claimed, and it drops all <figcaption> text:

# two <figure>s: imgs alt="first"/"second" + figcaption, then one img + figcaption
b = HTMLDocumentBackend(in_doc=d, path_or_stream=s)
for run in (1, 2):
    doc = b.convert()
    caps = [p.caption_text(doc) for p in doc.pictures]
    print(f"convert #{run}: {len(doc.pictures)} pictures, captions {caps}")

convert #1: 3 pictures, captions ['Figure 1. Two views', 'second', 'Figure 2. One view']
convert #2: 3 pictures, captions ['first', 'second', '']

On main at dd477a9 both runs give the same captions, duplicate included, so this is new with the PR.

blocking: main handles a second convert() and this PR breaks it. DocumentConverter builds a fresh backend per document, so only direct reuse hits this, the same reuse dolfim-ibm approved fixing for CSV in #4754. convert() already resets its other per-call state at html_backend.py:980 with self._render_visibility_cache.clear(). Add self._captioned_figure_ids.clear() on the next line, and run 2 matches run 1.

The fix itself checks out:

$ pytest tests/test_backend_html.py -q
106 passed, 1 skipped      # at d57ac3e
99 passed, 1 skipped       # at a8c01dc

Restoring html_backend.py from a8c01dc with your tests kept fails 3 of the 7 new ones, the two test_figure_keeps_one_caption cases and test_figure_later_image_falls_back_to_its_alt_text. A test that converts twice would pin the reset, and with it I'd approve.

@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 Oct 15, 2026
@ryanmeowy

Copy link
Copy Markdown
Contributor Author

@chrikrah thank you, this is a real regression and I can reproduce it. On the second convert() the claimed figure identities are still in the set, so every <figcaption> falls back to the alt text of its picture.

I added self._captioned_figure_ids.clear() in convert(), next to self._render_visibility_cache.clear(), and a test that converts twice and compares the captions of both runs. Without the reset the new test fails with ['first', 'second', 'third'], and with it both runs give ['one', 'second', 'two']. The file reports 107 passed, 1 skipped. I also checked origin/main at 6fadbae3, where both runs are equal, so the regression comes from this pull request. The fix is pushed as 4a9bb171.

@chrikrah chrikrah 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.

@ryanmeowy 4a9bb17 clears my blocking finding: self._captioned_figure_ids.clear() now sits beside the visibility-cache reset in convert(), and your new test fails without it. I would merge this, including on top of today's main.

$ PYTHONPATH=$PWD python -m pytest tests/test_backend_html.py -q     # git archive 4a9bb171, docling venv
107 passed, 1 skipped in 9.32s          # d57ac3e9 was 106 passed, 1 skipped; base a8c01dc was 99 passed, 1 skipped

$ sed -i '/_captioned_figure_ids.clear()/d' docling/backend/html_backend.py    # your test kept
$ PYTHONPATH=$PWD python -m pytest tests/test_backend_html.py -q -k figure_caption_claim
FAILED tests/test_backend_html.py::test_figure_caption_claim_is_reset_between_conversions
1 failed, 107 deselected in 2.67s

# diff a8c01dc..4a9bb17 applied to main d01c9c09 (81 commits later, incl. #4468, #4670, #4675, #4713)
$ PYTHONPATH=$PWD python -m pytest tests/test_backend_html.py -q
115 passed, 1 skipped in 9.51s

non-blocking: the reset follows the per-call _render_visibility_cache pattern from 1c74a9b (#2969), which owns the line above it. Blame on the claim path itself leads to a069b11 (#1960) and the no-image fallback in 70eb711 (#4050), and the reset leaves both alone.

@vagenas the push to 4a9bb17 dismissed your approval from the 14th. The only new code is that one reset line, so could you approve it again at this head?

@ryanmeowy
ryanmeowy requested a review from vagenas November 6, 2026 14:58
A figure with more than one image emitted its <figcaption> once per image,
because every image read the caption of its parent figure. Only the first
image of the figure now claims the caption.

Signed-off-by: Ryan Yang <ryanxys@gmail.com>
Reading the <figcaption> on the first <img> in document order dropped the
caption when that image produced no picture, and images inside a <p>
reached the manual caption fallback. The figure now records that its
caption was claimed, by the first image that produces a picture, and the
fallback skips a claimed figure.

Signed-off-by: Ryan Yang <ryanxys@gmail.com>
A backend instance can convert more than one document. The claimed figure
identities are per-run state, so convert() clears them with the rest of the
context. Without the reset, a second conversion falls back to the alt text
for every figure caption.

Signed-off-by: Ryan Yang <ryanxys@gmail.com>
@ryanmeowy
ryanmeowy force-pushed the fix/html-figure-caption-duplicate branch from 4a9bb17 to 3c3ce71 Compare November 7, 2026 16:04
@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 7, 2026

@maxmnemonic maxmnemonic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@PeterStaar-IBM
PeterStaar-IBM merged commit 99f7e92 into docling-project:main Nov 17, 2026
34 checks passed
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 html issue related to html backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML: a figure with several images repeats its caption

5 participants