haystack icon indicating copy to clipboard operation
haystack copied to clipboard

DocumentSplitter: Hindi, Marathi, Bengali and other danda (।) scripts aren't split into sentences, so the whole document becomes one chunk

Open shlokbam opened this issue 3 weeks ago • 1 comments

Describe the bug

DocumentSplitter(split_by="sentence") finds no sentence boundaries in Hindi, Marathi, Bengali and other languages that end sentences with the danda । (U+0964) or double danda ॥ (U+0965). The whole document comes back as one chunk, whatever split_length is set to.

RecursiveDocumentSplitter has the same issue: its default "sentence" separator sees the whole Hindi paragraph as one sentence. It then falls through to "\n" / " " and cuts chunks mid-sentence.

The cause is in haystack/components/preprocessors/sentence_tokenizer.py. ISO639_TO_NLTK only covers 19 languages, and hi, mr, bn, ne, pa, or and sa aren't among them, so load_sentence_tokenizer() falls back to the English Punkt model. Its PunktLanguageVars.sent_end_chars is only ('.', '?', '!'), so । is never treated as a sentence end. This happens with both keep_white_spaces=False (stock PunktLanguageVars) and keep_white_spaces=True (CustomPunktLanguageVars, which inherits the same sent_end_chars).

Why it matters for RAG: for any document longer than the embedding model's input limit, the single oversized chunk is silently truncated, so only its first few hundred tokens are embedded. The rest of the document can never be retrieved, and nothing raises an error.

Error message

No exception. Only these warnings are logged (one pair per language):

PreProcessor couldn't find the default sentence tokenizer model for hi.  Using English instead. You may train your own model and use the 'tokenizer_model_folder' parameter.
No abbreviations file found for hi. Using default abbreviations.

(The same pair appears for bn and mr.) Side note: the warning points to a tokenizer_model_folder parameter, which no Haystack 3.x component has as far as I can tell. It looks like a leftover from the 1.x PreProcessor.

Expected behavior

। and ॥ should end a sentence, the same way . does in English. For the six-sentence Hindi paragraph below with split_length=2, I'd expect 3 chunks, the same as the English version.

Results on haystack-ai 3.4.1 (split_overlap=0):

splitter / mode language sentences chunks produced expected
DocumentSplitter split_by="period", split_length=2 en 6 3 3
DocumentSplitter split_by="period", split_length=2 hi 6 1 3 *
DocumentSplitter split_by="sentence", language="en", split_length=2 en 6 3 3
DocumentSplitter split_by="sentence", language="hi", split_length=2 hi 6 1 3
DocumentSplitter split_by="sentence", language="bn", split_length=1 bn 4 1 4
DocumentSplitter split_by="sentence", language="mr", split_length=1 mr 4 1 4
RecursiveDocumentSplitter default separators, sentences found by the "sentence" step en 6 6 6
RecursiveDocumentSplitter default separators, sentences found by the "sentence" step hi 6 1 6

* split_by="period" splits on a literal ".", so this row may be working as designed. I've included it only because it's the obvious thing users try next.

Additional context

Affected languages: anything that uses the danda as a full stop, including Hindi (hi), Marathi (mr), Nepali (ne), Sanskrit (sa), Bengali (bn), Assamese (as), Punjabi/Gurmukhi (pa) and Odia (or). That's several hundred million speakers.

The same bug was just reported in LlamaIndex: run-llama/llama_index#23772 (their SentenceSplitter chunking regex has no ।/॥ either).

Candidate fix, verified without changing Haystack's code. I patched only the tokenizer that load_sentence_tokenizer() returns, adding ।/॥ to sent_end_chars:

from haystack.components.preprocessors import sentence_tokenizer as st

_orig = st.load_sentence_tokenizer
def patched(language, keep_white_spaces=False):
    tok = _orig(language, keep_white_spaces=keep_white_spaces)
    base = type(tok._lang_vars)  # PunktLanguageVars or CustomPunktLanguageVars
    class DandaLangVars(base):
        sent_end_chars = tuple(base.sent_end_chars) + ("।", "॥")
    tok._lang_vars = DandaLangVars()
    return tok
st.load_sentence_tokenizer = patched

With the patch, every sentence-mode row above gives the expected count: hi → 3, bn → 4, mr → 4, and the Recursive "sentence" step finds all 6 Hindi sentences. The English rows don't change (3 and 6).

A real fix would go in haystack/components/preprocessors/sentence_tokenizer.py (line numbers from v3.4.1):

  1. Danda as a sentence end: a PunktLanguageVars subclass with sent_end_chars = ('.', '?', '!', '।', '॥'), which CustomPunktLanguageVars (L85) would inherit from. load_sentence_tokenizer() (L80–81) would set it as _lang_vars in both the keep_white_spaces=False and True branches. Right now the stock vars are kept when keep_white_spaces=False.
  2. And/or language codes: add hi/mr/bn/ne/pa/or/sa to the Language Literal (L17) and handle them in the lookup around ISO639_TO_NLTK (L21, L68). NLTK ships no Punkt model for them, so they would still load the English pickle plus the danda-aware lang vars, but without the misleading warning (and the type checker would accept them).

Which approach would you prefer? Option 1 alone fixes the chunking for any language code, but it changes sent_end_chars for every language, which shouldn't matter since । doesn't appear in Latin-script text. Option 2 keeps the change scoped to the Indic codes.

Happy to open a PR (with tests and a release note) if this direction sounds good.

To Reproduce

# pip install haystack-ai nltk
from haystack import Document
from haystack.components.preprocessors import DocumentSplitter

HI = ("भारत एक विशाल देश है। इसकी राजधानी नई दिल्ली है। यहाँ अनेक भाषाएँ बोली जाती हैं। "
      "हिंदी सबसे अधिक बोली जाने वाली भाषा है। गंगा भारत की सबसे पवित्र नदी मानी जाती है। हिमालय उत्तर में स्थित है।")
EN = ("India is a large country. Its capital is New Delhi. Many languages are spoken here. "
      "Hindi is the most widely spoken language. The Ganga is considered India's holiest river. "
      "The Himalayas are in the north.")

for text, lang in ((EN, "en"), (HI, "hi")):
    splitter = DocumentSplitter(split_by="sentence", split_length=2, split_overlap=0, language=lang)
    splitter.warm_up()
    docs = splitter.run(documents=[Document(content=text)])["documents"]
    print(lang, len(docs))

# en 3
# hi 1   <- expected 3

FAQ Check

System:

  • OS: macOS 27.0.1 (arm64, Apple M2)
  • Haystack version: haystack-ai 3.4.1 (latest stable on PyPI), nltk 3.10.4, Python 3.13

shlokbam avatar Nov 09 '26 15:11 shlokbam

Hi! I’m new to contributing to Haystack and saw that this PR is addressing the danda sentence-splitting issue. Would it be helpful if I checked whether the change covers both DocumentSplitter and RecursiveDocumentSplitter, or are those cases already covered?

anvithspatil avatar Nov 10 '26 10:11 anvithspatil