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
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
# 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
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
| Aspect | Regular (Dataset) | Streaming (IterableDataset) |
|---|---|---|
| Memory | Loads data into memory/disk | Constant memory, streams from source |
| Random access | Yes (dataset[i]) | No (sequential only) |
| len() | Yes | No (unknown size) |
| Shuffling | Full shuffle | Buffer-based approximate shuffle |
| Speed | Faster per-example (cached) | Network-bound (download on the fly) |
| Best for | Datasets that fit on disk | Very large datasets, quick exploration |
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.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX