feat: DocumentSplitter — add split_by="token" mode using tiktoken - #12529
Open
rautaditya2606 wants to merge 1 commit into
Open
feat: DocumentSplitter — add split_by="token" mode using tiktoken#12529rautaditya2606 wants to merge 1 commit into
rautaditya2606 wants to merge 1 commit into
Conversation
rautaditya2606
requested review from
anakin87
and
a lite review from Copilot
and removed request for
a team
August 31, 2026 10:57
|
@rautaditya2606 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
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.
Related Issues
closes #12528
Proposed Changes
DocumentSplittersupports 7split_bymodes but not token-based splitting. Users who needsimple token-aware chunking are forced to use
RecursiveDocumentSplittereven when they don'tneed recursion. This PR adds
split_by="token"toDocumentSplitterusing tiktoken, aligningit with the existing
split_unit="token"support inRecursiveDocumentSplitter.What changed:
split_by="token"added to theLiteraltype andvalid_split_bylisttokenizer_encoding: str = "o200k_base"param (keyword-only, matchesRecursiveDocumentSplitterdefault)LazyImport— same pattern asRecursiveDocumentSplitter__init__: eagertiktoken_imports.check()if token mode; setsself._tiktoken_tokenizer = Nonewarm_up(): loads tiktoken encoder if token mode — mirrors sentence splitter pattern exactlyrun(): auto-triggerswarm_up()for token mode — same lazy-init pattern as sentence mode_split_by_token(): encode → slice by[i : i + split_length]withstep = split_length - split_overlap→ decode; trackscur_pageandcur_start_idx; delegates to_create_docs_from_splitsto_dict(): serialisestokenizer_encodingBefore:
After:
How did you test it?
Added
TestSplittingByTokenclass (7@pytest.mark.integrationtests) totest/components/preprocessors/test_document_splitter.py:test_basic_chunking— chunks produced, no chunk exceedssplit_lengthtokenstest_metadata_set—source_id,split_id,split_idx_start,page_numberall set correctlytest_overlap_produces_shared_text— overlap produces correct index overlap between consecutive chunkstest_page_tracking—page_numberadvances correctly across\fboundariestest_empty_document_skipped— empty content skipped with correct log messagetest_custom_encoding—tokenizer_encoding="cl100k_base"works correctlytest_serialization_roundtrip—to_dict/from_dictround-trip preserves all paramsAll 56 existing unit tests and 7 new integration tests pass. mypy clean.
Notes for the reviewer
_split_by_tokenis self-contained and does not share code withRecursiveDocumentSplitter—the recursive splitter's tokenizer logic is deeply intertwined with its overlap and chunk-length
helpers. A future refactor can extract shared utilities if needed.
split_thresholdis not applied in token mode — consistent with how it is silently irrelevantfor
sentenceandfunctionmodes.split_overlap >= split_lengthguard at_init_checksL153–154 covers token modeautomatically — no additional guard needed.
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.