Fix CJK token underestimation in _estimate_tokens fallback#661
Merged
MaojiaSheng merged 1 commit intovolcengine:mainfrom Mar 16, 2026
Merged
Fix CJK token underestimation in _estimate_tokens fallback#661MaojiaSheng merged 1 commit intovolcengine:mainfrom
MaojiaSheng merged 1 commit intovolcengine:mainfrom
Conversation
When tiktoken is unavailable, the fallback `len(text) // 3` severely
underestimates tokens for CJK text (Chinese/Japanese/Korean characters
are ~1-2 tokens each, not 0.33). This causes text exceeding the 8192-token
API limit to bypass chunking, resulting in BadRequestError.
Use `max(len(text) // 3, len(text.encode("utf-8")) // 4)` instead, which
picks the more conservative estimate. For ASCII-heavy text the char-based
estimate still wins; for CJK text the byte-based estimate correctly
produces ~0.75 tokens per character.
Fixes volcengine#616, fixes volcengine#634
Signed-off-by: JiangNan <1394485448@qq.com>
MaojiaSheng
approved these changes
Mar 16, 2026
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
When
tiktokenis unavailable,_estimate_tokens()falls back tolen(text) // 3, which assumes ~3 characters per token. This is reasonable for Latin/ASCII text but severely underestimates token counts for CJK (Chinese, Japanese, Korean) text, where each character typically maps to 1–2 tokens.The underestimation means CJK text that actually exceeds the 8192-token API limit gets an estimated count well below the threshold, so
_chunk_text()never triggers chunking. The full text is then sent to the embedding API, which rejects it with aBadRequestError.Fix
Replace:
With:
Each CJK character is 3 bytes in UTF-8, so
len(text.encode("utf-8")) // 4yields ~0.75 tokens per CJK character — much closer to the actual 1–2 range. Themax()ensures whichever estimate is more conservative wins: the char-based estimate is still used for ASCII-heavy text, while the byte-based estimate kicks in for CJK-heavy text.Fixes #616
Fixes #634