FIX: Chunk large inputs to prevent tiktoken-rs stack overflow - #9
Merged
Conversation
xfalcox
force-pushed
the
fix/tiktoken-stack-overflow-chunking
branch
2 times, most recently
from
February 26, 2026 16:42
32a7108 to
312046b
Compare
pmusaraj
approved these changes
Feb 26, 2026
xfalcox
force-pushed
the
fix/tiktoken-stack-overflow-chunking
branch
from
February 26, 2026 17:35
312046b to
aa1191b
Compare
tiktoken-rs v0.9.1 uses fancy-regex for BPE tokenization, which can stack overflow on large inputs due to catastrophic backtracking (openai/tiktoken#245). The Rust code unwraps the error, panics across the FFI boundary, and aborts the entire Ruby process — this is not rescuable from Ruby. A secondary issue is quadratic BPE merge time for long non-whitespace runs (openai/tiktoken#195), which causes the process to hang indefinitely. Python tiktoken fixed the backtracking in v0.8.0 with possessive quantifiers (openai/tiktoken#258), but tiktoken-rs 0.9.1 and tiktoken_ruby 0.0.15.1 have not ported the fix, and no newer versions are available. This adds a safe_encode method that chunks text larger than 50K characters at whitespace boundaries before passing to tiktoken. BPE token boundaries never span whitespace, so chunked encoding produces identical results for normal text. For pathological inputs (e.g. 500K repeated characters with no whitespace), it completes in seconds instead of crashing or hanging.
xfalcox
force-pushed
the
fix/tiktoken-stack-overflow-chunking
branch
from
February 26, 2026 17:35
aa1191b to
c4cef25
Compare
xfalcox
added a commit
to discourse/discourse
that referenced
this pull request
Feb 26, 2026
Bumps discourse_ai-tokenizers from 0.4 to 0.4.1 for a fix for tiktoken-rs stack overflow crash that aborts the Ruby process when encoding large inputs (discourse/discourse_ai-tokenizers#9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fancy-regexfor BPE tokenization, which can stack overflow on large inputs due to catastrophic backtracking (openai/tiktoken#245). The Rust code.unwrap()s the error, panics across the FFI boundary, and aborts the entire Ruby process — this is not rescuable from Ruby.The crash (from GDB / container logs):
Fix
Adds a
safe_encodemethod that chunks text larger than 50K characters at whitespace boundaries before passing to tiktoken. Since BPE token boundaries never span whitespace, chunked encoding produces identical results for normal text. For pathological inputs (e.g. 500K repeated characters with no whitespace), it completes in seconds instead of crashing or hanging.OpenAiCl100kTokenizerinherits fromOpenAiTokenizerand gets the fix automatically.Test plan
"^" * 500_000) complete without crashing