Skip to content

fix(rag-worker): use deterministic embedding point ids to avoid orphaned duplicate vectors#622

Open
serhiizghama wants to merge 2 commits into
rowboatlabs:mainfrom
serhiizghama:fix/rag-worker-deterministic-embedding-ids
Open

fix(rag-worker): use deterministic embedding point ids to avoid orphaned duplicate vectors#622
serhiizghama wants to merge 2 commits into
rowboatlabs:mainfrom
serhiizghama:fix/rag-worker-deterministic-embedding-ids

Conversation

@serhiizghama

Copy link
Copy Markdown

Problem

Closes #603.

The RAG worker writes embeddings to Qdrant and then updates the document record in MongoDB as two separate network calls. Point IDs are generated with crypto.randomUUID() on every run:

const points = embeddings.map((embedding, i) => ({
    id: crypto.randomUUID(),
    // ...
}));
await qdrantClient.upsert("embeddings", { points });
await dataSourceDocsRepository.updateByVersion(/* ... */);

If the Qdrant upsert succeeds but a later step fails (MongoDB timeout, connection drop, version conflict), the worker throws and the document becomes eligible for retry. On retry, fresh random UUIDs are generated, so a new set of points is inserted while the originals remain in the collection — the app no longer holds any reference to them. Over time this accumulates orphaned duplicate vectors that:

  • return duplicate chunks during vector search,
  • waste context space and degrade retrieval quality,
  • grow unbounded with no tracking.

The same pattern is present in all three pipelines: runProcessFilePipeline, runScrapePipeline, and runProcessTextPipeline.

Solution

Derive each point ID deterministically from the document id and chunk index instead of generating a random UUID per run:

id: embeddingPointId(doc.id, i),

embeddingPointId() produces an RFC 4122 v5 (name-based) UUID from ${docId}-chunk-${chunkIndex} under a fixed namespace. Because the same chunk always maps to the same id, a retry upserts over the existing points rather than inserting duplicates — the operation becomes idempotent. The return value is a valid UUID, which EmbeddingRecord.id (z.string().uuid()) requires.

The helper is dependency-free (uses the already-imported Node crypto module), so no new packages are added. Deletion (runDeletionPipeline) filters by the docId payload field rather than by point id, so it is unaffected.

Testing

The repo has no test harness, so I verified the helper's behavior in isolation:

  • DeterministicembeddingPointId("doc123", 0) returns the same id across calls.
  • Unique per chunk / per doc — different chunk index or doc id yields a different id.
  • Valid v5 UUID — output matches ^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$, satisfying EmbeddingRecord.

Re-running any pipeline for the same document now overwrites the existing points in Qdrant instead of adding a duplicate set.

Derive Qdrant point IDs from the document id and chunk index via an
RFC 4122 v5 UUID instead of a random UUID per run. This is a pure
addition; call sites are updated in the next commit.
…cates

The file, scrape and text RAG pipelines generated a fresh random UUID for
every Qdrant point on each run. When a job is retried after the Qdrant
upsert succeeded but a later step (e.g. the MongoDB status update) failed,
the retry wrote a brand-new set of points while the originals stayed in the
collection, leaving orphaned duplicate vectors that degrade retrieval and
are no longer tracked by the app.

Derive each point id from the document id and chunk index so retries upsert
over the same points and stay idempotent.

Fixes rowboatlabs#603
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Orphaned vectors piling up in Qdrant every time the RAG worker retries a failed doc

1 participant