crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

bug: NlpSentenceChunking.chunk() uses list(set(sens)) which destroys sentence order

Open kuishou68 opened this issue 7 months ago • 7 comments

Bug Description

In crawl4ai/chunking_strategy.py, the NlpSentenceChunking.chunk() method returns list(set(sens)):

def chunk(self, text: str) -> list:
    from nltk.tokenize import sent_tokenize
    sentences = sent_tokenize(text)
    sens = [sent.strip() for sent in sentences]
    return list(set(sens))  # BUG: set() is unordered!

Problem

set() in Python is unordered — converting sentences to a set and back to a list:

  1. Destroys sentence order: sentences are returned in arbitrary order, not document order
  2. Removes duplicate sentences that may be intentionally present

Fix

Return sens directly: return sens

kuishou68 avatar Apr 10 '26 08:04 kuishou68

I'll take this. One-line fix — list(set(sens)) destroys sentence order. PR incoming.

SamSi0322 avatar Apr 10 '26 15:04 SamSi0322

Nice find, thanks for reporting this.

On Fri, Apr 10, 2026, 10:19 AM SamSi0322 @.***> wrote:

SamSi0322 left a comment (unclecode/crawl4ai#1909) https://github.com/unclecode/crawl4ai/issues/1909#issuecomment-4224788810

I'll take this. One-line fix — list(set(sens)) destroys sentence order. PR incoming.

— Reply to this email directly, view it on GitHub https://github.com/unclecode/crawl4ai/issues/1909#issuecomment-4224788810, or unsubscribe https://github.com/notifications/unsubscribe-auth/CAXZQSIFFRE6PVY6U6JQGKT4VEGHPAVCNFSM6AAAAACXTSYME2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DEMRUG44DQOBRGA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

liamoss1231-ai avatar Apr 10 '26 15:04 liamoss1231-ai

Thanks for the clear report @kuishou68, and @SamSi0322 and @liamoss1231-ai for jumping on it.

I've pushed a fix on fix/nlp-sentence-chunking-1909 that adopts the approach from #1910 — returning sens directly without dedup. Chunking should split, not filter; if downstream consumers need deduplication they can handle it themselves.

While fixing this, I also found that NlpSentenceChunking.__init__() had a broken re-import (from crawl4ai.le.legacy.model_loader import ...) that shadows the working top-level import. Removed that in the same commit.

ntohidi avatar Apr 11 '26 09:04 ntohidi

mrged with the develop branch and will be in the next releas soon :)

ntohidi avatar Apr 16 '26 08:04 ntohidi

Hi @kuishou68 — thanks for the clear root cause analysis!

Fix submitted in PR #1933. list(set(sens)) → return sens directly, preserving document order and keeping intentional duplicate sentences.

How to test before it's merged

pip install git+https://github.com/hafezparast/crawl4ai.git@fix/maysam-nlp-sentence-order-1909

Could you confirm this resolves the issue?

hafezparast avatar Apr 24 '26 03:04 hafezparast

@hafezparast Thanks for the contribution! This was already fixed in PR #1913 and merged into the develop branch. It will be in the next release.

ntohidi avatar Apr 24 '26 15:04 ntohidi

The fix is included in the latest version, 0.8.7.

ntohidi avatar Jun 01 '26 14:06 ntohidi