Keep figures in the chunk they came from - #1290
Merged
Paul Lizer (paullizer) merged 1 commit intoAug 19, 2026
Merged
Conversation
Refs #1277 Images extracted from Word and PowerPoint files were appended as extra chunks at the end of the document, with page numbers continuing past the real content. A figure on page 5 of a 15-page document became chunk 16, so a search hit on the figure lost its surrounding text and a citation pointed at a page that does not exist. Merging rather than appending also removes a latent indexing hazard. Chunk ids are derived from the page number, so emitting a second chunk that reuses page 5 would have overwritten the original in the search index. Image content is therefore folded into the existing chunk's content, which is both what is wanted and the only safe option. Placement is resolved per source. PowerPoint images follow the slide that references them, mapped onto the chunk covering that slide so grouped slides work. Word images are located by walking document.xml in reading order and counting the words that precede each image reference, then mapped proportionally onto the word-count chunks; proportional rather than absolute because the extractor's word count does not match the raw body exactly, and an absolute offset would drift and cluster every image at the front. Legacy .doc and .ppt images carry no recoverable position, so they anchor to the final chunk instead of inventing a page beyond the document. Merged chunks are held under a size budget derived from the chunk size cap; anything that does not fit spills to a trailing chunk rather than producing an oversized chunk. PDFs were already correct. Content Understanding attributes each figure to its page by span and Document Intelligence Layout inlines tables and figures into the page markdown, so equations and tables already stayed on their page. That behavior is unchanged and now has a regression test. Verifying the Word offsets against a real document first caught a bug in the groundwork: relationship targets are written as media/image1.emf in Word but ../media/image1.png in PowerPoint, and the shared normalizer produced word/media/media/image1.emf for the Word form, so nothing matched. Both now resolve through one target normalizer. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1277
Problem
Images extracted from Word and PowerPoint were appended as extra chunks at the end of the document, with page numbers continuing past the real content. A figure on page 5 of a 15-page document became chunk 16.
That breaks retrieval in two ways: a search hit on the figure loses the text it belongs to, and a citation points at a page that does not exist.
The constraint that shaped the fix
chunk_id = f"{document_id}_{page_number}".Two chunks sharing a page number therefore overwrite each other in the search index. Emitting a second chunk tagged "page 5" would have silently destroyed the original. Image content is instead folded into the existing chunk's content — which is both the desired behavior and the only safe one.
How placement is resolved
word/document.xml, mapped proportionally onto the word-count chunks..doc/.pptWhy proportional and not absolute for DOCX: Word has no fixed pages until rendered, and the extractor's word count does not match the raw document body exactly — it adds markdown structure and may include headers, footers, and table formatting. An absolute word offset would drift systematically and cluster every image toward the front. Relative position is self-correcting.
Merged chunks are held under a size budget derived from the chunk size cap; anything that does not fit spills to a trailing chunk rather than producing an oversized chunk.
A bug caught by verifying first
The DOCX offset groundwork was written before this PR and was unverified. Running it against a real document immediately exposed a defect: relationship targets are written as
media/image1.emfin Word but../media/image1.pngin PowerPoint, and the shared path logic producedword/media/media/image1.emffor the Word form — so nothing matched. PowerPoint worked only because its../prefix happened to satisfy the same logic. Both now resolve through one target normalizer that requires amediaparent segment, which also rejects unrelated targets such as hyperlinks.Investigating also confirmed a surprise worth recording: in the sample document the four diagrams genuinely are at the very top (paragraphs 0–2, before the "Introduction" heading), stored as inline
w:objectOLE shapes with no absolute positioning. An offset of 0 was correct, not a bug.Verification
Placement measured against a synthetic DOCX with images planted at known positions:
New test file with 9 tests covering position detection, DOCX placement, PPTX slide mapping including the grouped case, the positionless fallback, the size budget spill, page-number uniqueness, and that Content Understanding still keeps figures on their origin page. All previously passing suites still pass.
Reviewer notes
Version bumped to
0.250.228.