[BUG] RAG text splitter repeats zero-overlap content and exceeds chunk_size
Description
RecursiveCharacterTextSplitter repeats source text with chunk_overlap=0 and can exceed its configured maximum chunk size. Retained separators are also inserted twice during merging.
Reproduction
from crewai_tools.rag.chunkers.base_chunker import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=3, chunk_overlap=0, separators=[""])
print(splitter.split_text("abcdef"))
# Actual: ["abc", "cde", "ef"]
# Expected: ["abc", "def"]
splitter = RecursiveCharacterTextSplitter(chunk_size=5, chunk_overlap=0, separators=["\n", ""])
print(splitter.split_text("aa\nbb\ncc"))
# Actual: ["aa\n\nbb", "\nbb\n\ncc"] (lengths 6 and 7)
# Expected: ["aa\nbb", "\ncc"] (lengths 5 and 3)
Expected behavior
Merging respects both chunk_size and chunk_overlap, without adding separators that are already present. The excess content otherwise changes retrieval chunks and embedding input sizes.
Cause and verification
The overlap loop always retains one split, even for zero overlap, and does not make room for the next split. Separator accounting assumes removed separators even when they were retained. Four regressions fail on current main; a local fix passes all 137 RAG tests, including overlap and separator controls. Python 3.12 on macOS arm64; no LLM calls.
AI assistance was used to investigate, implement and test this change.
The tested fix is now in #7471 (draft); all 137 RAG tests pass, and AI assistance was used. GitHub returns 403 when I try to add llm-generated; could a maintainer apply the required label to this issue and the PR before review?
Hi maintainers,
I’d be happy to help fix this issue.
From my understanding, there are two main problems in the current merging logic:
- The overlap cleanup loop uses
len(current_doc) > 1, which always retains at least one previous split. As a result, content is repeated even whenchunk_overlap=0. - When
keep_separator=True, the separator is already included in the generated splits, but_merge_splits()may insert it again. The projected chunk length also does not consistently account for the separator.
My proposed approach is to:
- Use an empty merge separator whenever separators are already retained in the splits.
- Include the actual merge-separator length when checking whether the next split would exceed
chunk_size. - Remove old splits until both conditions are satisfied: the retained content is within
chunk_overlap, and adding the next split will not exceedchunk_size. - Allow the cleanup loop to remove the final retained split when necessary, especially when
chunk_overlap=0. - Keep the separator length accounting consistent when elements are removed from the sliding window.
I would also add regression tests covering zero overlap, non-zero overlap, retained separators, multi-character separators, and the invariant that generated chunks do not exceed chunk_size.
If this approach looks reasonable, I’d be happy to prepare a pull request.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
This issue was closed because it has been stalled for 5 days with no activity.