DEV Community

KolmoPDF
KolmoPDF

Posted on

A merged PDF table can cite the wrong page for a correct answer

A stitched PDF table can return the right value with the wrong citation. The row came from page 8, but the pipeline kept only the table's starting page, 7. A reader opens page 7, cannot find the value, and has to search the original file manually.

This failure is easy to miss in a RAG test that checks the answer text alone. It also survives a table merge that correctly preserves every column and row. Page provenance must travel with the row through the merge and the subsequent chunking step.

Start with a two-page fixture

Imagine a report with a table called "Latency by configuration". Page 7 has the header and row A. Page 8 repeats the header and continues with row B. These values are illustrative:

Configuration Latency (ms) Source page
A 12 7
B 19 8

The last column is metadata in the example; it does not need to appear in the source table. After extraction, the logical table can be one object. Each row still needs its own source reference. If a row spans pages, it needs more than one reference.

Distinguish the physical PDF page index from the printed page label. A PDF viewer may call the first page index 0, while the document prints a Roman numeral or starts numbering after a cover. Store both when available, and choose the displayed citation convention explicitly. A page label extracted from the footer should not replace the physical location used to open the file.

Keep provenance on each row

Here is a minimal model and a test using Python's standard library. The page_index values are zero-based; the printed labels are strings because a document can use Roman numerals.

from dataclasses import dataclass

@dataclass(frozen=True)
class Source:
    page_index: int
    page_label: str

@dataclass(frozen=True)
class Row:
    cells: tuple[str, ...]
    sources: tuple[Source, ...]

page_7 = Source(page_index=6, page_label="7")
page_8 = Source(page_index=7, page_label="8")

rows = [
    Row(("A", "12"), (page_7,)),
    Row(("B", "19"), (page_8,)),
]

def chunk_rows(rows, size):
    if size < 1:
        raise ValueError("size must be positive")
    return [rows[i:i + size] for i in range(0, len(rows), size)]

chunks = chunk_rows(rows, 1)
assert chunks[1][0].cells == ("B", "19")
assert chunks[1][0].sources == (page_8,)
Enter fullscreen mode Exit fullscreen mode

This code starts after extraction. It does not identify table continuations or infer page labels. In production, add a document identifier and a stable row identifier; bounding boxes can help a viewer locate the exact row. Capture those references before combining pages. Copying the first page's metadata onto the merged table loses the evidence needed to cite B.

For a row broken across pages, keep both physical locations in sources. A citation can display both page labels, while the viewer uses the corresponding page indexes. If the extractor cannot establish where a cell came from, retain an explicit unknown source and send that result for review rather than assigning the first page by default.

Carry context into retrieval

A row alone, such as B | 19, lacks a measure and unit. A retrieval chunk should include the caption, column headers, applicable notes and the row's source reference. Repeating a header in a chunk provides context; it does not make the repeated header an original occurrence on the row's page. Keep generated context distinguishable from extracted content if the UI highlights source regions.

Notes need their own provenance too. If a qualifying note appears on page 9, an answer using that note may need to cite both page 8 for the value and page 9 for the qualification. Attach the note only to the cells or rows it actually qualifies. A page range for the whole table is useful for navigation, but it cannot by itself prove which page contains a particular value.

Test the answer and the location separately

For the fixture above, ask: "What is the latency for configuration B?" Check that the retrieved row contains 19, that the selected source is physical index 7, and that the displayed label is 8. An answer of 19 ms with page 7 should fail the citation test even though the value is correct.

Add a second test where a single row continues onto the next page. Check that the merge preserves both references. Then add a cover page to a copy of the fixture and update the physical indexes while leaving printed labels unchanged. This catches systems that treat page labels as PDF indexes.

These tests verify the structure passed downstream. For a full pipeline test, compare the cited page and row against the rendered PDF as well: an internally consistent index can still point at the wrong place if the extraction step assigned the source incorrectly.

Related: PDF RAG citations break when chunks lose page boundaries.

Top comments (0)