---
slug: ./Tokenizer
title: "ClikaRT::tokenizer::Tokenizer"
sidebar_label: "Tokenizer"
description: "A loaded tokenizer. Move-only. Every fallible operation returns its value directly and raises ClikaRT::Error on failure."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--class">class</span>

**Header:** `ClikaRT/tokenizer/tokenizer.h`

A loaded tokenizer. Move-only.

Every fallible operation returns its value directly and raises [`ClikaRT::Error`](../Error.md) on failure. Wrap a call in [`CLIKART_TRY(...)`](../../index.md#CLIKART_TRY) if you would rather inspect a [`Result`](../Result.md) than catch.

## Static member functions

### from\_huggingface() {#from_huggingface}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_huggingface`**(`std::string_view` `path`)

</div>

Load from a HuggingFace tokenizer: `path` is a model directory OR a single artifact file (`tokenizer.json` / `tokenizer.model` / `sentencepiece.bpe.model` / `tekken.json` / `vocab.json`+`merges.txt`). Auto-detects the artifact and overlays a sibling `tokenizer_config.json` (bos/eos ids + chat template) when present.

A bare SentencePiece artifact (`tokenizer.model` / `sentencepiece.bpe.model`) beside a `tokenizer_config.json` naming its `tokenizer_class` is laid out the way that class's tokenizer would be: XLM-RoBERTa / LayoutXLM, CamemBERT, BARThez, mBART and XGLM get their reserved head ids (`<s>` 0, `<pad>` 1, `</s>` 2, `<unk>` 3), their language codes, `<mask>`, and their `<s> … </s>` framing (mBART frames with the config's `src_lang`, or the class default); the Llama family (Llama, CodeLlama, Phi-3) gets `<s>` / `</s>` framing per `add_bos_token` / `add_eos_token`; any other class, or no config, loads the pieces with their own ids and no framing. For a model no class claims, a sibling `vocab.json` (with no `merges.txt`) numbers the pieces instead: each takes that dictionary's id, a piece it lacks its `<unk>`, and the special tokens `special_tokens_map.json` names that the dictionary lacks (an M2M100 checkpoint's `__en__` language tokens) follow it in order; nothing frames the sequence.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 213

</div>

### from\_sentencepiece() {#from_sentencepiece}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_sentencepiece`**(`std::string_view` `path`)

</div>

Load a SentencePiece tokenizer model (a `tokenizer.model` file). Bare: the pieces keep their own ids, nothing frames the sequence, and there is no `tokenizer_config.json` overlay (bos/eos default to -1, no chat template); use [`from_huggingface`](./Tokenizer.md#from_huggingface) on the model directory for the class layout and those.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 220

</div>

### from\_tekken() {#from_tekken}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_tekken`**(`std::string_view` `path`)

</div>

Load a Mistral `tekken.json` tokenizer (tiktoken-based; carries its own special tokens). Bare, as above.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 225

</div>

### from\_gpt2() {#from_gpt2}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_gpt2`**(`std::string_view` `dir`)

</div>

Load a GPT-2-style byte-level BPE tokenizer from a directory containing `vocab.json` + `merges.txt`. Bare, as above.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 230

</div>

### from\_tiktoken() {#from_tiktoken}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_tiktoken`**(`std::string_view` `path`)

</div>

Load an OpenAI `.tiktoken` ranks file (one `base64(token_bytes) rank` line per token, e.g. whisper's `multilingual.tiktoken` / `gpt2.tiktoken`). The file carries no special tokens, so the loader synthesizes the OpenAI special-token set of the family its ranks-table size names:

- 50257 ranks, whisper MULTILINGUAL: the current 100-language set (what every large-v3-era checkpoint uses). A pre-large-v3 multilingual checkpoint uses a 99-language id layout the file alone cannot express (ids from `<|translate|>` on shift by one); assemble those with [`TokenizerBuilder`](./TokenizerBuilder.md).
- 50256 ranks, whisper ENGLISH-ONLY (the GPT-2 table): the 99-language set. For a plain GPT-2 tokenizer WITHOUT whisper specials, use [`from_gpt2`](./Tokenizer.md#from_gpt2) or [`TokenizerBuilder`](./TokenizerBuilder.md) instead. Any other table size raises [`ClikaRT::Error`](../Error.md) naming the size and the known families. Bare, as above. (See also [`from_tekken`](./Tokenizer.md#from_tekken), the other tiktoken-based format, whose JSON carries its own specials.)

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 248

</div>

### from\_bert\_vocab() {#from_bert_vocab}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_bert_vocab`**(`std::string_view` `dir`)

</div>

Load a BERT WordPiece tokenizer from a directory containing a bare `vocab.txt` (one token per line; id = line number), folding in the sibling `tokenizer_config.json` / `special_tokens_map.json` when present (the special-tokens map wins where both name a special). Bare, as above.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 256

</div>

### from\_file() {#from_file}

<div class="api-signature">

`static` [`Tokenizer`](./Tokenizer.md) **`from_file`**(`std::string_view` `path`)

</div>

Load from any supported tokenizer artifact, detecting the format for you; use this when you hold "the tokenizer file that shipped with the checkpoint" without knowing which format it is. A DIRECTORY probes, in order: `tokenizer.json` → `tokenizer.model` → `tekken.json` → `vocab.json` + `merges.txt` → `*.tiktoken` (lexicographically first), the same preference [`from_huggingface`](./Tokenizer.md#from_huggingface) uses, extended by `.tiktoken`. A FILE resolves by its canonical name/extension first; a nonstandard name is classified by content: JSON with a `model` object → `tokenizer.json`; JSON with a `vocab` array of `token_bytes` entries → `tekken.json`; `base64 rank` lines → `.tiktoken`; a SentencePiece model by its protobuf shape. A flat token→id JSON map is GPT-2's `vocab.json`, loadable only as the canonical `vocab.json` + `merges.txt` pair (pass the directory or the `vocab.json` itself). Each detected format then loads exactly as its named loader would: `tokenizer.json` and a SentencePiece model through [`from_huggingface`](./Tokenizer.md#from_huggingface) (including the `tokenizer_config.json` overlay; for a SentencePiece model that is the layout its declared `tokenizer_class` selects; with no config it loads as [`from_sentencepiece`](./Tokenizer.md#from_sentencepiece) loads it), the rest through their bare loaders above. An unrecognized path raises [`ClikaRT::Error`](../Error.md) naming every format tried.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 278

</div>

## Member functions

### Tokenizer() {#Tokenizer}

<div class="api-signature">

**`Tokenizer`**(`Tokenizer&&`) `noexcept`

</div>

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 280

</div>

### operator=() {#operator-assign}

<div class="api-signature">

[`Tokenizer`](./Tokenizer.md)`&` **`operator=`**([`Tokenizer`](./Tokenizer.md)`&&`) `noexcept`

</div>

Move-assign: transfers the tokenizer.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 282

</div>

### \~Tokenizer() {#Tokenizer-2}

<div class="api-signature">

**`~Tokenizer`**()

</div>

Releases the tokenizer (outstanding [`StreamingDecoder`](./StreamingDecoder.md)s stay valid).

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 284

</div>

### encode() {#encode}

<div class="api-signature">

`std::vector<std::int32_t>` **`encode`**(`std::string_view` `text`, `bool` `add_special_tokens` `=` `true`) `const`

</div>

Encode `text` into token ids. `text` is read during the call (not retained).

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 288

</div>

### decode() {#decode}

<div class="api-signature">

`std::string` **`decode`**([`ClikaRT::Span`](../Span.md)`<const std::int32_t>` `ids`, `bool` `skip_special_tokens` `=` `true`) `const`

</div>

Decode token `ids` back into text. `ids` is a non-owning view read during the call; a `std::vector<std::int32_t>` binds to it directly.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 294

</div>

### tokenize() {#tokenize}

<div class="api-signature">

`std::vector<`[`Token`](./Token.md)`>` **`tokenize`**(`std::string_view` `text`, `bool` `add_special_tokens` `=` `true`) `const`

</div>

Encode `text` into per-token records: each [`Token`](./Token.md) carries its id, surface string, byte span `[begin, end)`, and whether it is a special token. Use this when you need token-to-source alignment (offsets) or the surface strings, not just the ids. `text` is read during the call, not retained.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 302

</div>

### encode\_text() {#encode_text}

<div class="api-signature">

[`Encoded`](./Encoded.md) **`encode_text`**(`std::string_view` `text`, `const` [`EncodeOptions`](./EncodeOptions.md)`&` `options` `=` `{}`) `const`

</div>

Encode one text into tensor form (a one-element batch). `ids` always carries the row; the [`Encoded`](./Encoded.md) tensor columns are filled per `options`.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 310

</div>

### encode\_batch() {#encode_batch}

<div class="api-signature">

[`Encoded`](./Encoded.md) **`encode_batch`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`ClikaRT::Span`](../Span.md)`<const std::string_view>` `texts`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`EncodeOptions`](./EncodeOptions.md)`&` `options` `=` `{}`<br />
) `const`

</div>

Encode a batch of texts into tensor form: a padded `[B, S]` (default) or a flat varlen `[ΣS]` + `cu_seqlens` layout per `options`. `texts` is read during the call, not retained.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 316

</div>

### decode\_batch(Span\< Span\< int32\_t\>\>, bool) {#decode_batch}

<div class="api-signature">

`std::vector<std::string>` **`decode_batch`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`ClikaRT::Span`](../Span.md)`<const` [`ClikaRT::Span`](../Span.md)`<const std::int32_t>>` `sequences`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `skip_special_tokens` `=` `true`<br />
) `const`

</div>

Decode many id sequences (in parallel), one string per sequence, in order.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 324

</div>

### decode\_batch(Encoded, bool) {#decode_batch-2}

<div class="api-signature">

`std::vector<std::string>` **`decode_batch`**(`const` [`Encoded`](./Encoded.md)`&` `encoded`, `bool` `skip_special_tokens` `=` `true`) `const`

</div>

Decode many id sequences (in parallel), one string per sequence, in order.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 331

</div>

### decode\_batch(Tensor, bool) {#decode_batch-3}

<div class="api-signature">

`std::vector<std::string>` **`decode_batch`**(`const` [`Tensor`](../Tensor.md)`&` `ids`, `bool` `skip_special_tokens` `=` `true`) `const`

</div>

Decode many id sequences (in parallel), one string per sequence, in order.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 339

</div>

### decode\_batch(Tensor, Tensor, bool) {#decode_batch-4}

<div class="api-signature">

`std::vector<std::string>` **`decode_batch`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`Tensor`](../Tensor.md)`&` `ids`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`Tensor`](../Tensor.md)`&` `cu_seqlens`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `skip_special_tokens` `=` `true`<br />
) `const`

</div>

Decode many id sequences (in parallel), one string per sequence, in order.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 345

</div>

### bos\_id() {#bos_id}

<div class="api-signature">

`std::int64_t` **`bos_id`**() `const noexcept`

</div>

The begin-of-sequence / end-of-sequence / padding token id named by the model's config, or -1 when the config does not name one.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 350

</div>

### eos\_id() {#eos_id}

<div class="api-signature">

`std::int64_t` **`eos_id`**() `const noexcept`

</div>

the eos id; -1 when the config names none (shared block above)

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 351

</div>

### pad\_id() {#pad_id}

<div class="api-signature">

`std::int64_t` **`pad_id`**() `const noexcept`

</div>

the pad id; -1 when the config names none (shared block above)

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 352

</div>

### vocab\_size() {#vocab_size}

<div class="api-signature">

`std::int64_t` **`vocab_size`**() `const noexcept`

</div>

Number of entries in the model vocabulary.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 357

</div>

### token\_to\_id() {#token_to_id}

<div class="api-signature">

`std::int32_t` **`token_to_id`**(`std::string_view` `token`) `const`

</div>

The id of a surface token, or NotFound if the token is not in the vocabulary. `token` is read during the call, not retained.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 362

</div>

### id\_to\_token() {#id_to_token}

<div class="api-signature">

`std::string` **`id_to_token`**(`std::int32_t` `id`) `const`

</div>

The surface string for a token id, or NotFound if `id` is out of range.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 366

</div>

### has\_chat\_template() {#has_chat_template}

<div class="api-signature">

`bool` **`has_chat_template`**() `const noexcept`

</div>

True if the loaded tokenizer carries a chat template. [`from_huggingface`](./Tokenizer.md#from_huggingface) reads it beside a `tokenizer_config.json`, from the first of three sources that holds one: that file's `chat_template` key, a sibling `chat_template.jinja`, then a sibling `chat_template.json`.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 372

</div>

### apply\_chat\_template(json::Json, bool) {#apply_chat_template}

<div class="api-signature">

`std::string` **`apply_chat_template`**(`const` [`json::Json`](../json/Json.md)`&` `messages`, `bool` `add_generation_prompt` `=` `true`) `const`

</div>

Render chat `messages` into a prompt string using the model's chat template. `messages` is a JSON array of `{"role": ..., "content": ...}` objects; `add_generation_prompt` appends the assistant-turn priming so the model continues as the assistant. A failure (no chat template, malformed messages, or a render error) raises [`ClikaRT::Error`](../Error.md). `messages` is read during the call, not retained.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 381

</div>

### apply\_chat\_template(ChatTemplateInputs) {#apply_chat_template-2}

<div class="api-signature">

`std::string` **`apply_chat_template`**(`const` [`ChatTemplateInputs`](./ChatTemplateInputs.md)`&` `inputs`) `const`

</div>

Render chat `messages` into a prompt string using the model's chat template. `messages` is a JSON array of `{"role": ..., "content": ...}` objects; `add_generation_prompt` appends the assistant-turn priming so the model continues as the assistant. A failure (no chat template, malformed messages, or a render error) raises [`ClikaRT::Error`](../Error.md). `messages` is read during the call, not retained.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 390

</div>

### apply\_chat\_template(ChatTemplateInputs, ChatTemplateOptions) {#apply_chat_template-3}

<div class="api-signature">

`std::string` **`apply_chat_template`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`ChatTemplateInputs`](./ChatTemplateInputs.md)`&` `inputs`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`ChatTemplateOptions`](./ChatTemplateOptions.md)`&` `options`<br />
) `const`

</div>

Render chat `messages` into a prompt string using the model's chat template. `messages` is a JSON array of `{"role": ..., "content": ...}` objects; `add_generation_prompt` appends the assistant-turn priming so the model continues as the assistant. A failure (no chat template, malformed messages, or a render error) raises [`ClikaRT::Error`](../Error.md). `messages` is read during the call, not retained.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 396

</div>

### encode\_chat(json::Json, bool, bool) {#encode_chat}

<div class="api-signature">

`std::vector<std::int32_t>` **`encode_chat`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`json::Json`](../json/Json.md)`&` `messages`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `add_generation_prompt` `=` `true`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `add_special_tokens` `=` `false`<br />
) `const`

</div>

Render chat `messages` with the model's chat template AND encode the result to token ids in one step, the messages→prompt→ids path. Encodes the rendered prompt with `add_special_tokens = false` by default: the template already writes its own bos/eos framing, so the tokenizer's post-processor must not add a second bos (the same law transformers' [`apply_chat_template`](./Tokenizer.md#apply_chat_template)`(`[`tokenize`](./Tokenizer.md#tokenize)`=True)` follows). Pass `true` only for a template that writes no bos itself; it additionally runs the post-processor framing, which is not what transformers does. `messages` is a JSON array of `{"role": ..., "content": ...}` objects, read during the call. A failure (no chat template, malformed messages, render error) raises [`ClikaRT::Error`](../Error.md).

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 410

</div>

### encode\_chat(ChatTemplateInputs, ChatTemplateOptions, bool) {#encode_chat-2}

<div class="api-signature">

`std::vector<std::int32_t>` **`encode_chat`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`ChatTemplateInputs`](./ChatTemplateInputs.md)`&` `inputs`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`const` [`ChatTemplateOptions`](./ChatTemplateOptions.md)`&` `options`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `add_special_tokens` `=` `false`<br />
) `const`

</div>

Render chat `messages` with the model's chat template AND encode the result to token ids in one step, the messages→prompt→ids path. Encodes the rendered prompt with `add_special_tokens = false` by default: the template already writes its own bos/eos framing, so the tokenizer's post-processor must not add a second bos (the same law transformers' [`apply_chat_template`](./Tokenizer.md#apply_chat_template)`(`[`tokenize`](./Tokenizer.md#tokenize)`=True)` follows). Pass `true` only for a template that writes no bos itself; it additionally runs the post-processor framing, which is not what transformers does. `messages` is a JSON array of `{"role": ..., "content": ...}` objects, read during the call. A failure (no chat template, malformed messages, render error) raises [`ClikaRT::Error`](../Error.md).

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 423

</div>

### streaming\_decoder() {#streaming_decoder}

<div class="api-signature">

[`StreamingDecoder`](./StreamingDecoder.md) **`streaming_decoder`**(`bool` `skip_special_tokens` `=` `true`) `const`

</div>

A streaming id→text decoder sharing this tokenizer's internals, the handle stays valid even after this [`Tokenizer`](./Tokenizer.md) is destroyed. One decoder serves one generation stream at a time (`finish` resets it). Infallible.

<div class="api-declared">

Declared in `ClikaRT/tokenizer/tokenizer.h`, line 430

</div>
