Exercise: fix the failing questions, then cite every claim
- Track
- Retrieval Quality & RAG Applications
- By
- Sourav Banerjee
- Last updated
- 30 Nov 2026
01 Context
Problem statement
T5 left Larkspur Cloud with a measured baseline: TF-IDF top 4 over the handbook, recall at 4 of 0.94 on the golden set. Two questions still come back short. You find out why each one fails, try a reranker and a query expansion against them, and keep only a change that fixes its target without breaking anything else. Then the assistant answers with a citation per claim, and code checks every citation before a reader sees it.
Two rules carry over and one is new. From lesson 5 of T5.1: keep a change only if it improves the questions it targets without breaking the others. From lesson 1 of T6.3: a reranker only reorders the candidates it is given, so it cannot fix a question whose evidence never reaches the list. From lesson 2 of T6.4: a citation counts only when its quote is found, word for word, in the chunk it names.
02 Outcomes
What you will learn
- Turn a baseline row into a list of failing questions, including half-answered multi-hop ones.
- Tell a ranking failure from a vocabulary failure by the rank of each missing span.
- Add a cross-encoder reranker over a deeper TF-IDF list and score it against the baseline.
- Add a query expansion with a local model and see what it breaks.
- Ask for per-claim citations as structured output and check each quote and chunk id in code.
03 Setup
Before you start
Lessons
- T6.1 Retrievers & Hybrid Search: lesson 3 of T6.1 Fusion retrieval for why the hand-over depth matters more than the fusion constant.
- T6.2 Query Transformation & Routing: lesson 1 of T6.2 Query rewriting and expansion for the vocabulary gap and the expansion prompt.
- T6.3 Post-Retrieval Optimisation & Reranking: lesson 1 of T6.3 Two-stage retrieval for the candidate ceiling and choosing the depth, and lesson 2 of T6.3 Cross-encoder reranking in a RAG chain.
- T6.4 End-to-End RAG Systems: lesson 2 of T6.4 Answers with sources and per-claim citations for the citation schema and the quote check.
Dataset
The Larkspur Cloud handbook (13 files) and its 30-question golden set, in data/. 25 questions have evidence spans and are scored. The other 5 are out of corpus and should get no citations.
Code
load_handbook, load_jsonl, tfidf_retriever, hit_rate, recall_at_k, mrr, is_hit and normalise from src/lab_kit.py, RecursiveCharacterTextSplitter, CrossEncoder from sentence_transformers, and ChatOllama from langchain_ollama.
Environment
The course's default environment (uv run jupyter lab), with Ollama running locally and ollama pull qwen2.5:3b done. The first run downloads cross-encoder/ms-marco-MiniLM-L-6-v2 from the Hugging Face Hub. After that everything runs offline, with no API keys. Hosted rerankers and chat models are out of scope here: the reranker is a local cross-encoder and the chat model is a 3B model on your machine.
04 Architecture
Experiment workflow
The corpus, the T5 chunks, k = 4 and the 25 scored questions stay fixed. Each row changes one stage of retrieval, and the citation step runs only on the row the keep rule chose.
Extension: run the cited answer on the 5 out-of-corpus questions and count the citations they get.
05 Build
Implementation tasks
Task 1: Load the data
Load the handbook and the golden set from
data/larkspur/, and split the handbook with the T5 baseline splitter. Give every chunk an id a citation can name, such as the file stem and the chunk's position (10_support_tiers#0).RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)Task 2: Find the failing questions
Build the baseline row: TF-IDF top 4 for every golden question, scored with
hit_rate,recall_at_kandmrr. A question fails when its top 4 misses any of its evidence spans. Count spans, not hits, so a multi-hop question that finds one of its two spans is on the list.Task 3: Diagnose each failure
Retrieve every chunk for each failing question and print the rank of each missing span. A span ranked inside the first 20 is a ranking problem. A span that does not appear scored zero against the question, and no depth or reranker can reach it.
tfidf_retriever(chunks, k=len(chunks))Task 4: Fix A: rerank a deeper list
Retrieve 20 chunks with TF-IDF, score each question and chunk pair with the cross-encoder, and keep the top 4. Load the model once. Score the row and list the questions it fixes and the questions where it finds fewer spans than the baseline.
CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")Task 5: Fix B: expand the query
Ask
qwen2.5:3bto expand each question with synonyms and terms, using notebook01a's instruction from lesson 1 of T6.2. Send the expanded text to TF-IDF top 4 and score it. Then run a third row: the expanded text retrieves 20 and the cross-encoder reranks them against the original question. Make the expansions once and reuse them.Task 6: Decide
Apply the keep rule to each row. When two rows fix the same questions and lose none, keep the cheaper one.
Task 7: Answer with per-claim citations
From the kept row's top 4, ask the model for a list of citations, one per answer sentence, with three fields:
claim,chunk_idandquote. Print each chunk's id above its text in the prompt. Usellm.with_structured_output(...)with a Pydantic schema.New here: a 3B model sometimes returns JSON that does not fit the schema, and LangChain raises
OutputParserException. Catch it and count that answer as a failure instead of letting one question stop the run.Task 8: Check every citation
For each citation, look up the cited chunk among the four retrieved, normalise whitespace and case, and test whether the quote is a substring of it. Count verified citations, failed ids and quotes that are not in their chunk. Split the failed ids in two: an id field that starts with a real id and then carries more text, and an id that matches nothing. Both fail the check. Then read the citations for the questions from task 02.
Task 9: Extension: out-of-corpus questions
Run the cited answer on the 5 golden questions with no evidence. Each should come back with no citations.
06 Measure
Record results
What you enter stays in this browser only.
| Row | Hit rate | Recall at 4 | MRR | Failing questions fixed | Questions lost against the baseline | Kept |
|---|---|---|---|---|---|---|
| TF-IDF top 4 (baseline) | none | none | ||||
| TF-IDF 20 + rerank to 4 | ||||||
| Expansion, TF-IDF top 4 | ||||||
| Expansion + rerank to 4 |
| Citation check, kept row | Count |
|---|---|
| Citations returned | |
| Verified | |
| Failed: id field holds a real id with text pasted after it | |
| Failed: id matches no retrieved chunk | |
| Failed: quote not in its chunk | |
| Answers with every citation verified | |
| Answers whose output did not fit the schema |
A failing question finds fewer than all of its evidence spans in the top 4. A lost question finds fewer spans than it did in the baseline.
07 Verify
Done when
0 of 4 done
What you enter stays in this browser only.
08 Debug
Common mistakes
Reranking a list that never held the evidence
A reranker reorders its candidates. If a span ranks past the depth or scores zero, no reranker brings it back (lesson 1 of T6.3).
Expanding the question that goes to the answer
The expanded text is for retrieval only. The reranker and the answer prompt get the question the user asked (lesson 1 of T6.2).
Trusting a citation because it looks right
A quote can be verbatim and still name the wrong id, or name the right id and paraphrase. Check both in code and show only the citations that pass (lesson 2 of T6.4).
Loosening the check to raise the score
Stripping a broken id until it matches makes the number look better and hides a real failure. Fix the prompt or the schema, then run the check again.
09 Transfer
Interview angle
“Your RAG system misses some questions. How do you decide between a reranker and a query rewrite?”
Show a model answer
Look at where the missing evidence ranks before you choose. Evidence that sits just below the cut is a ranking problem, and a reranker over a deeper list fixes it cheaply. Evidence that shares no words with the question never enters the list, so it needs a change to the query, or a dense retriever. Either way, score the change on the whole golden set and keep it only if nothing else gets worse.
10 Review
Worked solution
The worked solution is solution/p05_exercise_solution.ipynb (open in Colab). Locally, open it from exercise/solution/ so ../data/ resolves, and run it top to bottom with Ollama running. In Colab, clone the repository first, because the notebook reads the corpus and src/lab_kit.py from it, and install Ollama there, or point ChatOllama at a machine that runs it.
Show our run and the reasoning
Our run: qwen2.5:3b through Ollama 0.40.6, cross-encoder/ms-marco-MiniLM-L-6-v2 with sentence-transformers 6.2.0, langchain-ollama 1.1.0 and scikit-learn 1.9.1, offline.
| Row | Hit rate | Recall at 4 | MRR | Failing questions fixed | Questions lost against the baseline | Kept |
|---|---|---|---|---|---|---|
| TF-IDF top 4 (baseline) | 0.96 | 0.94 | 0.89 | none | none | replaced |
| TF-IDF 20 + rerank to 4 | 0.96 | 0.96 | 0.89 | g20 | none | yes |
| Expansion, TF-IDF top 4 | 0.92 | 0.88 | 0.81 | none | g13, g25 | no |
| Expansion + rerank to 4 | 0.96 | 0.96 | 0.90 | g20 | none | no, same fix at a model call more |
| Citation check, kept row | Count |
|---|---|
| Citations returned | 37 on 25 questions |
| Verified | 19 (51%) |
| Failed: id field holds a real id with text pasted after it | 12 (32%) |
| Failed: id matches no retrieved chunk | 6 (16%) |
| Failed: quote not in its chunk | 0 |
| Answers with every citation verified | 8 of 25 |
| Answers whose output did not fit the schema | 1 (g12) |
The baseline fails two questions. g20, "How fast will a Business customer hear back on a service-credit claim?", finds the 4-hour support span at rank 2 and the Billing-ticket span at rank 12. That is a ranking problem. The reranker reads 20 candidates and lifts the second span into the top 4, at 139 ms per question on our laptop, and no other question loses a span. g02, "What happens if my server goes down?", is a vocabulary problem. Its availability span ranks 15th, and its status-page span shares no word with the question, so TF-IDF scores it zero and it never enters the list. The reranker cannot reach it, and it keeps the availability chunk out of the top 4, behind the reboot chunk that matches "server". Lesson 1 of T6.2 shows both g02 chunks at zero on the T5.1 index. With the T5 chunks, one of them scores just above zero, and the result is the same.
The expansion turns g02 into "What are the consequences when my server experiences a failure or outage?". "Outage" is the right word, but it is not enough to fix g02, and the longer queries cost two other questions: g13 ("How is my data encrypted?") and g25 (the outbound traffic price) lose their only evidence chunk to the added words. That is the dilution lesson 1 of T6.2 warns about. Expansion plus rerank recovers both, because the cross-encoder reads the original question, but it fixes only g20, the same as the reranker alone, and adds a model call of about 1.3 s per question. The keep rule passes both rows, and the cheaper one stays. g02 is still open, and the next thing to try is a dense or hybrid retriever (T6.1), which places "goes down" near "outage".
The citations are where the 3B model struggles. Every quote it returned is a real sentence from the handbook, and no quote failed the substring check. The ids are the problem. In 12 citations the model copied the id and then the whole chunk text into the chunk_id field, because the prompt prints each id on the line above its chunk. In 6 more the id matches none of the four retrieved chunks. g01's refund citation is a typical case: the quote is the exact "5 to 10 business days" sentence, and the id field starts with 02_refunds#0 and goes on with the chunk. The check fails it, as it should, since stripping the field until it matches would hide the fault instead of fixing it. The fix belongs in the prompt or the schema, for example ids in a format the model cannot run into the text, or a list of allowed ids in the schema. g12's output did not fit the schema at all, and g08 and g13 came back with no citations even though their evidence was in the top 4. g20's answer cites only the 4-hour span, so the reranker fixed its retrieval but the answer still leaves out the Billing ticket.
Extension: the 5 out-of-corpus questions all came back with no citations, so the model did not invent support for questions the handbook does not answer.