Skip to content
Nelson D’SouzaEmail me
Writing

Grounding is a supply chain

Why my RAG system’s wrong citations started upstream of the model, in a book list, a parser, a cleaner and a request, and how I audited each.

By · · 13 min read

  • RAG
  • Grounding
  • Data Quality
  • LLM
In this note
  1. A curated list to stop invented books
  2. 27 of 80 books were someone else's
  3. A model cites what it's handed
  4. Link 1: the catalogue, verified in both directions
  5. Link 2: the first wire
  6. Link 3: a label that was 99.5% complete and wrong
  7. Link 4: navigation, cited as Augustine
  8. Links 5 and 6: a lost ranking, a noisy detector
  9. What the audits cost
  10. Back to "Every answer cites its sources"

Theo Scriptura once recommended The Promises of God and credited it to John Piper. It’s R.C. Sproul’s. That one was the model’s fault. Most of the wrong citations I traced after it were not. They entered upstream, in a book list, a parser, a cleaning helper and a request, and the model cited exactly what it was handed. An answer built that way is grounded in the technical sense, since every claim points at a passage it was given, and the check that watches the model passes it.

Theo Scriptura is my Bible study product. Its Ask feature answers theology questions from the Bible text and a library of 242,284 chunks of commentaries, sermons and theological works. Retrieval is hybrid (vector search and full-text search, merged by reciprocal rank fusion), a cross-encoder reranks the candidates, and the model answers with numbered citations to the passages it was given. A sampled check then asks a second model whether each cited passage supports the sentence that cites it.

The homepage makes a promise: “Every answer cites its sources. Verify for yourself.”

A curated list to stop invented books#

The first citation failure I fixed was that one, a classic hallucination. The model was naming books from memory.

So I stopped the model naming books at all, and constrained every recommendation to the site’s curated catalogue of 80 books, organised by topic and theologian. The model could now only point at entries in a list a person had assembled. Narrowing generation to a trusted source is the standard cure for this kind of error.

27 of 80 books were someone else’s#

I hadn’t checked the catalogue. When I audited it against Open Library, a free book-metadata service, 27 of the 80 entries didn’t resolve to the book they named. Twenty-two resolved to a different book by a different author. Five resolved to nothing.

A Hunger for God by John Piper linked to an ESV Compact Bible. Who Am I? by Jerry Bridges linked to The Message Remix Compact Edition. Unpacking Forgiveness by Chris Brauns linked to William Lane Craig’s Reasonable Faith.

The identifiers gave it away. They had the right publisher prefix and the wrong number: 1433501155 for a Crossway book that is really 1433521407. That’s what an identifier written from memory looks like: the shape is right and the digits aren’t.

The fix for the hallucination had moved the trust boundary from the model to the catalogue, and nobody had audited what was now inside it. The error rate moved with the boundary, from “sometimes invents a book” to “one catalogue entry in three was wrong”. Worse, the narrowing made the errors permanent. Before, a wrong book was one bad generation. After, it was the documented answer, served every time.

If the catalogue could be wrong, so could every other stage that hands the model something.

A model cites what it’s handed#

A RAG system is a supply chain. Text is fetched from sources, cleaned, chunked, labelled, embedded, stored, retrieved, reranked and finally handed to the model, which writes an answer and attaches citations. The reader sees only the last step.

At every earlier step, a plausible but wrong value can enter. A label can be attached to the wrong text. A cleaning step can let page navigation through as prose. A request can lose a field. None of these errors look like errors downstream. The model receives a passage labelled “Augustine” and cites it as Augustine, faithfully. The passage is wrong about itself, and the faithfulness check, the part of the system most visibly about citation integrity, only ever sees the passage.

The citation supply chainArchitecture diagram with 10 nodes, 9 connections. Each step is listed as text below.Source textsClean: navigationkept as proseLabel: 99.5% done,wrongPostgres withembeddingsBrowser: notranslationAsk APIHybrid retrieval,rerankBooks: 27 of 80wrongAnswer withcitationsFaithfulness check10% sampled
  1. Source texts to Clean: navigation kept as prose
  2. Clean: navigation kept as prose to Label: 99.5% done, wrong
  3. Label: 99.5% done, wrong to Postgres with embeddings
  4. Browser: no translation to Ask API
  5. Ask API to Hybrid retrieval, rerank
  6. Postgres with embeddings to Hybrid retrieval, rerank
  7. Hybrid retrieval, rerank to Answer with citations
  8. Books: 27 of 80 wrong to Answer with citations
  9. Answer with citations to Faithfulness check: “10% sampled”
Fig. 1 The citation supply chain Every red stage failed in a way an audit found. None of them is the model, and the faithfulness check at the end can see none of them.

The repair was round-trip verification. For each entry, search Open Library by title and author, take a candidate identifier, resolve that identifier back to a record, and accept it only when the returned record agrees on both title and author. Agreement in one direction isn’t enough: a search can return a near match, and an identifier can resolve to a different edition or a different book.

Round-trip verification of a catalogue entryFlowchart with 8 nodes, 8 connections. Each step is listed as text below.Known-goodentryresolves?Discard this sourceCheck digitvalid?Look up the realeditionSearch title andauthor, resolve theISBNTitle andauthormatch?VerifiedReport it, don'tguessnoyesnoyesyesno
  1. Known-good entry resolves? to Discard this source: “no”
  2. Known-good entry resolves? to Check digit valid?: “yes”
  3. Check digit valid? to Look up the real edition: “no”
  4. Check digit valid? to Search title and author, resolve the ISBN: “yes”
  5. Look up the real edition to Search title and author, resolve the ISBN
  6. Search title and author, resolve the ISBN to Title and author match?
  7. Title and author match? to Verified: “yes”
  8. Title and author match? to Report it, don't guess: “no”
Fig. 2 Round-trip verification of a catalogue entry The control comes first. A source that cannot find a book you know exists is a broken instrument, not evidence.

Two details in that flow are traps the audit ran into.

The control. I tried Google Books as a second source. It returned zero results for every query, including a known-good control. Without the control, that would have looked like independent confirmation that the books didn’t exist. With it, I threw the instrument out instead of believing it.

The check digit. ISBN-10 has a check digit, so a mistyped identifier can often be caught offline. One entry, for Chrysostom’s On the Priesthood, failed it. The arithmetic says the last character should be X, and the “repaired” 088141039X is a valid ISBN that does resolve: to Chrysostom’s On Wealth and Poverty. Applying the repair would have given a green check, a live link and the wrong book under the right title. That’s exactly the failure the catalogue existed to prevent, only harder to spot, so I looked up the real edition instead.

The catalogue now resolves 80 of 80. An offline check-digit test runs on every commit, shown to fail by restoring a real broken entry, and the round-trip script runs whenever entries are added. Of the two, the round trip is the real guard. A check digit catches a typo, not a confident wrong number: 1433501155, the Crossway mix-up above, has a valid one.

One more trap from the same audit: a unit test had asserted that All of Grace linked to 0802454577. That ISBN doesn’t exist. The test passed for as long as the data was wrong, because a literal assertion only checks that a value hasn’t changed.

The next audit started from a reader-visible symptom. A reader who studies in the Berean Standard Bible, the King James, or the Spanish or Hindi translations asked a question and got an answer whose key verse was in the Literal Standard Version (LSV). Over 90 days, the key verse translation broke down like this:

LSV   4,902
BSB       2   (the two test requests that found this)
none      2

Every part of the path existed and worked. The request schema accepted a translation field. The server resolved it into the request context. The verse lookup filtered on it. It was part of the answer cache key, and the interface rendered it beside the reference.

The wire that was never connectedSequence diagram of 7 messages between Browser, Ask API, Retrieval, Model. Each step is listed as text below.BrowserAsk APIRetrievalModelquestion, no translationsearchLSV passagespassagesanswer, KEY VERSE: reffetch the verse, default LSVLSV key verse
  1. Browser to Ask API: “question, no translation”
  2. Ask API to Retrieval: “search”
  3. Retrieval replies to Ask API: “LSV passages”
  4. Ask API to Model: “passages”
  5. Model replies to Ask API: “answer, KEY VERSE: ref”
  6. Ask API, internally: “fetch the verse, default LSV”
  7. Ask API replies to Browser: “LSV key verse”
Fig. 3 The wire that was never connected The server, the verse lookup and the cache all honoured the translation. The browser never put it in the request, so every key verse took the default.

The browser never sent it. It was one missing line at the start of an otherwise complete chain, and that’s why it survived: every component a person would inspect was correct in isolation. The fix reads the translation preference the Bible reader already stores, and sends it.

The interesting part is the fix I didn’t make. The obvious reading of “Ask ignores my translation” is to tell the model, in the prompt, which translation the reader uses. That would have created a new mislabelling bug. I ran the real search function and checked: retrieval returns LSV text, because the embeddings are built on LSV and the function takes no translation parameter. A model told to quote the BSB while holding LSV passages would label LSV text as BSB.

So the arrangement is now honest about what each piece of text is:

Text in the answerWhere it comes fromTranslation and label
Key verseFetched by the server after the answer is written. The model emits KEY VERSE: [reference] and never sees the verse text.The reader’s translation, labelled as such
Quotes in the bodyRetrieved passages, embedded on LSVLSV, labelled LSV

The issue also reported what looked like proof of mislabelling: an answer quoting John 1:1 with what seemed to be BSB text under an LSV label. Checked against the verse table, the two translations read “and the Word was God;” and “and the Word was God.”, identical apart from the closing punctuation. The text was the LSV and the label was right.

A second reported symptom, “one and only Son” quoted for John 3:16, wasn’t the LSV wording and wasn’t plumbing either. The model never receives the key verse text, so that wording came from its own memory. An instruction to quote only what it was given fixed it—a prompt-level guardrail rather than a code guarantee.

John Wesley’s Notes cover the whole Bible, and the work’s title says nothing about which book a given note is on. So his note on 1 John 1:1 could be cited for a question about John 1:1, labelled only “Wesley’s Notes on the Bible”. The fix was to derive each chunk’s book and chapter from the source text, and the first run reported 99.5% of 5,075 chunks located and labelled.

It looked like success. The distribution said otherwise:

largest:  Malachi=392, Genesis=338, Revelation=269, Isaiah=234, Exodus=224

Malachi has four chapters. Genesis has fifty. A four-chapter book can’t be the most commented-on book in the corpus.

333 of those chunks were the publisher’s link index, numbered lists of URLs left behind by the HTML-to-text conversion. They sat at the end of the file, after Malachi’s heading, so a parser tracking “the last heading I saw” labelled all of them Malachi. Excluded, Malachi falls to 59.

A coverage percentage counts rows that got a label, and can’t tell which of them got the right one.

Each guard in the labeller fails toward no label rather than a guess:

GuardWhen it fails
All 66 book headings resolveThe run refuses to start
The chapter stays within the book’s real count and never moves backwardsThe chunk is left unlabelled, and any old label is cleared
The chunk is not the link indexThe chunk is left unlabelled
After the run, the labels ranked by book are plausible from book lengthsI investigate before keeping the labels

An unlabelled passage stays in the index and can still be found. A wrongly labelled one becomes a confident, wrong citation, and if a filter uses the label, it can remove the right commentary from the results. The final result was 4,717 of 5,075 chunks labelled across all 66 books, verified against live retrieval in both directions: a question about John 1:1 now cites “John 1”, and one about 1 John 1:1 cites “1 John 1”.

The checks needed checking too. One asserted that chunks containing the word “Jonah” should be labelled Jonah. They came back John 21 and others, correctly: “Simon son of Jonah” is in John 21. Here the oracle was wrong before the data was.

89 rows in the commentary table were a website’s page furniture (the navigation bar and buttons), stored as the prose of Augustine and Calvin, embedded and citable.

The obvious culprit was the crawler. It even had a visible gap: it removed nav, header, footer and aside elements, and this site’s navigation was a div full of buttons. I ran the crawler’s real code on the real input before changing it. It was innocent: its HTML-to-Markdown step stripped the tags, and its output contained no buttons. Fixing the visible gap would have changed nothing and closed the issue.

The actual path was a set of topic importers that fetched pages as raw HTML and passed them to a cleaning helper written for plain text, which only ran a few whitespace and page-number regexes. That helper had been copied into fourteen importers. There is one importer per topic, and each new one began as a copy of an earlier one, helper included. Hashing the copies found two variants, differing only in comments: harmless, and exactly what fourteen copies of a repair look like shortly before one diverges for real.

Two fixes followed. First, the cleanup of the stored rows was rewritten on a real HTML parser, the same module the ingest path already used. The regex version had needed six passes, each correct and each finding a new shape, among them truncated tags, zero-padded entities, script bodies stored as prose and HTML comments. The parser version reported zero surviving markup on its first run. The fourteen copies of the helper now each delegate to one shared extractor.

Second, the rule “this column holds prose, not markup” moved into the database as a Postgres CHECK constraint. Three independent paths write that table, and the next importer will be a copy of one of the fourteen. A guard in code covers only the callers that remember it; a constraint covers every writer. It is deliberately narrow: it rejects three markers of page furniture (<button, data-toggle and <link rel=) that appear in no row of real prose, because a constraint that rejected a legitimate chunk would be worse than one that misses some markup. It stops the kind of row that got in and no other kind. The delete and the constraint shipped in one transaction, because adding a CHECK constraint validates the rows already there.

Telling furniture from prose had its own trap. One cleaning rule classified a row as furniture if every word in it was on a furniture word list. To catch one site’s apology (“We’re sorry, there does not appear to be a reader compatible version of this book”), the list grew to include not, this, book, version and reader. Every one is ordinary English, and together they would have classified “This book is not of God” as furniture and deleted it. The apology now sits in a phrase list, matched whole; the word list holds only labels.

Two more failures sat outside Ask, in the other places the product tells a reader where something comes from. They had the same shape.

The Bible reader shows cross-references from a 341,279-row dataset. After loading it, Romans 9’s panel showed five references, all from numbered books: 1 John, 1 Samuel, 1 Corinthians, 1 Kings, 1 Chronicles. Every row was real; the query had sorted ties alphabetically.

The dataset lists each verse’s targets most relevant first, so that ranking was the most valuable thing in it, and it lived only in the line order of a text file. The loader used the order to insert rows and threw it away. It was recoverable only because an identity column happened to preserve it.

The rank is now an explicit target_rank column, verified position by position against the source for 124 verses across all 66 books, and Romans 9 opens with 1 Timothy 2:7. Losing an order produces no error.

The blog quotes historic authors, and an attribution audit across 74 posts first reported 204 “unsupported” quotations. Sampled findings were not quotations at all: a Bible verse filed under Matthew Henry because his name was nearby, and spans running from a stray quote mark across a paragraph break.

Each pass at the detector was driven by a sampled false positive. One defect could never have been found that way: a length floor that turned closing quote marks into opening ones and hid ten real quotations. Sampling only sees what the detector emits.

What the corpus cannot settle now goes in a register of hand-verified quotations, each with its work, URL and the date it was read first-hand. Without it, Calvin’s real words on Matthew 5:39, from commentaries the product does not hold, would be “unsupported” on every run, and someone who’d learned to skim the report would eventually delete them.

What the audits cost#

None of this is free, and some of it is permanent work.

  • Audits are recurring, not one-off. Each link needed its own instrument: a round-trip resolver, a distribution check, a reproduction against real input, a sampled detector. New sources will need new ones.
  • Correct labelling limits the product. Body quotes stay in the LSV because it is the only translation embedded. Scripture is stored and embedded verse by verse, with a translation column, so another translation would be more rows of the same kind. The commentary is a different problem. Its chunks are English prose, no Bible translation changes them, and that language limit is the one in Localise the data layer with the UI.
  • Fail-as-absent gives up coverage. A region whose headings stop making sense stays unlabelled. Of the 358 Wesley chunks left unlabelled, 333 are the link index, so the guards cost 25 chunks of real notes.
  • The faithfulness check stays, with a narrower claim. It samples 10% of live answers and asks whether each cited passage supports its sentence. That’s worth knowing, but it’s monitoring only. It doesn’t, and can’t, say whether the passage is what its label claims.

Back to “Every answer cites its sources”#

The homepage still says “Every answer cites its sources. Verify for yourself.” In the narrow sense it was true throughout. Every answer did cite its sources, and the model was faithful to every one of them, including the compact Bible and the navigation bar filed as Augustine.

The part I take most seriously is “Verify for yourself”. A reader who follows a citation should land on the text, author and book the label names. No check on the model’s output can promise that, because the label was decided before the model saw it. It has to be promised stage by stage: a round trip for the catalogue, a distribution check for the parser, a reproduction against real input for the cleaner, each run against a case whose answer is already known.

Built in: Theo Scriptura

All writing