Skip to content

FIX: Normalize non-UTF-8 inputs before tokenization - #10

Merged
xfalcox merged 2 commits into
mainfrom
fix-tokenizer
Feb 27, 2026
Merged

FIX: Normalize non-UTF-8 inputs before tokenization#10
xfalcox merged 2 commits into
mainfrom
fix-tokenizer

Conversation

@SamSaffron

Copy link
Copy Markdown
Member

Add normalize_text helper to BasicTokenizer and
OpenAiTokenizer that converts ASCII-8BIT, Latin-1, and
other non-UTF-8 strings to valid UTF-8 before passing them
to the underlying tokenizer. Invalid byte sequences are
scrubbed with replacement characters.

This prevents EncodingError crashes in truncate, encode,
below_limit?, and tokenize when callers pass binary or
mixed-encoding strings (common in Rails when reading from
external sources).

Bumps version to 0.4.2.

Add `normalize_text` helper to `BasicTokenizer` and
`OpenAiTokenizer` that converts ASCII-8BIT, Latin-1, and
other non-UTF-8 strings to valid UTF-8 before passing them
to the underlying tokenizer. Invalid byte sequences are
scrubbed with replacement characters.

This prevents `EncodingError` crashes in `truncate`, `encode`,
`below_limit?`, and `tokenize` when callers pass binary or
mixed-encoding strings (common in Rails when reading from
external sources).

Bumps version to 0.4.2.
@xfalcox
xfalcox merged commit 7232a18 into main Feb 27, 2026
5 checks passed
@xfalcox
xfalcox deleted the fix-tokenizer branch February 27, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants