skip to content

When an ingestion pipeline re-chunks an edited document, how do you diff the new chunks against the stored ones so only changed chunks are re-embedded?

level: seniorimportance: should knowfreq 28%

answer

  1. fingerprint what the model sees
  2. include the model version
  3. match as a multiset
  4. reuse, embed, supersede
  5. log the re-embed fraction

basics

~10 s

Fingerprint each new chunk's exact embedding input plus the model version, match fingerprints against the document's stored chunks, reuse vectors for matches, embed only the misses, and retire stored chunks nothing matched.

solid answer

~40 s

Re-chunk the new version, then compute for each chunk a fingerprint of exactly what the embedding model will receive, chunk text plus any prefix folded into the input, together with the model version and any embedding parameters. Load the stored fingerprints for that document and match them as a multiset, so duplicate passages pair off one by one. Every match keeps its vector, with only its position or ID rewritten if those moved; every unmatched new chunk goes to the embedder; every stored chunk left unmatched is superseded and handed to the replacement step. Fingerprint the input, not the raw text, or a changed prefix slips through and stale vectors survive. Log the re-embed fraction per edit: it proves the diff works and catches the day it stops working.

code

pseudocode · 22 lines
pseudocode
// Illustrative: re-embed only chunks whose embedding input changed
new_chunks = chunk(document.text)
stored     = load_records_grouped_by_fingerprint(document.id)  // fingerprint -> list of records
plan       = []
to_embed   = []

for position, c in new_chunks:
    input = embedding_input(c, document)          // exactly what the model will see
    fp    = hash(model_version, embed_params, input)
    if stored[fp] is not empty:
        old = stored[fp].remove_one()             // pairs duplicates one by one
        plan.add(reuse(old.vector, fp, position))
    else:
        to_embed.add(position, fp, input)

vectors = embed_in_batches(to_embed.inputs)
for each (item, v) paired from to_embed and vectors:
    plan.add(new_record(v, item.fp, item.position))

superseded = every record still left in stored
apply_as_one_unit(document.id, plan, superseded)  // replacement mechanics live elsewhere
emit(re_embed_fraction = count(to_embed) / max(1, count(new_chunks)))

go deeper

for a junior

Recall the three outcomes of the diff, reuse, embed and superseded, and that the comparison uses a hash of each chunk rather than its ID.

for a middle

Explain why the fingerprint covers the full embedding input and the model version, and why matching must count duplicates instead of using a plain set.

for a senior

Design the diff so reuse is keyed on content while records keep their own IDs, hand the superseded set to an atomic replacement, and alert on the re-embed fraction's three failure signatures.

for a principal

Decide what is allowed into the embedding input as a team rule, since every field there turns its changes into re-embedding cost across the corpus.

## What a chunk-set diff decides When a document changes, the ingestion pipeline re-chunks the new version and must decide, for every chunk, whether to call the embedding model. A **chunk-set diff** compares the new chunk list with the chunks already stored for that document and sorts everything into three groups: - **reuse**: a new chunk whose embedding input matches a stored chunk's; its vector is copied, perhaps under a new position or ID; - **embed**: a new chunk with no match; it goes to the embedding model; - **superseded**: a stored chunk nothing matched; it is handed to the step that replaces the document's chunk set and retires old records, which is a subject of its own. The diff only pays off when boundaries are stable. On fixed-size windows an early insert changes every later chunk's text, so the diff correctly reports nearly everything as new. ## What the fingerprint must cover The comparison key is a **fingerprint**, a hash such as SHA-256, over everything that determines the vector: - the exact **embedding input**, not just the chunk text: any title, heading path or summary prefix folded in, and any instruction text prepended for the model; - the **embedding model version**, so vectors from a retired model never match; - **embedding parameters** that change the output, such as a requested vector dimension. Leave out anything that does not reach the model. Position, owner and access lists belong in the record's metadata; folding them into the fingerprint makes every metadata change look like a content change. | change | fingerprint changes? | action | |---|---|---| | chunk moved, text identical | no | reuse vector, rewrite position | | typo fixed in the chunk | yes | embed | | title prefix changed | yes, for every chunk carrying it | embed | | owner changed, not in the input | no | update metadata only | | embedding model switched | yes, for every chunk | whole-corpus re-embed, a separate job | ## Matching, step by step 1. Load the stored records for the document, grouped so each fingerprint maps to a list of records. 2. Walk the new chunks in order; for each, take one record off its fingerprint's list if any is left. Taking one at a time matters: a document with the same warning paragraph three times has three records under one fingerprint, and a plain set of fingerprints cannot tell one copy from three. 3. Send every unmatched new chunk to the embedder in batches. 4. Treat every record still left in the lists as superseded. 5. Hand the plan (reused, new, superseded) to the replacement step as one unit, so a query never sees half of it. A lookup is enough for deciding what to embed, because a vector depends only on its own input, not on its neighbours. A sequence alignment, like a line diff, is worth adding only when you also want lineage, such as carrying a record ID or attached feedback from an old chunk to the edited one that replaced it. ## Keep vector reuse apart from record identity A diff keyed on chunk IDs fails under positional IDs: after an insert, ID 12 holds different text from before, so an ID-keyed check re-embeds it even though that text already has a vector under ID 11. Key reuse on the fingerprint, and let the record ID be whatever scheme was chosen for identity. Copying a vector into a different record costs a store write, not an embedding call. ## Measuring that it works Emit, per document version, the counts of reused, embedded and superseded chunks, and the **re-embed fraction**: embedded divided by chunks in the new version. Three signals are worth an alert: - small edits to long documents should show fractions of a few percent; a steady climb means boundaries have started moving again; - a fraction near 1.0 on a document whose text barely changed means something volatile reached the embedding input, a timestamp or a regenerated summary; - edits processed after a model switch that still reuse old vectors mean the model version is missing from the fingerprint, and old-model vectors are mixing with new ones. In a forty-page design document of 40 chunks, one sentence inserted near the top should produce one or two embedded chunks with paragraph-anchored chunking, a fraction of a few percent; the same edit on fixed windows produces a fraction near 95%, and the diff is right both times.

  • Why include the embedding model version in the fingerprint when the text is unchanged?
    Because a vector is only reusable with the model that produced it. Without the version, a pipeline that switched models would match old fingerprints and keep old-model vectors beside new ones, and nothing would flag the mix. With it, a model change misses every fingerprint, which is correct; running that whole-corpus re-embed is a separate job.
  • Should the diff be a sequence alignment, like a text diff, or a fingerprint lookup?
    For deciding what to embed, a lookup is enough: a vector depends on the chunk's own input, not its neighbours, so order does not matter. A sequence alignment helps only when you also want lineage, which old chunk an edited one replaced, to carry IDs or attached data forward; it costs more and is optional.
  • A metadata field such as the page owner changed but no text did. What should the diff do?
    If the field is not part of the embedding input, the fingerprint matches, so update the stored record's metadata and embed nothing; the vector is still right. If the field is folded into the input, the fingerprint changes and re-embedding is correct, which is the argument for keeping volatile fields out of the input.

  • Comparing raw chunk text is enough even when a prefix is embedded with it.
  • If the document hash changed, every chunk must be re-embedded.
  • A plain set of fingerprints handles repeated passages correctly.
  • The model version does not matter when the chunk text is identical.
  • Unmatched stored chunks can simply be left where they are.