Install & Compatibility
Where this runs
tested against v1.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
webdataset
✓ import webdataset as wds
WebDataset
✓ dataset = wds.WebDataset(url)
✗ from webdataset import Dataset
While `Dataset` might work in older versions or internal contexts, the canonical and recommended approach is to import `webdataset as wds` and use `wds.WebDataset`.
This quickstart demonstrates how to create a `webdataset` pipeline to load data from a remote TAR file, apply shuffling and decoding, extract specific components like images and JSON metadata, and then preprocess and batch the samples. It shows the typical 'fluid' interface with chained method calls and how it integrates with PyTorch-style data iteration. It fetches from a publicly available OpenImages shard, decodes using PIL, and extracts components into tuples.
import webdataset as wds
import torch
import os
from itertools import islice
# Example URL to a public WebDataset shard. In a real scenario, this would be your dataset path(s).
# For local files: url = "file:./my_dataset-{0000..0009}.tar"
# For cloud storage: url = "pipe:gsutil cat gs://my-bucket/dataset-{0000..0009}.tar"
url = "http://storage.googleapis.com/nvdata-openimages/openimages-train-000000.tar"
# Define a simple preprocessing function (e.g., for images and labels)
def preprocess(sample):
# Assuming 'jpg' for image and 'json' for metadata (e.g., labels)
image = sample['jpg']
metadata = sample.get('json')
# Example: convert image to PyTorch tensor and extract a dummy label
# In a real scenario, you'd decode and transform the image bytes properly
# For this example, we'll just return a placeholder tensor and label
# (webdataset.decode() would handle actual image decoding)
# If actual image decoding is not done yet, 'image' might be bytes.
# For a quickstart without full image processing libs, mock it:
if isinstance(image, bytes):
# Mock a tensor, in a real app, use PIL/torchvision transforms
processed_image = torch.randn(3, 224, 224) # e.g., C, H, W
else:
processed_image = image # If decode() was used earlier
# Extract a dummy label from metadata, or just use a placeholder
label = 0 # Placeholder label
if metadata and isinstance(metadata, dict) and 'annotations' in metadata:
try:
label = metadata['annotations'][0]['category_id']
except (IndexError, KeyError):
pass
return processed_image, label
# Create a WebDataset pipeline
dataset = (
wds.WebDataset(url) # Load from URL
.shuffle(100) # Shuffle samples within a buffer
.decode("pil") # Decode images using PIL (requires Pillow installed)
.to_tuple("jpg", "json") # Extract 'jpg' and 'json' components as a tuple
.map(preprocess) # Apply custom preprocessing
.batched(16) # Batch samples
)
# Use with PyTorch DataLoader (optional, for parallel loading and iteration)
# If you don't use PyTorch, you can iterate directly over 'dataset'
# from torch.utils.data import DataLoader
# dataloader = DataLoader(dataset, num_workers=4, batch_size=None) # batch_size=None if .batched() is used above
print(f"Accessing the first 2 batches from: {url}")
# Iterate over a few batches
for i, (images, labels) in enumerate(islice(dataset, 2)):
print(f"Batch {i+1}:")
print(f" Images shape: {images.shape}")
print(f" Labels: {labels}")
if i == 1:
break
print("Quickstart complete.")
Debug
Known issues
gotchaWebDataset implements PyTorch's `IterableDataset` and thus does not provide a `__len__` method by default. Code expecting `len(dataset)` will raise a `TypeError`. To provide a length, you must explicitly add `with_length(N)` to your pipeline. This also impacts deterministic epoch balancing in distributed training.fixAppend `.with_length(N)` to your dataset pipeline if you require a length. For distributed training, consider `wds.resampled()` for approximate balancing or careful shard management.
affects: All versions
breakingDirect string arguments like `decode('PIL')` or `decode('numpy')` for decoding images were deprecated in favor of using actual functions (e.g., `decode(wds.decode('pil'))` or `decode('rgb')`, `decode('torchrgb')`). This change improves clarity and flexibility.fixReplace deprecated string decoders with functional calls or the appropriate shorthand string, e.g., `dataset.decode('rgb')` or `dataset.decode(wds.decode_pil)`. Consult the latest documentation for preferred decoder functions. affects: Versions released after September 27, 2024 (e.g., 1.0.0 and above likely affected)
gotchaUsing the `pipe:` protocol with untrusted or unescaped URLs can lead to shell injection vulnerabilities, as `webdataset` executes shell commands.fixEnable secure mode by setting `webdataset.utils.enforce_security = True` in your code or by setting the environment variable `WDS_SECURE=1`. Avoid using `pipe:` with untrusted inputs.
affects: All versions
gotchaWebDataset relies heavily on external command-line tools like `curl`, `gsutil`, `aws`, and `file` for core I/O and type detection. This can affect portability across different operating systems or environments where these tools are not available or behave differently, and complicates error handling.fixEnsure all necessary command-line tools are installed and configured correctly in your environment. For cloud storage, consider using the `objectio` library if installed, as WebDataset passes URLs to it for direct access (without `pipe:`). Users can also implement custom `gopen_schemes`.
affects: All versions
gotchaAchieving precisely balanced epochs and avoiding sample repetition in multi-worker or distributed training setups (especially with `resampled=True` and shuffling) can be complex. Older usage of `repeat` argument might be outdated. Workers can endlessly repeat their shard shares without proper configuration.fixFor distributed training, use `wds.split_by_node` and `wds.split_by_worker` in your pipeline. If using `resampled=True`, ensure appropriate logic to handle epoch boundaries. Be cautious with the `repeat()` method and consider its interaction with `with_epoch()` if you need fixed epoch sizes. The `wds.DataPipeline` can explicitly manage these stages.
affects: All versions, especially with distributed training or `num_workers > 1`
gotchaLong delays before the first batch, or inconsistent batch completion times, can occur due to large batch sizes, large shuffle buffers requiring time to fill, or slow underlying disk/storage access. This is often a configuration issue rather than a `webdataset` bug.fixProfile your data pipeline to identify bottlenecks. Reduce shuffle buffer size for initial debugging, ensure efficient network/disk I/O, and optimize image decoding/preprocessing steps. Monitor `curl` performance if using remote URLs.
affects: All versions
Errors
Common errors & fixes
BrokenPipeError: [Errno 32] Broken pipe
Occurs when a PyTorch DataLoader worker process disconnects prematurely or unexpectedly, often due to exceeding file descriptor limits, insufficient shared memory, or an error within the worker's data processing.
fixDebug by setting `num_workers=0` in `DataLoader`. For `num_workers > 0`, ensure proper resource limits (`ulimit -n`), increase shared memory, and consider `torch.multiprocessing.set_start_method('spawn', force=True)` at the beginning of your script. webdataset.TarIO.InvalidTarFile: Bad Tarfile.
The specified `.tar` archive is corrupted, empty, not a valid tar file, or truncated, preventing `webdataset` from correctly reading its structure or contents.
fixVerify the integrity and format of your `.tar` files. Ensure they are not empty, properly closed, and contain valid tar archives. Re-create them if necessary.
PIL.UnidentifiedImageError: cannot identify image file
The image data within a `.tar` file is corrupted, has an unsupported format, or is not a valid image that the Pillow library (or other decoding library) can interpret during the decoding step.
fixEnsure original image files are valid and uncorrupted before archiving. Verify that the `webdataset` decoding pipeline (e.g., `wds.decode('pil')` or `wds.decode('rgb')`) matches the image format and that `Pillow` or other necessary libraries are installed. Upgrade
Version history
1.0.2latest on PyPI · released Jun 19, 2025
Audit
Dependencies
pytorchrequiredCore dependency for `IterableDataset` implementation and `DataLoader` compatibility.
numpyrequiredCore dependency for numerical operations.
braceexpandrequiredUsed for expanding brace-enclosed sequences in URLs (e.g., dataset-{000000..012345}.tar).
PillowoptionalDynamically loaded for image decoding (PIL/Pillow).
torchvisionoptionalDynamically loaded for image/video/audio decoding and transformations.
msgpackoptionalDynamically loaded for MessagePack decoding.
curloptionalCommand-line tool used internally for accessing HTTP/HTTPS servers.
gsutiloptionalCommand-line tool used internally for accessing Google Cloud Storage buckets.
aws clioptionalCommand-line tool used internally for accessing Amazon S3 buckets.
azure clioptionalCommand-line tool used internally for accessing Azure storage buckets.