When RAG Goes Wrong
Wrong facts in, wrong answer out.
A RAG answer can only be as good as the text we retrieve. If the retriever picks the wrong page, even a great model gives a poor answer. Let's see it happen.

- Answer depends on retrieval
- Wrong page
- Poor answer
A question in different words
This time the customer asks the same thing in other words, can I get next day delivery on a bike. Small common words like can, get, and bike match the rentals page more often. Run it. Word matching picks the rentals page, so the model can't find the answer.
import re
from kb import load_docs
from langchain.chat_models import init_chat_model
llm = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
words = lambda t: set(re.findall(r"[a-z0-9]+", t.lower()))
def retrieve(question, docs):
return max(docs, key=lambda d: len(words(question) & words(d["text"])))
question = "Can I get next-day delivery on a bike? One sentence."
page = retrieve(question, load_docs())
print("Using:", page["source"])
prompt = f"Answer only from this text. If it isn't there, say so.\n\n{page['text']}\n\nQuestion: {question}"
print(llm.invoke(prompt).text.strip())Using: rentals.md That isn't in the text.
Also uses kb.py
from pathlib import Path
def load_docs(folder="data/rag/northwind"):
return [{"source": p.name, "text": p.read_text()} for p in sorted(Path(folder).glob("*.md"))]Where RAG fails
Most RAG failures come from retrieval. The right text wasn't found, or it was split badly, or it was buried among other text. Sometimes the model ignores the context. The fix is almost always better retrieval, and that's what most of this course is about.
| key | value |
|---|---|
| Missed | right text not found |
| Split badly | facts cut in half |
| Buried | too much other text |
| Ignored | model guesses anyway |
Summary
If retrieval picks the wrong text, the answer is wrong. Better retrieval is the main way to improve RAG. Next, RAG compared with other options.
Key takeaways- Garbage in, garbage out
- Retrieval matters most
- Search by meaning next