AI Hints
Home›RAG Tutorial›Limits and Choices
RAG · Lesson 10 of 120

When RAG Goes Wrong

Wrong facts in, wrong answer out.

⏱ 2 min read▶ Video at 9:13🎓 Free
Watch this lesson in the RAG courseWatch on YouTube ↗Full RAG course

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.

When RAG Goes Wrong
From the video: When RAG Goes Wrong
  • 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.

wrong_page.pypython
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())
Output
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.

keyvalue
Missedright text not found
Split badlyfacts cut in half
Buriedtoo much other text
Ignoredmodel 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
Next up: RAG, Fine-Tuning or Long Context — choosing