Sub topic 11
Verifying Quoted Text Against Sources
This chapter assumes you can write basic Python and parse JSON. It also assumes you have a numbered context block and a validator for [n] markers, as covered in earlier chapters.
Why marker checks are not enough
A marker check confirms that [2] points to a real source. It does not confirm that source 2 says what the answer claims. A model can cite a real source for a statement the source never made.
Quote verification adds a stronger test. You ask the model to copy a short span of text from the source. Your code then checks that the span really appears in that source. Copying is something code can check exactly. Judging meaning is not.
Ask for quotes in structured output
Quotes work best in the structured format from the previous chapter. Each claim lists its source_ids and a short quote.
{
"answer": "The service retries failed requests up to three times.",
"claims": [
{
"text": "The service retries failed requests up to three times.",
"source_ids": [2],
"quote": "failed requests are retried up to three times"
}
]
}In the prompt, ask for a short, exact quote. Tell the model not to paraphrase or fix the wording inside the quote. A span of one sentence or less is easier to match and easier for a person to read.
The check
The function below normalizes both strings by lowercasing and collapsing whitespace. It then tests whether the quote is a substring of the source.
def normalize(text):
return " ".join(text.lower().split())
def quote_is_supported(quote, source_text):
"""True if the quote appears verbatim in the source, ignoring case and whitespace."""
return normalize(quote) in normalize(source_text)Normalization matters because line breaks and double spaces in retrieved text often differ from what the model outputs. Without it, you reject honest quotes.
Check against the cited source only
Run the check against the sources the claim names, not against all sources. If you search everything, a quote from source 3 would pass a claim that cites source 2. That hides a misattribution.
def verify_claims(claims, sources):
"""sources: list of dicts with 'text'. Returns a list of (claim_text, problem) pairs."""
problems = []
for claim in claims:
ids = claim["source_ids"]
if any(not 1 <= n <= len(sources) for n in ids):
problems.append((claim["text"], "unknown source id"))
continue
if not any(quote_is_supported(claim["quote"], sources[n - 1]["text"]) for n in ids):
problems.append((claim["text"], "quote not found in cited sources"))
return problemsflowchart TD
A[Claim with source_ids and quote] --> B{Ids in range?}
B -- no --> X[Flag: unknown source id]
B -- yes --> C{Quote found in a cited source?}
C -- no --> Y[Flag: quote not found]
C -- yes --> D[Claim passes quote check]The diagram block above uses the required format below.
Common errors
- Treating a pass as proof of support. A quote can exist in the source and still not back the claim. The quote may be about something else, or it may be negated nearby. Quote checks catch fabricated text, not wrong reasoning.
- Quotes that are too short. A quote like "three times" matches almost anywhere. Require a minimum length, such as five words.
- Over-strict matching. Curly versus straight quotes and Unicode dashes cause false failures. Normalize these as well if your sources contain them.
- Silent failure. When a claim fails, decide what happens. Options include removing the claim, regenerating the answer, or showing the answer with a warning.
Exercise
A claim cites source 2 with the quote "retried up to three times". The quote appears in source 3 but not in source 2. Should verification pass, and why?
Show answer
No. Verification must search only the cited sources. The quote is real, but it is attributed to the wrong source, so the claim is misattributed and should be flagged.
Exercise
Why does quote_is_supported normalize whitespace and case before comparing?
Show answer
Retrieved text often contains line breaks and irregular spacing, and the model may change capitalization. Normalizing prevents honest quotes from being rejected for formatting differences alone.