Skip to content

Keep figures in the chunk they came from - #1290

Merged
Paul Lizer (paullizer) merged 1 commit into
Developmentfrom
paullizer-figure-chunk-association
Aug 19, 2026
Merged

Keep figures in the chunk they came from#1290
Paul Lizer (paullizer) merged 1 commit into
Developmentfrom
paullizer-figure-chunk-association

Conversation

@paullizer

Copy link
Copy Markdown
Contributor

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

Source Resolution
PDF via Content Understanding Already correct. Figures are attributed to their page by span. Unchanged, now regression-tested.
PDF via Document Intelligence Layout Already correct. Tables and figures are inlined into the page markdown by the service.
Equations Already correct. Returned inline in the page markdown by both engines, so they inherit the right page.
PPTX The slide that references the image, mapped onto the chunk covering that slide so grouped slides work.
DOCX Position in reading order from word/document.xml, mapped proportionally onto the word-count chunks.
Legacy .doc / .ppt No recoverable position, so the image anchors to the final chunk rather than inventing a page beyond the document.

Why 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.emf in Word but ../media/image1.png in PowerPoint, and the shared path logic produced word/media/media/image1.emf for 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 a media parent 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:object OLE 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:

total body words: 1200          document produces 3 chunks
image1  offset=120  (10%) -> chunk 1
image2  offset=600  (50%) -> chunk 2
image3  offset=1080 (90%) -> chunk 3
OLD behavior would have produced chunks 4, 5, 6

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

  • DOCX placement is approximate by design. Word has no pages until rendered, so this maps onto word-count chunks; it will be close, not paragraph-exact. Stated plainly in the feature doc rather than implying precision we do not have.
  • Existing documents are unaffected until reprocessed. Anything already ingested keeps its trailing figure chunks until Change Extraction or a re-upload. Called out in the release notes.
  • No Cosmos or search index migration; only chunk content and page assignment change.

Version bumped to 0.250.228.

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

1 participant