# How Do You Handle Tables, PDFs and Images?

> Why retrieval-augmented generation (RAG) breaks on tables, PDFs and images, and how to parse, keep and search them, explained simply for your AI interview.

**In short:** Parse the file with a layout parser, not plain text, so tables keep their headers and scans get OCR (reading the letters from a picture). Keep each table whole, describe each chart or keep its page picture, and keep the page number so answers can cite it. Then test on questions that need a table number.

AI and LLM interview question Q130, Retrieval-augmented generation (RAG). Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/how-do-you-handle-tables-pdfs-and-images

## Say it in 30 seconds

Tables, PDFs and images are where RAG often goes wrong. A plain text dump loses the table's columns, and a scan has no text until you run OCR, which reads the letters. So I parse the file with a layout parser, keep each table whole with its headers, and store the page number so the answer can point back to it. Charts get a short description, or the page picture goes to a model that can see it. Then I test with real questions that need a number from a table.

## Say it in 2 minutes

The first mistake is treating every file as plain text. A PDF can hold a text layer, which is real text, or it can be a scan, which is only a picture of a page. A scan needs OCR, optical character recognition, to turn the letters into text. Either way, a plain text dump loses the table's columns, so a fee of 35 can end up with no label next to it.

So I parse first. A layout parser reads the page, finds the tables and headings, and returns each table as one unit with its column names. I keep a small table whole. If a table is too long for one chunk, I split it by row, and each piece repeats the table title and column names. Each chunk also keeps its page number and source file, so the answer can cite the page and a person can check it.

Charts and photos are different. I either write a short description of each useful chart when I index, which is cheap to search but can miss a detail, or I keep the page picture and send it to a vision model (an AI model that can look at pictures) when someone asks. The first is cheap per question, and the second reads the chart but costs more every time. I choose by how often people ask about that chart, and I test both on real questions.

Last, I test. I take real questions from the team, including ones that need a number from a table or a chart, and check each answer against the page. If the number is wrong, I look at the chunk first, because it may have lost its headers.

## What do tables, PDFs and images have to do with RAG, in plain words?

RAG (retrieval-augmented generation) is when a bot searches your documents for the right chunk, then gives that chunk to the model to answer from. That only works if the chunk says what the text says. Many company files are PDFs with tables, scanned forms and charts, and plain text misses much of that.

Picture a new credit analyst who copies a rate table into a notebook, one line per rate and no column titles. By Friday they quote the wrong rate, because nothing on the line says which product it belongs to. A RAG bot has the same problem when a table is flattened into text.

- **Parsing:** Reading a file and pulling out its text, tables and layout.
- **OCR:** Optical character recognition: reading the letters in a scanned picture.
- **Chunk:** One piece of a document, stored and searched as a unit.
- **Page image:** A picture of a whole page, so its layout and charts survive.
- **Index:** The searchable store the bot looks in to find chunks.

## How do you get text out of a PDF without losing its structure?

First, check what kind of PDF it is. One made from a word processor has a text layer, meaning the words are stored as real text, so they can be read directly. A scan is only a picture, so it needs OCR before anything can be searched. Azure's document service describes this step as OCR that extracts text, tables, structure and key/value pairs.

Then parse the layout too. A layout parser keeps the page's structure, so a table comes out as cells with their headers, and a heading stays above the paragraph it belongs to. Google's Document AI also has a Gemini layout parser that returns text, tables and lists as context-aware chunks.

*Diagram, From a PDF to searchable chunks: A policy PDF goes to a layout parser first. Tables stay whole with their headers, charts become a description or a page picture, and prose is cut at its headings. Every chunk keeps its page number, so the answer can point back to the source.*
1. The file goes to a parser first, not straight into the index.
2. A table stays one unit, with its column names.
3. A chart becomes a short description, or its page picture is kept.
4. Plain prose is cut at its headings.
5. The table chunks go into the index.
6. Figure descriptions go in too.
7. Prose chunks go into the index with their page number, so answers can cite it.

> **Tip:** Plain text extraction is a common cause of wrong table answers. If the parser returns a table as a run of words with no cells, fix the parser before you touch the model or the prompt.

## How do you store a table so the answer is right?

- **Row alone: A number with no name.** The chunk says International wire: 35. It does not say which account type or currency the fee is for, so the answer can be wrong.
- **Row with headers: The table title and column names come too.** Each row keeps the table title, the column names and the account type, so a reader or the model can tell what 35 means.

This is a context problem. Anthropic's engineers wrote that a chunk on its own can fail to say which company or period it covers. Their fix adds a short context line to each chunk before it is embedded. For a table, the simplest context is the title and headers, repeated on every row chunk.

```python
# A made-up business fee table, split two ways: one chunk per row, or each row with its title and headers.
TITLE = "Business account fees, USD"
HEADERS = ["Service", "Fee"]
ROWS = [["Domestic wire", "15"], ["International wire", "35"], ["Cheque deposit", "2"]]

def naive_chunks(rows):
    # The row alone: the number has no title or column names around it.
    return [r[0] + ": " + r[1] for r in rows]

def kept_chunks(rows):
    # Each row keeps the table title and column names, so it reads correctly alone.
    return [TITLE + ". " + HEADERS[0] + " | " + HEADERS[1] + ". " + r[0] + " | " + r[1] for r in rows]

print("Naive: " + naive_chunks(ROWS)[1])
print("Kept:  " + kept_chunks(ROWS)[1])
print(len(kept_chunks(ROWS)), "chunks, each with the title")
```

Output:

```text
Naive: International wire: 35
Kept:  Business account fees, USD. Service | Fee. International wire | 35
3 chunks, each with the title
```

Microsoft's chunking guide advises reformatting tables so they are easier to process. Its other option is to have a language model write a text description of each table. That works, but Microsoft marks its processing cost as high, so you pay for it at indexing time.

## How do you handle charts and images?

Charts hold facts that text extraction often misses, like a fee trend or a limit shown as a bar. You have two routes, and the trade-off is cost against accuracy.

> "Documents are visually rich structures that convey information through text, but also figures, page layouts, tables, or even fonts." (ColPali, 2024 research paper)
>
> "For non-PDF files, the Responses API doesn't extract embedded images or charts into the model context." (OpenAI)

*Diagram, Two ways to make a chart searchable: A chart on page 14 has two routes. Route 1 writes a description when you index, so search is cheap but can miss a detail. Route 2 indexes the page text so the page can be found, then sends the page picture when someone asks, so the model reads the chart but costs more on every question.*
1. Route 1: a model writes a text description when you index the file.
2. Route 2: the page text is indexed, and the page picture is sent to the model when someone asks.
3. The description is searched like any other text.
4. The model looks at the picture on each question, so each answer costs more.

Some setups already send the page. Anthropic says each PDF page reaches Claude as an image with its text, so you can ask about charts and diagrams. OpenAI says that for PDFs the API extracts both text and page images and sends both to models that accept image inputs, but for other file types it does not pull embedded charts into the context. Check what your API really sends before you assume the model can see a chart.

A research line called ColPali goes further and searches page images directly, instead of the text pulled out of them. It is a newer approach, so test it before relying on it.

Page images cost more. Anthropic warns that dense PDFs, with many small-font pages, complex tables or heavy graphics, can fill the model's working memory (its context window) before the page limit is reached. Split big files into sections when that happens.

### How would you build search over 3,000 scanned insurance policy PDFs?

Start by sorting the pages. Some have a text layer, some are scans, and some are scans of tables. Run OCR only where there is no text, and check a sample of pages by eye.

1. **Parse with the layout:** Find the tables and headings, and keep each table whole with its title and column names.
2. **Describe only the charts people ask about:** Read the question log first. Describe the few charts that matter. Keep the rest as page pictures, with the page text indexed so the page can be found.
3. **Tag every chunk:** Store the policy number, page and table id, so answers cite the page and access rules can filter by policy.
4. **Test on real questions:** Use 100 questions the claims team actually asked, and check each number against the page.

The model should take numbers from the table it retrieved, never from memory. If the table is missing from the results, the bot should say so, not guess.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] Check whether a PDF has a text layer or is a scan.
- [ ] Use a layout parser, so tables keep their cells and headers.
- [ ] Keep each table with its title and column names on every chunk, and split it by row only when it is too big to keep whole.
- [ ] Handle charts with a description, or a page image, and name the cost of each.
- [ ] Keep the page number on every chunk, so answers can cite it.
- [ ] Test with real questions that need a number from a table.

**Common traps**

- Trap: "Just paste the PDF text into the prompt." Better: Plain text drops table columns and misses charts. Parse the layout first.
- Trap: "Chunk every 500 characters, tables included." Better: A cut through a table splits a fee from its label. Keep tables whole.
- Trap: "The model reads images, so there is nothing to do." Better: Only some setups send page images. Check what your API really passes on.

## Can you spot the problem?

**Spot the problem:** A user asks the bank's assistant for the international wire fee on a business account. Which retrieved chunk can the assistant answer from?

A. Wire fees: the international row is in the table
B. Business account fees, USD. Service | Fee. International wire | 35
C. Wire fees are listed in the appendix on page 9.
D. Fee schedule, retail accounts. International wire | 20

**Answer:** B. Only the chunk that names the table, the account type, the currency and the column names gives the fee in context. A pointer with no figure cannot answer, and the retail row is for another product.

**Quick check:** A claim form's fee table is a photo inside a scanned PDF. Which change lets the assistant read it?

A. A longer instruction to the bot
B. Run OCR, then keep the table whole with its headers
C. A bigger model
D. Less randomness in the answers

**Answer:** B. A scan is a picture. OCR or a layout parser turns it into text with the cells and headers. A longer instruction, a bigger model or less randomness in the answers cannot read pixels.

## Can you answer these from memory?

**Flash cards**

- **Text PDF or scan?** A text PDF has real text. A scan is a picture and needs OCR first.
- **Why keep tables whole?** A split table loses its headers, so the numbers lose their meaning.
- **What goes on each table chunk?** The table title and the column names, so the row makes sense alone.
- **Chart: two routes?** A description at index time, or the page image at answer time.
- **Why keep the page number?** So each answer can cite its page and a person can check it.
- **How do you test it?** Real questions that need a table number, checked against the page.

## Likely follow-up questions

- Q131 Retrieval metrics: recall@k, MRR, nDCG? https://designervenkat.online/interview/ai/retrieval-metrics-recall-at-k-mrr-ndcg
- Q135 How do you add citations? https://designervenkat.online/interview/ai/how-do-you-add-citations
- Q139 How do you keep the index fresh? https://designervenkat.online/interview/ai/how-do-you-keep-the-index-fresh
- Q442 How do images become tokens? (full answer coming)
- Q448 How do you extract structured data from documents and images? (full answer coming)

## Learn it in depth

- [Chunking](https://designervenkat.online/learn/ai-engineering/chunking)
- [Image Understanding](https://designervenkat.online/learn/ai-engineering/image-understanding)
