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

Lazy Loading & Generators

Processing millions of docs without crashing RAM.

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 have 1 million documents. loader.load() tries to pull all 1 million into a Python list in RAM. Your server crashes. OOM KilleD.

LangChain loaders expose .lazy_load(). This returns a Generator, not a List. It reads one file, yields it, and forgets it.

lazy_loading.py
loader = DirectoryLoader("./huge_dataset")

# Bad (Eager)
# docs = loader.load() # Consumes 10GB RAM

# Good (Lazy)
for doc in loader.lazy_load():
  # Process one doc at a time
  vector_store.add_documents([doc])
  # RAM stays constant

In production pipelines, identifying "Lazy" operations is critical. Your ingestion script should be a pipeline of generators: Generator(Disk) -> Generator(Splitter) -> Generator(Embedder) -> Vector Store.

Don't buy more RAM. Write better pipelines. Lazy loading is the difference between a toy script and a production ETL job.

Key Intuition: "If it fits in RAM, it's not Big Data."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonDeduplication via Content HashingNext lesson →Embeddings: What Matters in Practice