fix: use tiktoken for accurate CJK token counting in document chunking - #1357
Open
Sanjay-Kirti wants to merge 1 commit into
Open
fix: use tiktoken for accurate CJK token counting in document chunking#1357Sanjay-Kirti wants to merge 1 commit into
Sanjay-Kirti wants to merge 1 commit into
Conversation
Sanjay-Kirti
force-pushed
the
fix/cjk-tokenizer-1354
branch
from
June 30, 2026 08:11
98ae397 to
0709fb4
Compare
debanjum
reviewed
Jul 29, 2026
debanjum
left a comment
Member
There was a problem hiding this comment.
Thanks for submitting a PR to fix this! Left some comments, can merge the changes once they're resolved and CI passes
Replace text.split() tokenizer in TextToEntries with tiktoken cl100k_base encoding. text.split() splits on whitespace, which severely undercounts tokens for CJK languages (Chinese, Japanese, Korean) that don't use spaces between words. This caused CJK markdown files to be saved as single entries instead of being chunked by headings, degrading search quality. The fix uses tiktoken (already a project dependency) with lazy-initialized encoder. All call sites only use len() on the result, so the return type change from List[str] to List[int] is safe. Also adds CJK-specific test cases and adjusts existing test thresholds to account for tiktoken's more accurate token counting. Fixes khoj-ai#1354
Sanjay-Kirti
force-pushed
the
fix/cjk-tokenizer-1354
branch
from
August 2, 2026 08:16
0709fb4 to
c5f4660
Compare
Author
|
@debanjum Thanks for the review, I've addressed all the feedback and just pushed the updates:
|
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
Fixes #1354
Replace
text.split()tokenizer inTextToEntrieswith tiktokencl100k_baseencoding to fix markdown/org chunking for CJK (Chinese, Japanese, Korean) languages.Problem
TextToEntries.tokenizer()usestext.split()which splits on whitespace. CJK languages don't use spaces between words, so a 744-token Chinese document was counted as ~152 tokens. This causedMarkdownToEntries(andOrgToEntries) to save entire CJK documents as single entries instead of chunking by headings severely degrading search quality for CJK users.Changes
src/khoj/processor/content/text_to_entries.py: Replacetext.split()withtiktoken.get_encoding("cl100k_base").encode()inTextToEntries.tokenizer(). Encoder is lazy initialized and cached as a class attribute.tests/test_markdown_to_entries.py: Add 3 CJK specific test cases:Token Count Improvements
Concrete examples of how tiktoken correctly counts tokens compared to the old whitespace-split method:
text.split()(old)tiktoken(new)Why this is safe
All 3 call sites only use
len(TextToEntries.tokenizer(...))they never inspect individual tokens. The return type change fromList[str]→List[int]has no impact.tiktokenis already a project dependency (used inhelpers.pyfor chat token counting).Test Results
Ran the full test suite locally with Postgres/pgvector enabled:
test_markdown_to_entries.pypass (including the 3 new CJK ones)