[PR #32087] fix: preserve spaces between words when splitting by space separator #33544

Open
opened 2026-02-21 20:53:28 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/32087

State: open
Merged: No


Summary

  • Fixes a bug where spaces between words were removed in child chunks when using Knowledge Pipeline with Parent Chunk Mode.
  • When the text splitter falls back to space as the separator with keep_separator enabled (the default for RecursiveCharacterTextSplitter), the merge separator was incorrectly set to an empty string (""), causing words to be concatenated without spaces.
  • The space split path uses re.split(r" +", text) which removes all spaces from the text, but unlike other separators, the keep_separator logic does not re-append the space to each split. The merge step then joined splits with "" instead of " ", losing all word spacing.

Root Cause

In FixedRecursiveCharacterTextSplitter.recursive_split_text():

# Space split does NOT append separator to splits (unlike other separators)
if separator == " ":
    splits = re.split(r" +", text)
else:
    splits = text.split(separator)
    if self._keep_separator:
        splits = [s + separator for s in splits[:-1]] + splits[-1:]

# But _separator was still set to "" when keep_separator is True
_separator = "" if self._keep_separator else separator

Since keep_separator defaults to True in RecursiveCharacterTextSplitter, and the space case doesn't append the separator to each split, the merge step lost all spaces.

Fix

When the separator is a space, always use " " as the merge separator regardless of _keep_separator, since the space split path does not append the separator to splits.

Test plan

  • All 94 existing text splitter tests pass
  • Verify with a document containing space-separated words that child chunks preserve word spacing

Closes #32066

**Original Pull Request:** https://github.com/langgenius/dify/pull/32087 **State:** open **Merged:** No --- ## Summary - Fixes a bug where spaces between words were removed in child chunks when using Knowledge Pipeline with Parent Chunk Mode. - When the text splitter falls back to space as the separator with `keep_separator` enabled (the default for `RecursiveCharacterTextSplitter`), the merge separator was incorrectly set to an empty string (`""`), causing words to be concatenated without spaces. - The space split path uses `re.split(r" +", text)` which removes all spaces from the text, but unlike other separators, the `keep_separator` logic does not re-append the space to each split. The merge step then joined splits with `""` instead of `" "`, losing all word spacing. ## Root Cause In `FixedRecursiveCharacterTextSplitter.recursive_split_text()`: ```python # Space split does NOT append separator to splits (unlike other separators) if separator == " ": splits = re.split(r" +", text) else: splits = text.split(separator) if self._keep_separator: splits = [s + separator for s in splits[:-1]] + splits[-1:] # But _separator was still set to "" when keep_separator is True _separator = "" if self._keep_separator else separator ``` Since `keep_separator` defaults to `True` in `RecursiveCharacterTextSplitter`, and the space case doesn't append the separator to each split, the merge step lost all spaces. ## Fix When the separator is a space, always use `" "` as the merge separator regardless of `_keep_separator`, since the space split path does not append the separator to splits. ## Test plan - [x] All 94 existing text splitter tests pass - [ ] Verify with a document containing space-separated words that child chunks preserve word spacing Closes #32066
yindo added the pull-request label 2026-02-21 20:53:28 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33544