When the Answer Isn't in the Document

grounding and refusal

Part of: Chat With Your PDF

The worst failure in a RAG app is rarely a crash. It's a confident, well-written answer that happens to be wrong because the document never covered the question. This lesson catches that before it reaches the user. Two guards, two jobs - Before the call : if nothing retrieved is actually relevant, don't ask the model at all. Say so. - After the call : if the model cites a source, check the citation is real. Guard one: the similarity floor Cosine similarity hands you a warning sign for free. If the best chunk still scores low, the document probably doesn't cover the topic, and no amount of clever prompting fixes that. Set a floor and refuse below it: It also saves money. A question with no relevant match skips the more expensive generation call entirely. Guard two: citation validation Even with good context, a model will sometimes cite a source number that doesn't exist, like "[5]" when you sent only 3 chunks. That's a small but real hallucination. Catch it with a regex: If citations valid fails, treat the answer as untrustworthy. Retry, strip the bad citation, or fall back to "I couldn't verify this answer." Where it earns its keep A RAG app that never says "I don't know" is harder

Challenge: Validate the Grounded Answer