Skip to content

fix(docx): preserve nested list hierarchy across empty spacer paragraphs - #4951

Open
taran-dev4u wants to merge 5 commits into
docling-project:mainfrom
taran-dev4u:fix/docx-nested-list-empty-paragraphs
Open

taran-dev4u wants to merge 5 commits into
docling-project:mainfrom
taran-dev4u:fix/docx-nested-list-empty-paragraphs

Conversation

@taran-dev4u

@taran-dev4u taran-dev4u commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

In a DOCX file, an empty spacer paragraph between list items was resetting \parents\ and numbering state, causing subsequent indented items to be added to the root list group as flat siblings when the group was reused.

  • Preserve the active list hierarchy (\parents, \level_at_new_list, \level_start_ilevel,
    ested_lists, and numbering levels via a _CachedListState\ dataclass) when caching across empty spacer paragraphs so resumption restores previous levels.
  • Restore the cached hierarchy in _manage_list_structure\ when resuming a list after spacer paragraphs, ensuring indented items nest under their parent list item.

Issue resolved by this Pull Request:
Resolves #4947

Checklist:

  • Documentation has been updated, if necessary. (not needed)
  • Examples have been added, if necessary. (not needed)
  • Tests have been added, if necessary.

Review evidence:

Document structure Before After
Two-level list with empty paragraphs Sub-items flattened to root \ListGroup\ (depth 2) Sub-items nested in child \ListGroup\ under parent \ListItem\ (depth 4)
Empty paragraphs before resumed list Deleted from document tree Deleted from document tree (unchanged)
Multi-level lists without spacers Nested under parent item Nested under parent item (unchanged)

Tests: Added \ est_nested_list_items_keep_nesting_across_empty_paragraphs\ in \ ests/test_backend_msword_spacer.py. Fails on \main\ and passes with this fix.

Checks run:

  • \pytest tests/test_backend_msword_spacer.py: 4 passed
  • \pytest tests/test_backend_msword_lists.py: 195 passed
  • \pytest tests/test_backend_msword.py: 88 passed, 1 skipped (all passing)

  • uff check\ /
    uff format --check\ on modified files: clean

Written with AI assistance; I reviewed the change and ran the checks myself.

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @taran-dev4u, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 2 protections blocking · waiting on 👀 reviews

Protection Waiting on
🔴 Require two reviewer for test updates 👀 reviews
🟢 Enforce conventional commit —

🔴 Require two reviewer for test updates

Waiting for

  • #approved-reviews-by >= 2
This rule is failing.

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

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)(?:\(.+\))?(!)?:

@taran-dev4u
taran-dev4u force-pushed the fix/docx-nested-list-empty-paragraphs branch from edd355d to b80f3e9 Compare October 21, 2026 20:46
@taran-dev4u
taran-dev4u marked this pull request as ready for review October 21, 2026 20:46
@github-actions github-actions Bot added bug Something isn't working docx issue related to docx backend labels Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

The PR keeps nested DOCX list levels when empty spacer paragraphs separate list items. It saves the list state at a spacer and restores it when the same list resumes. It adds a Test 13 case to the docx_lists.docx source file and a regression test.

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

2 related pull request(s) in the same code
PR State Signal Assessment
#5076 open changes the same code related: This PR adds inline equations to headings. It changes the same function, _handle_text_elements, but it does not change list handling.
#4993 closed also references issue #4947 related: This PR also fixes #4947 with a similar save-and-restore of list state across empty paragraphs. It is closed and not merged, so it is not a duplicate.

Reference data changes

Change Files
structure 1
text 2
  • structure tests/data/docx/groundtruth/docx_lists.docx.json: list_item: 59 → 64; section_header: 13 → 14; text: 26 → 27
  • text tests/data/docx/groundtruth/docx_lists.docx.itxt: +29 / -19 lines
  • text tests/data/docx/groundtruth/docx_lists.docx.md: +8 / -0 lines

Assessment: expected. The PR adds a Test 13 section to docx_lists.docx. That section has one heading, one text item, and five list items, which matches the count changes of +1 section_header, +1 text, and +5 list_item. The Markdown change is only additions, so existing output is unchanged.

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

@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.05882% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/msword_backend.py 97.05% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

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

Hi @taran-dev4u, can you resolve the CI failure? Thanks!

@taran-dev4u

Copy link
Copy Markdown
Contributor Author

The CI re-run completed with all checks passing green. Thanks!

dolfim-ibm
dolfim-ibm previously approved these changes Oct 25, 2026

@dolfim-ibm dolfim-ibm 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

@dolfim-ibm

Copy link
Copy Markdown
Member

CI is green on b80f3e9. #4993 is closed as a duplicate of this PR. @Jojoussef worked on the same bug, and their test checks one more thing. Please work together on this PR: if you take code or tests from #4993, add a Co-authored-by trailer for @Jojoussef.

A few small points before merge:

Co-authored-by: Youssef Benatti <[email protected]>

Signed-off-by: Taran Mamidala <[email protected]>
@taran-dev4u

Copy link
Copy Markdown
Contributor Author

Thanks @dolfim-ibm! Updated with all four points:

  • Added the assertion checking that empty spacer \TextItem\s do not survive in document texts.
  • Guarded \self.level = max(0, self.level_at_new_list - 1)\ to prevent negative levels when \level_at_new_list\ is 0.
  • Grouped the list cache fields into a _CachedListState\ dataclass with a \clone()\ method.
  • Dropped clearing the list cache on Title and Heading.
  • Added the \Co-authored-by\ trailer for @Jojoussef to the commit.

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Oct 25, 2026
DanielNg0729
DanielNg0729 previously approved these changes Oct 26, 2026

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

@taran-dev4u we have recently dealt with quite a few PRs fixing issues with nested lists in the DOCX parser and we have tried to collect those edge cases through real docx files in our tests folder. This helps us visualize the edge cases and also understand how they should be serialized in some formats like Markdown, through the ground truth files. Could you please add a simple example with an empty spacer paragraph between list items? I think tests/data/docx/sources/docx_lists.docx could be a good place to add it (you can append it after a page break). The document nested-list-empty-paragraph.docx posted by @neerajsangal-suresteps in the original issue #4947 would be a great example.

@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Oct 27, 2026
@taran-dev4u

Copy link
Copy Markdown
Contributor Author

Thanks @ceberam! Added the nested list with empty spacer paragraphs to ests/data/docx/sources/docx_lists.docx (Test 13, following a page break) matching the example from #4947, regenerated the .md, .itxt, and .json ground truth files, and added a test asserting the parsed hierarchy in ests/test_backend_msword.py.

@github-actions github-actions Bot added the ai:ci-safe AI triage found no CI-safety concern (advisory) label Oct 27, 2026
@github-actions github-actions Bot removed the ai:ci-safe AI triage found no CI-safety concern (advisory) label Nov 1, 2026
@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) ai:possible-duplicate AI triage: another PR may fix the same problem labels Nov 1, 2026
@ceberam

ceberam commented Nov 6, 2026

Copy link
Copy Markdown
Member

Thanks @taran-dev4u ! One last question: aren't you testing the same thing in two different test files: test_nested_list_items_separated_by_empty_paragraphs_keep_hierarchy and test_nested_list_items_keep_nesting_across_empty_paragraphs? I would remove the second one, since testing through private classes is always more fragile.

@taran-dev4u

Copy link
Copy Markdown
Contributor Author

Good catch, thanks! Removed the duplicate test from \ est_backend_msword_spacer.py\ and kept the fixture-based check in \ est_backend_msword.py.

@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:possible-duplicate AI triage: another PR may fix the same problem labels Nov 7, 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 docx issue related to docx backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOCX: nested list items become flat when separated by empty paragraphs

4 participants