RAG over financial reports returns wrong totals when asked to sum or compare figures — what is the right design?
Similarity search returns approximately relevant text, not the exact rows an aggregate needs; route numeric and aggregate questions to structured retrieval like text to SQL so totals are computed exactly.
Imagine asking a helper to total your monthly spending, but instead of handing them your bank statement, you hand them a stack of sticky notes that merely look related to money. They'll add up whatever notes they happened to grab and confidently give you a number — but it'll be wrong, because some receipts were never in the stack and they're doing the math in their head. The fix isn't a thicker stack of sticky notes or fancier handwriting. The fix is to pull up the actual statement in a spreadsheet and let the calculator add the column. For a system reading financial reports, that means turning 'what's the total revenue?' into a precise database query that runs over the real rows, instead of letting the language model eyeball a pile of similar-looking text.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3-4 min: why similarity search can't guarantee a complete figure set, why LLM arithmetic over chunks is unreliable, and the route to structured retrieval (text to SQL / table extraction) fix versus the distractors.
| Question type | Right retrieval approach |
|---|---|
| Exact figures or aggregates (sum, count, max, compare) | Structured query: text to SQL or table extraction, executed for an exact result |
| Fuzzy or semantic lookup over prose | Vector retrieval over chunks |
| Mixed (narrative plus a number) | Route per sub-question, then combine the results |
Real products, models, and research that use this idea.
- A finance assistant over 10-K filings that quoted wrong segment totals until aggregate questions were routed to text to SQL over an extracted tables database.
- LlamaIndex and LangChain ship SQL query engines and table-retrieval tools precisely for this numeric-question path alongside vector retrieval.
- BI copilots (e.g., over a warehouse) that generate and run SQL for 'total', 'average', and 'compare' questions rather than retrieving prose summaries.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does the router decide a question is numeric/aggregate versus a factual lookup?
QWhat are the main failure modes of text to SQL and how do you mitigate them?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Trying to fix wrong totals by raising top-k or shrinking chunks; both keep the LLM doing unreliable mental math over an incomplete set of figures.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.