WRITE · TRACE · UNDERSTAND

Reject citations outside the supplied evidence set

Implement citation_errors(claims,allowed).

Read lesson: Reject citations outside the supplied evidence set →

Your experiment

Implement citation_errors(claims,allowed). Each claim has text and citations list. Return ordered strings "i:missing" if no citations and "i:unknown:key" for each distinct unknown id in that claim, preserving first occurrence. Empty claims return [].

Python runs in your browser. Your draft stays on this device.

What to look for

Identifier validation proves only that a reference belongs to the supplied set. Faithfulness needs a separate check against the referenced content, and uncertain claims should remain qualified rather than acquiring false certainty from a valid citation id.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Reject citations outside the supplied evidence set works

The idea

A model that answers from retrieved documents can cite a source it was never given. citation_errors catches that with plain code: every claim must cite something, and every cited id must belong to allowed, the set of evidence ids the application supplied.

Nothing here asks the model whether its citations are right, and nothing judges whether a source supports the claim. The function only compares ids against a list the application controls, which makes the check cheap, deterministic, and out of reach of anything written in a prompt. It returns error strings in claim order: i:missing for a claim with no citations and i:unknown:key for each distinct id outside the set. An empty list means every citation points at supplied evidence.

How it works

  1. Convert allowed to a set for fast membership tests.
  2. Visit the claims in order together with their index i.
  3. Remove repeated citation ids within the claim, keeping first occurrences, to get refs.
  4. Append i:missing when refs is empty.
  5. Append i:unknown:key for each id in refs that is not in allowed, in order.
  6. Return the list of errors, which is empty when every claim cites only supplied evidence.

What it costs

Time
O(total citations)
Space
O(allowed + errors)

Each citation costs one set lookup, so the check is negligible next to the model call that produced the claims.

Where it is used

  • Anthropic's Citations feature for Claude returns cited passages that are guaranteed to be valid pointers into the documents supplied with the request.
  • OpenAI's file search tool returns file_citation annotations that name the uploaded file each cited passage came from.
  • The ALCE benchmark from Princeton measures citation precision and recall for language model answers that cite retrieved passages.

Watch out for

  • Asking the model to confirm its own citations: the check has to be code that compares ids with the list the application supplied.
  • Reporting the same unknown id twice for one claim: the Lab expects each distinct id once, in order of first appearance.
  • Reading a clean result as proof of truth: a known id can still be attached to a claim its source does not support.