Repository navigation
fix(chunker): assemble the One chunk in reading order - #22940
1BugKiller wants to merge 1 commit into
Conversation
Python's one.py sorts its sections by (page, top, left) before joining them, and the general chunker applies the same sort through sortPDFUnits. The One chunker joined the JSON items in upstream order, so a PDF whose blocks arrive out of order — multi-column layouts, layout models that emit per-region results — produced a single chunk with scrambled text. Sort the items with the shared sortPDFUnits before joining, under the same condition the position merge already uses: a PDF candidate that is not a spreadsheet. Units without coordinates and units that declare PreserveOrder stay where they are, so non-PDF payloads are unaffected. Test covers three out-of-order page-1/page-2 blocks: the chunk now reads FIRST, SECOND, page two.
|
The Could someone add it? Everything else is ready: one commit on |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Fixes #22939
What was wrong
The One parser claims "one file forms a chunk which maintains original text order", but the Go port joined the parsed blocks in upstream order. A PDF whose blocks arrive out of order (multi-column layouts, layout models that emit per-region results) produced a single chunk with swapped text:
The General parser on the same input already emits the sorted order, so the two parsers disagreed on the same document. Python's
rag/app/one.pysorts its sections by(page, top, left)before joining.The change
Sort the items with the shared
sortPDFUnitsbefore joining, under the same condition the position merge already uses — a PDF candidate that is not a spreadsheet:sortPDFUnitsonly reorders contiguous runs of positioned units; units without coordinates and units that declare_preserve_orderare barriers that keep their upstream position. Non-PDF payloads and spreadsheet items therefore behave exactly as before.Tests
internal/ingestion/component/chunker/one_test.go:TestOneChunker_KeepsReadingOrderForOutOfOrderPDFItems— three blocks given out of order. Before:"SECOND …\nFIRST …\nBlock on page two.". After:"FIRST …\nSECOND …\nBlock on page two.".The existing One tests still pass, including
TestOneChunker_MultiItemMergesPDFPositions(position merge unchanged),TestOneChunker_JSONMultipleMerges(position-less items keep their order) andTestOneChunker_MultiItemSpreadsheetSkipsPDFPositions(spreadsheets are not sorted).Verification
TestChunkerGoldenParityfails on this base as well: it compares snapshots byte-for-byte and git checks them out with CRLF on Windows. Verified pre-existing onmain.gofmt,go vetand the repo'stools/hooks/check_files.pychecks (eof, trailing whitespace, mixed line endings, ASCII comments, case conflicts, merge conflicts) are clean.