Skip to content

fix(msword): preserve nested list structure across empty paragraphs - #4993

Closed
Jojoussef wants to merge 1 commit into
docling-project:mainfrom
Jojoussef:fix/4947-docx-nested-list-empty-paragraph
Closed

Jojoussef wants to merge 1 commit into
docling-project:mainfrom
Jojoussef:fix/4947-docx-nested-list-empty-paragraph

Conversation

@Jojoussef

Copy link
Copy Markdown

Overview

In DOCX documents, empty spacing paragraphs placed between list items cleared list hierarchy state in MsWordDocumentBackend._handle_text_elements. When the next sub-item (w:ilvl > 0) was encountered, _prev_numid() and _prev_indent() returned None, causing _manage_list_structure to add sub-items as flat siblings at level 0 instead of nesting them under their parent ListItem.

Fixes #4947.

Implementation details

  • In docling/backend/msword_backend.py, added list state snapshotting (_save_list_state, _restore_saved_list_state, and _discard_saved_list_state).
  • When an empty spacing paragraph occurs while a list is active, MsWordDocumentBackend preserves the active list hierarchy (parents, level_at_new_list, level_start_ilevel, nested_lists, and indentation history).
  • If the list continues with the same numbering ID or deeper left indentation, the hierarchy and previous level state are restored, allowing _manage_list_structure to nest sub-items under their preceding parent item.
  • Empty spacing paragraphs between resumed list items are tracked in _pending_list_empty_paragraphs and transferred to _deferred_empty_paragraphs, ensuring they are removed at the end of conversion.
  • If non-list content (headings, titles, code blocks, or body text) follows, the snapshot is discarded and the empty paragraph remains in body content.

Testing

  • Added regression test test_nested_list_items_separated_by_empty_paragraphs_preserve_nesting in tests/test_backend_msword.py.
  • Verified that items with ilvl=1 separated by empty paragraphs nest under their ilvl=0 parent in the document tree.
  • Verified that deferred empty paragraphs are removed from the document tree.
  • Verified that Markdown export retains four-space list indentation for sub-items.

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

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

@mergify

mergify Bot commented Oct 23, 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) ai:possible-duplicate AI triage: another PR may fix the same problem bug Something isn't working docx issue related to docx backend labels Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

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

This PR fixes nested list structure flattening in DOCX files when empty spacing paragraphs appear between list items. It adds a snapshot/restore mechanism (_save_list_state, _restore_saved_list_state, _discard_saved_list_state) to preserve list hierarchy across empty paragraphs, and a regression test that verifies sub-items remain nested.

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

Possible duplicates

PR State Signal Assessment
#4951 open also references issue #4947; changes the same code duplicate: Both PRs fix the same bug (issue #4947) in msword_backend.py by snapshotting and restoring list hierarchy state across empty spacing paragraphs. Only one can be merged; they differ in implementation approach (this PR uses a single dict snapshot triggered in _handle_text_elements, while PR #4951 uses flat cached fields restored inside _manage_list_structure).

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

In DOCX files, empty paragraphs between list items cleared list hierarchy
state in MsWordDocumentBackend._handle_text_elements. When the subsequent
sub-item arrived, _prev_numid and _prev_indent returned None. This caused
_manage_list_structure to add sub-items as flat siblings in the root list
group instead of nesting them under the preceding ListItem.

Snapshot active list hierarchy state before processing empty spacing paragraphs
and restore it when the list resumes with matching numId or indentation.
Deferred empty paragraphs between list items are tracked and deleted at the
end of conversion.

Fixes docling-project#4947

Signed-off-by: Youssef Benatti <[email protected]>
@Jojoussef
Jojoussef force-pushed the fix/4947-docx-nested-list-empty-paragraph branch from beae89b to a27ddbe Compare October 23, 2026 14:17
@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 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 Oct 23, 2026
@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 38.18182% with 34 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
docling/backend/msword_backend.py 38.18% 30 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@dolfim-ibm

Copy link
Copy Markdown
Member

Closing this as a duplicate of #4951, which fixes the same bug (#4947) and was opened two days earlier. Both patches change the same lines in _handle_text_elements, so only one can land, and #4951 fits the existing list-cache mechanism more closely.

Two points from this version are useful input:

Thanks for the clear repro. The best outcome is one fix that has the strengths of both. Please join the review on #4951, and if your test case covers something it misses, suggest it there. @taran-dev4u can add it with a Co-authored-by trailer.

@dolfim-ibm dolfim-ibm added the duplicate This issue or pull request already exists label Oct 25, 2026
@dolfim-ibm dolfim-ibm closed this Oct 25, 2026
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) ai:possible-duplicate AI triage: another PR may fix the same problem bug Something isn't working docx issue related to docx backend duplicate This issue or pull request already exists

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants