# gpt-oss tokenizer and Harmony format Status: implemented and checked against llama.cpp on 2026-10-14 (STRA-213). Crates: `leleka-tokenizer` (`gguf_bpe`) and `leleka-harmony`. ## Tokenizer (`crates/tokenizer/src/gguf_bpe.rs`) A byte-level BPE tokenizer built from the vocabulary inside the GGUF file (`tokenizer.ggml.model` = `gpt2`, `tokenizer.ggml.pre` = `gpt-4o`): 201,088 tokens, 446,189 merges, token types, bos/eos/pad ids. It loads in about 0.3 s. Text is split by the o200k pre-tokenizer expression, each piece is mapped to the GPT-2 "bytes as characters" alphabet and merged by rank with a heap (O(n log n), long runs of one character are safe). `Utf8Stream` turns token bytes into text deltas that never split a character. Special tokens (`<|start|>`, `<|end|>`, `<|message|>`, `<|channel|>`, `<|constrain|>`, `<|return|>`, `<|call|>`, `<|endoftext|>`, reserved ids) are all control tokens in this vocabulary. ### Check against llama.cpp `crates/tokenizer/tests/gpt_oss_oracle.rs` compares ids with llama.cpp's `/tokenize` on 67 strings (`fixtures/gpt_oss_corpus.json`: ASCII, code, JSON, numbers, case changes, whitespace and line endings, eleven scripts, emoji, combining marks, control characters, Harmony markers and look-alikes). The expected ids are in `fixtures/gpt_oss_ids.json`. The vocabulary belongs to the model file, which is not in the repository, so the test needs `LELEKA_GPTOSS_GGUF=` and otherwise reports "skipped". Result: identical ids for every string except the two deliberate differences below. ### Deliberate differences from llama.cpp 1. **Special text is never parsed unless asked.** `encode(text, false)` produces no special token. The llama.cpp server turns `<|start|>`, `<|end|>`, `<|message|>`, `<|channel|>` and `<|constrain|>` into special tokens even with `parse_special` off, so a user could type `<|end|><|start|>system...` and forge role markers. Leleka tokenizes user-supplied text with `parse_special = false` and the rendered template with `true`, so user text cannot create markers (the test checks this). 2. **Combining marks stay with their word.** The pre-tokenizer is OpenAI's o200k expression, where `\p{M}` belongs to the word it follows. llama.cpp's rewritten expression leaves `\p{M}` out, so it splits such words (the Hindi sentence in the corpus: llama.cpp 13 ids, Leleka 8). Precomposed accented Latin, Cyrillic, Greek, CJK, Arabic and Hebrew strings in the corpus are identical. The o200k expression is the one the model was trained with; this was not measured, only reasoned from the published expression. ## Harmony (`crates/harmony`) * `render(messages, tools, options)` reproduces the gpt-oss chat template stored in the GGUF, including its whitespace quirks (nested object types, oneOf alternatives, defaults). `tests/render_oracle.rs` compares it with llama.cpp's `/apply-template` on ten conversations (plain, developer/system messages, tool schemas of many shapes, two tool rounds, empty arguments, dropped history reasoning, unicode and look-alike markers): byte-identical. Two template behaviours are worth knowing: a `set` inside a loop does not leak out in Jinja, so the template's "object variants become any" shortcut never applies; and a tool schema array without `items` makes the template itself fail (not rendered by the oracle, so not covered). * The server field for the model's reasoning is `reasoning_content` (it becomes `thinking` in the template); a field named `thinking` is dropped by the server. * Differences kept on purpose: a tool without a description is rendered without the `//` line (the template would raise); `current_date` is a parameter, not read from the clock. * `HarmonyParser` turns the generated stream (text pieces and special markers) into events: reasoning text, answer text, tool calls (name, argument deltas, end), `Returned`, `Called`. ## Regenerating the fixtures Start a private llama-server with the model (`-m -ngl 0 -c 512 --port 8099`), then POST each corpus string to `/tokenize` (`add_special` false, `parse_special` false and true) and each conversation to `/apply-template`. Stop the server afterwards. The fixtures contain only test strings and ids, no credentials or captured requests.