Skip to content
Raisalon

Artificial Intelligence

Test whether repeating PDF table headers improves RAG retrieval

Run a four-question retrieval test on extracted PDF table rows. Compare row-only chunks with repeated headers and separate retrieval errors from answer errors.

Author
4 min read

When retrieval-augmented generation (RAG) answers a question about a PDF table with the wrong number, inspect the retrieved row. A chunk such as Premium | 40 preserves the value and loses its meaning. This walkthrough tests one change: carrying the column header into every row chunk. You can run the comparison in Python without an embedding service or a language model.

Find the first place the meaning disappears

Imagine a product sheet with two tables. One gives response latency in milliseconds and the other gives throughput in requests per second. Both organize rows by plan. These are invented values chosen to make the ambiguity visible.

Plan Latency (ms)
Standard 80
Premium 40
Plan Throughput (requests/s)
Standard 20
Premium 50

In a real PDF pipeline, first compare the extracted text with the visible page. If a parser already attached 40 to the Standard row, repeating the header just preserves a wrong association. Check merged cells, blank cells that inherit a group label, and tables continued on the next page. A screenshot or page reference helps a reviewer trace any answer back to the source. LangChain’s table retrieval benchmark

Change only the chunk representation

The script below starts after extraction. It represents each row once as plain cells and once with its original header. The search ranks chunks by the number of query words they contain. Ties go to the earlier row in source order. This simple rule makes the mechanism easy to audit. Its results describe this four-row fixture.

import re

rows = [
    ("lat-standard", "Plan Latency ms", "Standard 80"),
    ("lat-premium", "Plan Latency ms", "Premium 40"),
    ("rate-standard", "Plan Throughput requests/s", "Standard 20"),
    ("rate-premium", "Plan Throughput requests/s", "Premium 50"),
]
questions = [
    ("standard latency", "lat-standard"),
    ("premium latency", "lat-premium"),
    ("standard throughput", "rate-standard"),
    ("premium throughput", "rate-premium"),
]

def words(text):
    return set(re.findall(r"[a-z0-9]+", text.lower()))

for keep_header in (False, True):
    chunks = [
        (row_id, words(f"{header} {cells}" if keep_header else cells))
        for row_id, header, cells in rows
    ]
    correct = 0
    for query, expected in questions:
        query_words = words(query)
        found, _ = max(chunks, key=lambda chunk: len(query_words & chunk[1]))
        correct += found == expected
        print(query, found, expected)
    print("Include header:", keep_header, "correct first results:", correct)

With row-only chunks, the correct row comes first for two of four questions. Both throughput queries tie with the corresponding latency row, which appears earlier. Repeating the headers gives four correct first results. For premium throughput the row containing 50 now matches two terms. The latency row containing 40 matches one. Reversing the table order in the baseline swaps which metric fails.

Evaluate row retrieval and numeric answers separately

A search result can point to the right page and still supply the wrong cell. Give each evaluation question a document, page, table, row identifier and expected value with its unit. If only one row supports the answer, count how often that row appears first and how often it appears anywhere in your top five. A generator can only use evidence that survives retrieval and makes it into its context.

Then inspect the generated answer separately. A system that retrieved Premium | 50 under the throughput header and answered “50 milliseconds” retrieved the evidence but interpreted it incorrectly. A system that never returned that row needs attention earlier in the pipeline. Reporting a single answer score hides the distinction.

Choose a chunk size with a budget

For a narrow table, one row plus headers can fit comfortably in a chunk. A wide table may consume most of its token budget repeating long headings. Group adjacent rows under one header and measure whether the correct value remains easy to locate. For a header such as “2025” underneath “Operating expenses,” retain both levels. A bare year cannot identify the measure.

To compare 128-, 256- and 512-token chunks on your own PDFs, hold the questions, parser, embedding model and ranking settings fixed. Record how many chunks each version creates and how many tokens reach the answer model. Keep questions that require two rows, such as the difference between plans, in a separate group. Their supporting evidence spans more than one cell.

Keep values, units and provenance together

A practical chunk for the last row could read “Product limits. Premium plan. Throughput: 50 requests per second.” Keep the document ID, page number and table identifier in metadata so a search result can link to the source. If the same PDF has an older schedule in an appendix, also keep the effective date or section name with the chunk. Otherwise the retriever may return a correctly labeled value from the wrong period.

Add at least one unanswerable question, such as “What is the Enterprise plan throughput?” when the table contains only Standard and Premium plans. A nearest-match retriever can still return a Premium row. For that question, inspect whether the answer stage acknowledges the missing plan. Counting every nonempty result as a success would hide this failure.

Separate extraction, retrieval and response failures in your notes. Each needs different evidence to fix: a misread cell on the page, an absent row in the candidate list, or a wrong answer despite the correct row being present.

Next steps

Start with five questions whose answers you can point to on a page. Keep a copy of the top retrieved chunks for both representations. If header repetition improves row retrieval, try it on another document layout before adopting it across the collection. If both representations miss the same value, inspect the parser output before spending time tuning the chunk size.

Share

Written by