Skip to content

BertTokenizer throws ArgumentException on lone surrogates and U+FFFE #7950

Done

Description

@krwq

System Information (please complete the following information):

Describe the bug
BertTokenizer throws ArgumentException ("String contains invalid Unicode code points") when the text contains:

  • a lone surrogate and RemoveNonSpacingMarks is on
  • U+FFFE, with any options

A lone surrogate is easy to hit, for example when a text is cut into chunks in the middle of an emoji.

Both come from string.Normalize in BertNormalizer: the NFD at the start (only with RemoveNonSpacingMarks) and the NFC at the end reject these characters. #7820 drops lone surrogates in the normalizer loop, but with RemoveNonSpacingMarks the NFD runs before that loop. In globalization-invariant mode string.Normalize returns its input unchanged, so nothing throws there.

To Reproduce

using Microsoft.ML.Tokenizers;

var tokenizer = BertTokenizer.Create("vocab.txt", new BertOptions { RemoveNonSpacingMarks = true });
tokenizer.EncodeToIds("one\uD83Dline"); // ArgumentException

BertTokenizer.Create("vocab.txt").EncodeToIds("one\uFFFEline"); // ArgumentException

Expected behavior
No exception. Both characters should be dropped, the same way BertNormalizer already drops U+FFFD. Hugging Face gives [UNK] for U+FFFE, but it's a noncharacter, so dropping it is fine.

Screenshots, Code, Sample Projects
A fix on top of #7938, which changes the same line:

// string.Normalize throws on lone surrogates and U+FFFE. The loop below removes U+FFFD.
original = _isNormalizationSupported
    ? SentencePieceNormalizationStep.ReplaceLoneSurrogates(original).Replace('\uFFFE', '\uFFFD').Normalize(NormalizationForm.FormD)
    : NonSpacingMarkRemover.Remove(original);

SentencePieceNormalizationStep.ReplaceLoneSurrogates also needs to become internal, and the check at the top of the loop that skips U+0000 and U+FFFD needs || c == '\uFFFE'.

A regression test for BertTokenizerTests, using AssertTokens and the vocabulary from #7820. With #7938 applied, it fails without the fix and passes with it, with ICU and in invariant mode.

Test
// string.Normalize throws on a lone surrogate, as in a text cut in the middle of an emoji, and on U+FFFE, as in UTF-16 read
// with the wrong byte order.
[Theory]
[InlineData(0xD83D, false)]
[InlineData(0xD83D, true)]
[InlineData(0xDC4D, true)]
[InlineData(0xFFFE, false)]
[InlineData(0xFFFE, true)]
public void TestLoneSurrogatesAndUFFFEAreRemoved(int c, bool removeNonSpacingMarks)
{
    AssertTokens($"one{(char)c}line", "[CLS] one ##line [SEP]", new BertOptions { RemoveNonSpacingMarks = removeNonSpacingMarks });
    AssertTokens($"great product {(char)c}", "[CLS] great product [SEP]", new BertOptions { RemoveNonSpacingMarks = removeNonSpacingMarks });
}

Additional context
I found this while reviewing #7820 and kept it out of that PR, because the fix overlaps with my PR #7938. I'll fix it once #7938 is merged.

Activity

  1. self-assigned this
    on Nov 25, 2026
  2. added
    bug
    area-Tokenizers
    on Nov 25, 2026
  3. added
    untriaged
    on Nov 25, 2026
  4. removed
    untriaged
    on Nov 26, 2026
  5. added a commit that references this issue on Nov 27, 2026
    218d0b8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tokenizersbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions