Topic 4 of 4 · 4 min read
Retrieving from spreadsheets and JSON records
Goal: Choose a representation for a spreadsheet or product catalogue that keeps rows and headers together and test it with three queries.
Chunking a spreadsheet as plain text separates each row from its column headers, so load tables into a form that keeps the two together and query it. A row such as 4419, 24.90, 3 means nothing without the header that says which number is the price.
Check yourself · 1 of 3
What is the main problem with splitting a 5,000-row CSV into fixed-size text chunks?
Why it matters
A text splitter cuts by length. On a CSV with 5,000 rows, only the first chunk contains the header line. Every other chunk is a block of numbers with no labels. Embeddings of those blocks look alike, so search returns arbitrary rows, and the model reading them has to guess what each column means.
How to do it
- Pick the representation by the question. For counting, filtering and totals, load the file into a table (SQLite, DuckDB, Postgres or a dataframe) and use text-to-SQL. For "find the product that matches this description", turn each row into a short labelled sentence and embed that.
- Repeat the headers in every unit. A row document reads
product: Trail Jacket; colour: green; price_eur: 89.90; stock: 14. Now any single chunk is self-explanatory. - Fix types on load. Prices as numbers, dates as dates, empty cells as nulls. A column of numbers stored as text sorts "9" after "10".
- Handle nested JSON on purpose. Flatten stable fields into columns, keep one-to-many parts such as
variantsin a child table with the parent id, and store the raw record for display. - Keep metadata for filtering. Category, brand and price range as filters narrow a semantic search before ranking.
- Test with three queries. One exact lookup ("price of SKU TJ-204"), one filter ("green jackets under 100 euros"), one aggregate ("how many are out of stock"). If any of the three fails, the representation is wrong for that need.
Before and after
Weak: the CSV split into 1,000-character chunks.
...5, Merino Hat, grey, 24.90, 3
...6, Trail Jacket, green, 89.90, 14
Fixed: one labelled record per row, plus the same data loaded as a table.
sku: TJ-204 | name: Trail Jacket | colour: green | price_eur: 89.90 | stock: 14
The semantic questions use the labelled records. The totals use SQL over the table.
What goes wrong
- Wrong tool for aggregates. Vector search cannot sum a column. It returns a few similar rows and the model adds them up, wrongly.
- Merged cells and multi-row headers. Excel layouts with notes above the table or merged cells break loaders. Clean the sheet to one header row first.
- Units and locale.
1.234can be one thousand or one point two. Fix the locale on load. - Stale snapshots. A catalogue that changes daily needs a refresh job, or the answers describe yesterday's stock.
Examples
A catalogue answered two ways
A shop loads its product CSV into DuckDB. "How many green jackets are in stock?" becomes a SELECT sum(stock) with a colour filter. "Something warm for hiking in winter" goes to an index of labelled row sentences, filtered to the jackets category. One source file, two retrieval paths chosen by question type.
Nested JSON with variants
A product record has variants: [{size: "M", stock: 3}, {size: "L", stock: 0}]. Stored as one text blob, "is it in stock in L?" depends on the model reading the nesting correctly. Loaded as a variants table with product_id, size and stock, the answer is a one-line query and no guess.
Key points
- A length-based text splitter separates rows from their headers, leaving blocks of unlabelled numbers.
- Use a queryable table for counting, filtering and totals, and labelled one-row records for semantic matching.
- Repeat the column names in every row document so any single chunk explains itself.
- Fix types, locale and empty cells at load time, and flatten JSON into columns and child tables on purpose.
- Test with an exact lookup, a filter and an aggregate; each exposes a different failure.
Common mistakes
- Splitting a CSV by character count and indexing the pieces, which leaves most chunks without headers.
- Using vector search to answer sums, averages or counts over a column.
- Loading numbers and dates as text, so sorting and comparisons give wrong results.
- Indexing a spreadsheet with notes, merged cells or several header rows without cleaning it first.
Check yourself · 2 of 3
A user asks, "What is the total stock of all green products?" Which representation answers it best?
Check yourself · 3 of 3
Why write each row as sku: TJ-204 | colour: green | price_eur: 89.90 before embedding it?
Practice items, not counted towards your mastery.
Start here
- From Local to Global: A Graph RAG Approach to Query-Focused Summarization (opens in a new tab)PaperFree
The paper behind GraphRAG: entity graphs, community summaries and why plain RAG fails on whole-corpus questions.
- GraphRAG documentation (Microsoft) (opens in a new tab)DocsFree
The indexing pipeline, query modes (local, global) and configuration of an open-source GraphRAG system.
- BIRD text-to-SQL benchmark (opens in a new tab)PaperFree
A text-to-SQL benchmark on large, messy databases, with a leaderboard showing how far models still are from reliable.
More for this topic
- CSV files overview (DuckDB documentation) (opens in a new tab)DocsFree
Loading a CSV into a typed, queryable table with header detection, so rows and headers stay together.
- Spider 2.0: enterprise text-to-SQL workflows (opens in a new tab)PaperFree
Enterprise-style text-to-SQL tasks with big schemas and multi-step queries; shows why real databases are harder than demos.