Intermediate

Streaming Datasets

Process terabyte-scale datasets without downloading them first. Streaming mode yields examples on-the-fly, using minimal memory regardless of dataset size.

Why Streaming?

Some datasets are too large to download and store locally. The Common Crawl corpus is over 100TB, The Pile is 800GB, and even LAION-5B image-text pairs require petabytes of storage. Streaming lets you process these datasets without downloading a single file.

Basic Streaming

Python
from datasets import load_dataset

# Enable streaming with streaming=True
dataset = load_dataset("c4", "en", split="train", streaming=True)

# This returns an IterableDataset (no download!)
print(type(dataset))  # <class 'datasets.IterableDataset'>

# Iterate through examples one at a time
for i, example in enumerate(dataset):
    print(example["text"][:100])
    if i >= 4:
        break

Streaming Operations

Python
# Map (applied lazily during iteration)
tokenized = dataset.map(tokenize_fn)

# Filter
english = dataset.filter(lambda x: x["language"] == "en")

# Take first N examples
small = dataset.take(1000)

# Skip first N examples
rest = dataset.skip(1000)

# Shuffle with a buffer
shuffled = dataset.shuffle(seed=42, buffer_size=10000)

# Chain operations (all lazy)
processed = (
    dataset
    .filter(lambda x: len(x["text"]) > 100)
    .map(tokenize_fn)
    .shuffle(buffer_size=5000)
)

Interleaving Datasets

Python
from datasets import interleave_datasets

# Mix multiple streaming datasets
en = load_dataset("mc4", "en", split="train", streaming=True)
fr = load_dataset("mc4", "fr", split="train", streaming=True)
de = load_dataset("mc4", "de", split="train", streaming=True)

# Interleave with custom probabilities
mixed = interleave_datasets(
    [en, fr, de],
    probabilities=[0.5, 0.3, 0.2],
    seed=42
)

Streaming vs Regular Loading

AspectRegular (Dataset)Streaming (IterableDataset)
MemoryLoads data into memory/diskConstant memory, streams from source
Random accessYes (dataset[i])No (sequential only)
len()YesNo (unknown size)
ShufflingFull shuffleBuffer-based approximate shuffle
SpeedFaster per-example (cached)Network-bound (download on the fly)
Best forDatasets that fit on diskVery large datasets, quick exploration
Buffer Shuffle: Streaming datasets shuffle using a buffer. A buffer_size of 10,000 means 10,000 examples are loaded and randomly sampled from. Larger buffers give better randomness but use more memory.

Next: Creating Datasets

Learn how to build your own datasets from scratch and share them with the community on the Hugging Face Hub.

Next: Creating Datasets →

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.