Skip to content

Keep DOCX tables in the section where they appear - #11

Merged
sinnercode228 merged 1 commit into
mainfrom
fix/docx-table-order
Nov 1, 2026
Merged

sinnercode228 merged 1 commit into
mainfrom
fix/docx-table-order

Conversation

@sinnercode228

Copy link
Copy Markdown
Owner

What was wrong. load_docx (backend/src/docmind/ingestion/loaders.py) iterated document.paragraphs and only afterwards document.tables, so every table row was appended to the buffer of the section still open at the end. A pricing table under "Pricing" ended up in the text of the last section with that section's heading, and the "Pricing" section itself disappeared if it had no paragraphs. Retrieval then cited the wrong section.

The fix. Walk the body with Document.iter_inner_content() (python-docx >= 1.1, already our minimum) and handle paragraphs and tables in the same loop, so table rows go into the section they belong to. Paragraph handling is unchanged.

How it was tested. New test_docx_table_stays_in_its_section in backend/tests/test_ingestion.py builds a DOCX with "Pricing" + a table, then "Contacts" + a paragraph, and asserts two sections with the table row under "Pricing". It fails on main (the row lands in "Contacts" and "Pricing" is missing). Full backend suite (119 passed), ruff check, ruff format --check and mypy pass.

Fixes #10

load_docx read every paragraph first and only then every table, so all
table rows ended up in the last section's text under the last heading.
Walk the body with iter_inner_content() so paragraphs and tables are
handled in document order.

Fixes #10
@sinnercode228
sinnercode228 merged commit 4f78d69 into main Nov 1, 2026
@sinnercode228
sinnercode228 deleted the fix/docx-table-order branch November 1, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOCX tables are attached to the last section instead of where they appear

1 participant