Skip to content
derpx06Notes on systems, models & learning
3. Data Ingestion & Preparation · lesson 30 of 68 · 1 min · January 10, 2026

Lazy Loading & Generators

Processing millions of docs without crashing RAM.

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."