---
title: "Load a SentencePiece .model tokenizer in C#"
url: https://recursiveai.org/onenlp/cookbook/load-sentencepiece-model-file/
description: "Open a native SentencePiece .model file with OneNLP in C#. Encode text to pieces and IDs and see what happens to beginning- and end-of-sequence tokens."
version: "1.0.0"
---

# Load a SentencePiece .model tokenizer in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/load-sentencepiece-model-file/load-sentencepiece-model-file.zip)

Open a native `.model` with `Tokenizer.FromFile` to turn text into pieces and IDs.

```shell
dotnet add package RecursiveAI.OneNlp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneNlp.Tokenizers;

// tiny.model is in the example zip.
var tokenizer = Tokenizer
    .FromFile("tiny.model");
var encoded = tokenizer.Encode("hello worlds");
Console.WriteLine(string.Join(" ",
    encoded.Tokens));
```

The snippet prints:

```text
▁hello ▁world s
```

Unzip [the example](https://recursiveai.org/onenlp/cookbook/load-sentencepiece-model-file/load-sentencepiece-model-file.zip), then `dotnet run`.

The zip includes `tiny.model`, an invented Unigram file. Its pieces, in ID order, are `<unk>`, `<s>`, `</s>`, `▁hello`, `▁world`, and `s`.

## Full program

```csharp
using RecursiveAI.OneNlp.Tokenizers;

var tokenizer = Tokenizer
    .FromFile("tiny.model");
var withSpecials = tokenizer
    .Encode("hello worlds");
var withoutSpecials = tokenizer.Encode(
    "hello worlds",
    addSpecialTokens: false);
Console.WriteLine("Default: " +
    string.Join(" ", withSpecials.Tokens));
Console.WriteLine("IDs: " +
    string.Join(" ", withSpecials.Ids));
Console.WriteLine("Without specials: " +
    string.Join(" ", withoutSpecials.Ids));
var literal = tokenizer.Encode("</s>");
Console.WriteLine("Literal </s>: " +
    string.Join(" ", literal.Ids));
Console.WriteLine("ID 2 is special: " +
    tokenizer.IsSpecialToken(2));
```

`dotnet run` prints:

```text
Default: ▁hello ▁world s
IDs: 3 4 5
Without specials: 3 4 5
Literal </s>: 0 5 0
ID 2 is special: False
```

The file reserves ID `2` for `</s>`, but `IsSpecialToken(2)` returns `False` because the native loader doesn’t register control pieces as special tokens. The literal text `</s>` encodes as `<unk> s <unk>` (`0 5 0`): ID `0` is `<unk>`, and only `s` has a matching ordinary piece.

## Variations

**Load a stream instead of a path.** Name the format explicitly when you don’t have the filename extension:

```csharp
using RecursiveAI.OneNlp.Tokenizers;

using var stream = File.OpenRead("tiny.model");
var tokenizer = Tokenizer.FromStream(stream,
    TokenizerFormat.SentencePieceModel);
Console.WriteLine(string.Join(" ",
    tokenizer.EncodeToIds("hello worlds")));
```

```text
3 4 5
```

## Related recipes

- [Load a BERT vocab.txt tokenizer](https://recursiveai.org/onenlp/cookbook/load-bert-wordpiece-vocabulary/)
- [Load WordPiece and SentencePiece tokenizers](https://recursiveai.org/onenlp/cookbook/load-transformer-tokenizer-files/)

## The short answer

Pass the native SentencePiece file to `Tokenizer.FromFile("tiny.model")` and call `Encode` to get the pieces and IDs. OneNLP recognizes the `.model` extension. The file in the zip is a small Unigram example. It doesn’t insert a beginning or end marker, even when you leave `addSpecialTokens` at its default.

## What’s in a SentencePiece .model file?

The binary file stores an ordered list of pieces, their scores and types, the model kind, and normalization settings. The order supplies the IDs. In this example `▁hello` and `▁world` have IDs `3` and `4`, and `s` has ID `5`. The `▁` symbol represents a space, including the space marker before the first word in this model. It is part of the piece, not an extra ID to insert. Use the `.model` that belongs to your checkpoint; the same text can produce different IDs with a different vocabulary.

OneNLP 1.0.0 has loaders for Unigram and BPE `.model` files. The extension alone doesn’t tell you which kind the file contains. A word- or character-type model throws `NotSupportedException`. The loader also refuses models that preserve extra spaces or don’t escape spaces. If you get `NotSupportedException` on a `.model`, check how the file was trained; changing the filename doesn’t change the model kind or its settings.

## Does SentencePiece add BOS or EOS tokens by default?

Not with this native Unigram loader. BOS and EOS mean beginning and end of sequence. The sample reserves `<s>` and `</s>` as control pieces, but they don’t match ordinary input and the loader installs no post-processor to insert them. That’s why `addSpecialTokens: false` and the default call return the same IDs in the full program. The flag controls insertion by a post-processor; it doesn’t cause a reserved control piece to match literal text.

A converted JSON tokenizer can define a sequence template that does insert these IDs. If you switch from that configuration to a native file, check the expected IDs for a known input against the model’s reference encoding. Add boundary IDs only if the model expects them, and use that model’s IDs, not the IDs from this sample. The [native loader’s source](https://github.com/recursive-ai-oss/onenlp-csharp/blob/59bd14caa0e08a13494d74e29c396d1444730911/src/OneNlp/Tokenizers/Loaders/SentencePieceModelFile.cs#L85-L117) shows which Unigram pieces can match and how the tokenizer is assembled.
