← All notes

Tokenize a text dataset with Hugging Face Datasets

Short answer

from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("bert-base-uncased")
ds = ds.map(lambda b: tok(b["text"], truncation=True), batched=True)

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Hugging Face (datasets docs), Preprocess · Hugging Face, Apache-2.0 (huggingface/datasets docs); Hugging Face (datasets docs), Process text data · Hugging Face, Apache-2.0 (huggingface/datasets docs).

Cite this

Permanent ID: iil:note:e4x7ffmb

iinventedlight. 2026. "Tokenize a text dataset with Hugging Face Datasets." iinventedlight Library, iil:note:e4x7ffmb, version of 2026-11-04. https://iinventedlight.com/library/notes/hf-datasets-preprocess-text/. License: CC-BY-4.0.

@misc{iil_note_e4x7ffmb,
  author = {{iinventedlight}},
  title = {{Tokenize a text dataset with Hugging Face Datasets}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-04},
  url = {https://iinventedlight.com/library/notes/hf-datasets-preprocess-text/},
  note = {Permanent ID iil:note:e4x7ffmb. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/hf-datasets-preprocess-text.md.