Implementation:Huggingface Datasets Sequence
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, NLP |
| Last Updated | 2026-02-14 18:00 GMT |
Overview
Concrete tool for representing variable or fixed-length sequences of feature types provided by the HuggingFace Datasets library.
Description
Sequence is a utility class that creates list-typed features in dataset schemas. It wraps a child feature type and an optional fixed length. When the child feature is a dictionary of sub-features, Sequence automatically converts it into a dictionary of List features (transposing from list-of-dicts to dict-of-lists) for TFDS compatibility. When the child feature is any other type, it creates a standard List feature. The underlying Arrow type uses 32-bit offsets or fixed-size lists depending on the length parameter.
Usage
Use Sequence in Features schemas to define columns that contain lists of values. It is the standard way to represent token sequences, embedding vectors, multi-label fields, and repeated structures.
Code Reference
Source Location
- Repository: datasets
- File:
src/datasets/features/features.py - Lines: 1179-1209
Signature
class Sequence:
def __new__(cls, feature=None, length=-1, **kwargs):
...
Import
from datasets import Sequence
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| feature | FeatureType |
Yes | Child feature data type for each item in the sequence. |
| length | int |
No | Fixed length of the sequence. Defaults to -1 (variable length). |
Outputs
| Name | Type | Description |
|---|---|---|
| instance | List or dict[str, List] |
A List feature (for simple types) or a dict of List features (for dict child types). |
Usage Examples
Basic Usage
from datasets import Features, Sequence, Value, ClassLabel
# Variable-length integer sequence
features = Features({
"token_ids": Sequence(Value("int32")),
"labels": Sequence(ClassLabel(names=["O", "B-PER", "I-PER"])),
})
print(features)
# {'token_ids': Sequence(feature=Value(dtype='int32', id=None), length=-1, id=None),
# 'labels': Sequence(feature=ClassLabel(num_classes=3, names=['O', 'B-PER', 'I-PER'], id=None), length=-1, id=None)}