Skip to content

RecursiveDocumentSplitter split_idx_start is off by the trailing separator with split_unit="word" and split_overlap > 0 #12756

Description

@anishmehta24

Describe the bug
With split_unit="word" and split_overlap > 0, RecursiveDocumentSplitter sets split_idx_start one character (the length of the previous chunk's trailing separator) too far for every chunk after the first, and the _split_overlap ranges inherit the same shift. The repro from #11710 still fails on main (f1e61b5).

Cause, in _run_one: the position advances by len(chunk) - len(overlap_str), but in word mode _get_overlap rebuilds the overlap as " ".join(last_words), which has no trailing whitespace, while in the chunk those words are followed by the separator (" " with the default separators). So the subtraction leaves the position one separator past where the next chunk actually starts.

Error message
None, the metadata is silently wrong.

Expected behavior
document.content[split_idx_start : split_idx_start + len(chunk.content)] == chunk.content for every chunk (as the existing text.index(...) tests assert for the other modes), and the _split_overlap ranges of consecutive chunks point at the same text.

Additional context

from haystack import Document
from haystack.components.preprocessors import RecursiveDocumentSplitter

text = "This is sentence one. This is sentence two. This is sentence three. This is sentence four."
splitter = RecursiveDocumentSplitter(split_length=4, split_overlap=1, split_unit="word")
splitter.warm_up()
for doc in splitter.run(documents=[Document(content=text)])["documents"]:
    start = doc.meta["split_idx_start"]
    print(start, text.index(doc.content), repr(doc.content))
# 0 0 'This is sentence one. '
# 18 17 'one. This is sentence'
# 31 30 'sentence two. This is'
# 50 49 'is sentence three. This'
# 69 68 'This is sentence four.'

Same with separators=[" "] explicitly. split_unit="char" is correct.

To Reproduce
Run the snippet above.

FAQ Check

System:

  • OS: Windows 11
  • Haystack version (commit or version number): main at f1e61b5
  • Python: 3.12

I have a fix with regression tests and will open a PR. Found by property-testing the splitters; report and fix prepared with an AI assistant (Claude Code) and reviewed by me.

Activity

  1. sjrl commented on Oct 1, 2026

    @sjrl
    Contributor

    @anakin87 I think this is getting addressed in #13006

  2. anakin87 commented on Oct 1, 2026

    @anakin87
    Member

    @sjrl thank you!

  3. anakin87 commented on Oct 1, 2026

    @anakin87
    Member

    I verified that this bug was fixed in #13006

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions