Repository navigation
feat: DocumentSplitter — add split_by="token" mode using tiktoken - #12529
Conversation
|
@rautaditya2606 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||
anakin87
left a comment
There was a problem hiding this comment.
I left some initial comments.
I'd appreciate it if also @davidsbatista could take a look as he previously worked on this component (if I am not mistaken)
|
hey @anakin87 , made all the changes. please let me know if anything else needs fixing |
|
You also need to update the haystack/components/preprocessors/document_preprocessor.py
|
|
Hello @davidsbatista, I've implemented this as you suggested |
|
Apart from a minor comment, it looks good. @davidsbatista could you take a final look? |
|
Looks already pretty good to me. I would only add two more tests to def test_add_page_number_to_metadata_with_no_overlap_token_split(self):
splitter = DocumentSplitter(split_by="token", split_length=5, split_overlap=0)
text = "one two three four five\fsix seven eight nine ten\feleven twelve thirteen fourteen fifteen"
doc = Document(content=text)
docs = splitter.run(documents=[doc])["documents"]
assert len(docs) > 1
assert docs[0].meta["page_number"] == 1
for d in docs:
expected_page = 1 + text[: d.meta["split_idx_start"]].count("\f")
assert d.meta["page_number"] == expected_page
assert docs[-1].meta["page_number"] == 3
def test_add_page_number_to_metadata_with_overlap_token_split(self):
splitter = DocumentSplitter(split_by="token", split_length=5, split_overlap=2)
text = "one two three four five\fsix seven eight nine ten\feleven twelve thirteen fourteen fifteen"
doc = Document(content=text)
docs = splitter.run(documents=[doc])["documents"]
assert len(docs) > 1
for d in docs:
expected_page = 1 + text[: d.meta["split_idx_start"]].count("\f")
assert d.meta["page_number"] == expected_page
assert docs[-1].meta["page_number"] == 3 |
|
Hello @davidsbatista, I have added the tests |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
davidsbatista
left a comment
There was a problem hiding this comment.
looks good to me, only waiting for @anakin87 approval and then we can merge
|
thank you for the review @davidsbatista and @anakin87 |
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.