﻿# WebDataset sample keys: keeping image, text and metadata together

Understand WebDataset basename grouping, tar member order, duplicate suffixes and shard boundaries when packaging multimodal examples.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/data-guide-webdataset-sample-key-shards/
Language: en

## The sample spans several archive members

WebDataset uses tar archives with conventions for grouping files into examples. Components belonging together share a basename after extensions are removed. An image, a caption and a metadata document can therefore become fields of one logical sample.

This is different from treating every archive member as an independent training example. An image-caption task needs their relationship preserved. A dataset that contains all required bytes can still be wrong if the packaging process pairs a caption with a different image.

## Name keys independently of display filenames

The maintained writer uses a sample __key__ and field names to construct archive member names. Choose a key scheme that preserves the source identity needed by the experiment. Reusing a display filename from unrelated source directories can create collisions in the derived collection.

Before a large export, inspect the intended key and component set for representative records. A key that is unique only within one input folder may cease to be unique after several folders are combined. Retain a mapping back to the original artifact identifiers.

## Grouping depends on the stream order

The reader implementation groups consecutive components by key and emits a sample when that key changes. It also checks for duplicate component suffixes within a sample. Consequently, merely placing matching names somewhere in the same tar archive is insufficient for the streaming grouping contract.

Keep a sample's members together when writing. A generic archive sort or later repackaging step can change their order. Validate using the actual reader after such a transformation rather than checking only that all filenames remain present in the archive listing.

## Shard boundaries are example boundaries too

The reader marks the end of a shard so grouping does not mix samples from different shards. Keep each logical sample complete within the intended archive. Splitting an image and its caption across archives should not be treated as an implicit cross-shard join.

Count reconstructed samples and missing fields in a trial read. Byte totals and archive-member totals answer different questions from complete-example counts. A decoder failure is also distinct from a missing component: both can remove usable examples but need different repairs.

## Verify the packaged learning unit

A useful export check compares source keys, component types and decoded sample relationships. For an image-caption collection, inspect actual pairs as well as the counts. If error handling skips records, preserve the rejected keys so the effective training population is visible.

WebDataset provides a practical streaming representation. It does not decide which caption is accurate or how training and evaluation identities should be separated. Packaging checks protect the intended sample structure; annotation quality and split design remain additional tasks.

## Primary references

[WebDataset format overview](https://github.com/webdataset/webdataset).

[WebDataset writer implementation](https://raw.githubusercontent.com/webdataset/webdataset/main/src/webdataset/writer.py).

[WebDataset grouping implementation](https://raw.githubusercontent.com/webdataset/webdataset/main/src/webdataset/tariterators.py).

Related explanations: [Paired image and text learning](/journal/clip-image-text-contrastive-space/); [Provenance of derived samples](/journal/document-markdown-json-provenance/).
