Extracting and Verifying Citations

Parse the citation markers out of the answer and check every number against the sources you actually sent, catching hallucinated citations.

Part of: Personal Notes Brain

The model replied with something like "You bake bread at 450F [1]." Your job is not done. You need to pull the citation numbers back out of that text, map them to real note titles for display, and check that every number the model used points to a source you actually gave it. Models sometimes cite a number that does not exist, especially in longer answers. A hallucinated citation is worse than no citation, because it looks trustworthy. Pulling citations out with a regex re.findall grabs every [n] pattern as a string. Converting to int and deduping with dict.fromkeys keeps first-appearance order while dropping repeats, since a chunk can get cited several times in one answer. Verifying against the real source list If verify citations returns anything non-empty, the model referenced a source number that was never in the context you sent. That is a hallucinated citation, and it is your signal to either retry the call with a firmer instruction or strip the bad marker before showing the user anything. Why this matters Citations only build trust if you actually check them. A UI that prints "[1] Recipes" under an answer is honest only when you have verified that [1] is really what the mode

Challenge: Fact-Check the Footnotes