Learn/LangChain Mastery/Part 4 · 3. Data Ingestion & Preparation
Lesson 29 of 68
SyllabusLangChain Mastery · Part 4

Deduplication via Content Hashing

Why duplicates kill quality and cost. How to fix it with MD5.

Course outline & contents
LangChain Mastery
The Document Object & MetadataLoaders: PDF, HTML, Text, WebCleaning & NormalizationChunking: Why It ExistsRecursiveCharacterTextSplitterToken-Aware & Syntax-Aware SplittingMarkdown & Code SplittingMetadata Enrichment StrategiesDeduplication via Content HashingLazy Loading & Generators

If you ingest a dataset twice, most naive RAG pipelines index it twice. Vector stores don't care. They will happily store 2 identical vectors.

The Cost:

  1. Storage $$: You pay double.
  2. Retrieval Logic: When you search, the top 5 results are just the same document 5 times. You lose diversity.

Before embedding, hash the content string. Use the Hash as the ID.

hashing.py
import hashlib

def generate_id(content):
  return hashlib.md5(content.encode()).hexdigest()

doc.id = generate_id(doc.page_content)

# Upsert uses ID. If ID exists, it overwrites (doesn't duplicate).
vector_store.add_documents([doc])

LangChain has a RecordManager to handle this automatically for large datasets. It tracks which docs exist and handles updates/deletions.

An indexed document must be unique. If you don't deduplicate, your top-k results will be an echo chamber.

Key Intuition: "Idempotency prevents insanity."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonMetadata Enrichment StrategiesNext lesson →Lazy Loading & Generators