DEV Community

KolmoPDF
KolmoPDF

Posted on

PDF table headers can swap datasets while every number survives

A Markdown table can have unique headers and still assign a result to the wrong dataset. Suppose a PDF groups two columns under Dataset A and Dataset B, both measured as accuracy. After conversion, the labels may be flattened into Dataset A / Accuracy and Dataset B / Accuracy. If the header order is swapped while the data cells stay in place, the table renders perfectly and every number survives. A retrieval system can then cite the correct page and give the wrong result.

This is a useful companion to the dataset-label check. Checking for missing or duplicate names catches one failure. Checking the relationship between each header and its cells catches another.

Make the fixture asymmetric

Use a small synthetic table with deliberately distinct values. The source layout is equivalent to:

Model Dataset A / Accuracy Dataset B / Accuracy
Cedar 91 88
Birch 73 96

Imagine a converter emits the same cells but reverses the two dataset labels. A Markdown parser will accept it. A test that compares only the set of labels and the set of numbers will accept it too: both sets are unchanged.

For a regression fixture, record the expected association for each cell, including the row identity and complete header path. Here is a minimal check using Python's standard library. The converted list stands in for rows produced by your converter; adapt the mapping step to its actual output.

expected = {
    ("Cedar", "Dataset A", "Accuracy"): "91",
    ("Cedar", "Dataset B", "Accuracy"): "88",
    ("Birch", "Dataset A", "Accuracy"): "73",
    ("Birch", "Dataset B", "Accuracy"): "96",
}

# Deliberately swapped header paths, with the original cell positions.
headers = [("Dataset B", "Accuracy"), ("Dataset A", "Accuracy")]
converted = [("Cedar", ["91", "88"]), ("Birch", ["73", "96"])]

observed = {
    (model, *header): value
    for model, values in converted
    for header, value in zip(headers, values, strict=True)
}
for cell, value in expected.items():
    if observed.get(cell) != value:
        print(f"MISMATCH {cell}: expected {value}, got {observed.get(cell)}")
Enter fullscreen mode Exit fullscreen mode

The example needs Python 3.10 or later for zip(..., strict=True). It reports four mismatches for this fixture. The strict zip catches rows with a different number of cells and headers; it cannot detect a wrong association on its own. The comparison against an independently prepared source expectation is what exposes the swap.

A production test should also detect unexpected extra rows or columns, duplicate row identifiers, and duplicate header paths before building a dictionary. Otherwise one duplicate can overwrite another. Store a stable row identifier from the fixture when row labels are not unique. Keep raw cell strings for audit, and normalize numbers only under an explicit rule for units, decimal separators, and missing values.

Preserve the header path

A multi-level heading is a path, not a single string. In this example, Accuracy appears twice, so indexing that word alone cannot distinguish the columns. Store the ordered path for each cell, for example ['Dataset A', 'Accuracy'], along with its row key. Flatten it for readable Markdown if needed, but retain the structured path in the intermediate representation.

Merged cells complicate the mapping. A group label may span several columns, and an empty cell under that label can mean a continuation rather than an absent dataset. Build the path from the table's actual span structure when the extractor provides it. If the structure is ambiguous, preserve the raw location and flag it for review instead of silently filling a guessed label across the row.

Coordinates can help a reviewer locate the source cell, but a bounding box is not proof that a header was assigned correctly. Keep the document version and page number with the cell and header locations. A changed PDF may shift coordinates even when its displayed values look familiar.

Test retrieval separately

After conversion passes the structural fixture, index it and ask two paired questions: what is Cedar's accuracy on Dataset A, and what is it on Dataset B? Check the retrieved evidence for both the value and the full header path. Then check the answer and its citation. This separates an extraction error from a retrieval miss or a generation error.

Also try a negative case: ask for a dataset absent from the table. A system should avoid treating the nearest available column as the requested one. The correct response depends on the rest of the document, so run that test against a controlled fixture whose complete contents are known.

Header-path tests cost more to prepare than a Markdown syntax check because someone must establish the expected mapping from the source. Keep a small set of reviewed examples across layouts, then run both kinds of check after converter changes. When a fixture fails, the specific row and header path point directly to the relationship that needs inspection.

Top comments (3)

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

This is the failure mode that worries me most in RAG pipelines: the table renders perfectly, every number is real, and the answer is still wrong. Your header-path point is the right fix at the table level — the same "silently wrong, not visibly broken" problem shows up one level earlier, at page ingestion. In benchmarking I found tools returning clean, empty output for scanned pages (no error, no warning) and mangled reading order on Persian/RTL documents — the kind of thing a spot check on one page never catches.

The check that has saved me: per-page provenance. For every page, record whether the text came from the embedded text layer or from OCR, and route thin-text pages to OCR automatically instead of trusting the empty layer. Tables go out as Markdown with the header path attached, and reading order is verified against the original layout for RTL text. That way a wrong answer traces to a specific page and a specific extraction decision instead of a mystery.

I wrote up the benchmark that convinced me of this — MarkItDown returning an empty file for a scanned PDF — and built the approach into pdf-slice. Happy to compare notes on what your fixture catches that mine misses.

Collapse
 
kolmopdf profile image
KolmoPDF •

A useful addition to the page-level fixture is a scanned page with a small embedded page number: a nonempty text layer can still miss the entire body. I'd record the extraction route per page and compare the extracted text with the rendered page before accepting OCR routing. For RTL pages, I'd keep a short expected reading-order sequence separately from the character-recognition check, since readable words can still arrive in the wrong order.

Collapse
 
kamalmosadegh profile image
Kamal Mosadegh •

You're right — a fixture that reproduces the exact wrong join is the right next step, and a before/after pair is what turns "looks fine" into a failing test. I can share a minimal one from my own benchmark set: a small table where the header joins to the wrong dataset, with the expected output alongside, so the failure is reproducible in one command. Happy to drop the file here if that helps.