Tokenizers documentation

Node

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Node

The Node binding is encode-only. It loads a tokenizer.json and turns text into token ids; it does not decode, and it does not build, edit, save or train tokenizers.

Installation

npm install tokenizers

Loading a tokenizer

const { PipelineTokenizer } = require('tokenizers')

const tokenizer = PipelineTokenizer.fromFile('tokenizer.json')

The file is put through the legacy 1.0 → canonical 2.0 upgrade on the way in, so tokenizers already on disk keep loading.

Encoding

encode returns a Uint32Array of ids rather than an object: a JS Array costs one napi value per token, which on token-dense input is 13x the encode itself.

const ids = tokenizer.encode("Hello, y'all! How are you 😁 ?")

For a tight loop, encodeBytesInto writes into a Uint32Array you own. That drops the two remaining per-call costs — the JS string → UTF-8 copy, and a fresh ArrayBuffer every call — and returns how many ids it wrote:

const out = new Uint32Array(512)
const n = tokenizer.encodeBytesInto(Buffer.from("Hello, y'all!"), out)
// out.subarray(0, n) holds the ids

Options

Both methods take an optional second argument. A field left out keeps the tokenizer’s own behaviour.

const ids = tokenizer.encode("Hello, y'all!", {
  addSpecialTokens: true,
  encodeSpecialTokens: false,
  padding: { padId: 0, padToken: '[PAD]', length: 16 },
  truncation: { maxLength: 512 },
})
OptionDefaultDescription
addSpecialTokenstrueWhether the post-processor adds its special tokens, such as [CLS] and [SEP].
encodeSpecialTokensfalseWhether a special token written in the text goes through the model (true) or becomes its added-vocabulary id.
paddingconfiguredPadding for this call, replacing the tokenizer’s configured padding. false disables it.
truncationconfiguredTruncation for this call, replacing the tokenizer’s configured truncation. false disables it.

padding

Replaces the configured padding as a whole, so a field left out takes the default below, not the configured value.

FieldDefaultDescription
directionrightWhether pad tokens are appended right or prepended left.
padId0The id of the padding token.
padTypeId0The type id of the padding token.
padToken[PAD]The text of the padding token.
lengthbatch longestPads every encoding to exactly this many tokens.
padToMultipleOfRounds the padded length up to a multiple of this.

truncation

FieldDefaultDescription
maxLengthrequiredThe maximum number of tokens, including special tokens, to keep.
strategylongest_firstWhich sequence of a pair is truncated: longest_first, only_first or only_second.
directionrightWhether to truncate at the end of the sequence or at its beginning.
Update on GitHub