crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] RAG text splitter repeats zero-overlap content and exceeds chunk_size

Open vineethsaivs opened this issue 2 months ago • 4 comments

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.

vineethsaivs avatar Sep 15 '26 06:09 vineethsaivs

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?

vineethsaivs avatar Sep 15 '26 06:09 vineethsaivs

Hi maintainers,

I’d be happy to help fix this issue.

From my understanding, there are two main problems in the current merging logic:

  1. The overlap cleanup loop uses len(current_doc) > 1, which always retains at least one previous split. As a result, content is repeated even when chunk_overlap=0.
  2. 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 exceed chunk_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.

Eg0-11 avatar Sep 15 '26 06:09 Eg0-11

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.

github-actions[bot] avatar Oct 25 '26 12:10 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Oct 31 '26 12:10 github-actions[bot]