Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(splitters): surface a clear settings error when sentence chunk_size exceeds the tokenizer cap (#5117)

Amir Fathi4e7005aafc5c
diff --git a/changelog.d/+sentence-splitter-chunk-size-cap.bugfix.md b/changelog.d/+sentence-splitter-chunk-size-cap.bugfix.md
new file mode 100644
index 0000000000..61d8e5073c
--- /dev/null
+++ b/changelog.d/+sentence-splitter-chunk-size-cap.bugfix.md
@@ -0,0 +1,9 @@
+Fixed the **Sentence-based** chunking method aborting indexing with a
+confusing `sentence-transformers/all-mpnet-base-v2` token-limit
+`ValueError` whenever `chunk_size` exceeds 384 (which includes the
+default of 1000). That tokenizer is an internal splitter default the user
+never configures, so the raw error read as an embedding failure.
+`get_text_splitter` now catches the cap error and raises a clear,
+settings-side message that names the `chunk_size` the user set and points
+at the other chunking methods (`recursive`, `token`, `semantic`). Refs
+#4800.
diff --git a/src/local_deep_research/embeddings/splitters/text_splitter_registry.py b/src/local_deep_research/embeddings/splitters/text_splitter_registry.py
index 50a29706e4..79cfdd52f0 100644
--- a/src/local_deep_research/embeddings/splitters/text_splitter_registry.py
+++ b/src/local_deep_research/embeddings/splitters/text_splitter_registry.py
@@ -7,6 +7,7 @@ based on configuration, similar to how embeddings_config.py works for embeddings
from __future__ import annotations
+import re
import threading
from typing import Optional, List, Any, TYPE_CHECKING
@@ -119,10 +120,46 @@ def get_text_splitter(
SentenceTransformersTokenTextSplitter,
)
- return SentenceTransformersTokenTextSplitter(
- chunk_overlap=chunk_overlap,
- tokens_per_chunk=chunk_size,
- )
+ try:
+ return SentenceTransformersTokenTextSplitter(
+ chunk_overlap=chunk_overlap,
+ tokens_per_chunk=chunk_size,
+ )
+ except ValueError as exc:
+ # SentenceTransformersTokenTextSplitter is backed by a fixed
+ # sentence-transformers tokenizer. It raises at construction,
+ # before any chunk is emitted, when tokens_per_chunk (our
+ # chunk_size) exceeds that tokenizer's max_seq_length. The raw
+ # error names an internal model the user never configured, which
+ # reads as an embedding failure. Translate only that specific cap
+ # error into a settings-side message that points at the chunk_size
+ # the user actually set; re-raise anything else unchanged.
+ #
+ # The cap is model-specific (LangChain reads it from
+ # model.max_seq_length; 384 is the default all-mpnet-base-v2
+ # value but not universal), so read it back from LangChain's own
+ # message rather than hardcoding a number that could silently lie
+ # if the tokenizer ever changes. See issue #4800.
+ message = str(exc)
+ if "maximum token limit" not in message:
+ raise
+ cap_match = re.search(r"is:\s*(\d+)", message)
+ if cap_match:
+ cap = cap_match.group(1)
+ limit_phrase = f"capped at {cap} tokens per chunk"
+ fix_phrase = f"Lower the chunk size to {cap} or less"
+ else:
+ limit_phrase = (
+ "capped at the tokenizer's maximum sequence length"
+ )
+ fix_phrase = "Lower the chunk size to fit that limit"
+ raise ValueError(
+ f"chunk_size={chunk_size} is too large for the 'sentence' "
+ f"chunking method: it uses a fixed sentence-transformers "
+ f"tokenizer {limit_phrase}. {fix_phrase}, or choose a "
+ f"different chunking method ('recursive', 'token', or "
+ f"'semantic') for larger chunks."
+ ) from exc
if splitter_type == "semantic":
# Semantic chunking requires embeddings
diff --git a/tests/embeddings/test_text_splitter_registry.py b/tests/embeddings/test_text_splitter_registry.py
index a8df7d3bc6..d8baa4d780 100644
--- a/tests/embeddings/test_text_splitter_registry.py
+++ b/tests/embeddings/test_text_splitter_registry.py
@@ -201,6 +201,132 @@ class TestGetTextSplitterSentence:
assert isinstance(splitter, SentenceTransformersTokenTextSplitter)
+class TestGetTextSplitterSentenceCapError:
+ """Regression tests for #4800.
+
+ The 'sentence' splitter is backed by a fixed sentence-transformers
+ tokenizer whose cap is the model's max_seq_length (384 for the default
+ all-mpnet-base-v2, but model-specific). For chunk_size over that cap (the
+ default chunk_size is 1000) SentenceTransformersTokenTextSplitter raises at
+ construction, before any chunk is emitted, with a message naming an
+ internal model the user never configured. get_text_splitter must translate
+ that specific error into a clear, settings-side message and surface the
+ real per-model cap read back from LangChain's message, not a hardcoded
+ number. These tests mock the splitter so they need no model download and
+ run in CI.
+ """
+
+ _CAP_MESSAGE = (
+ "The token limit of the models "
+ "'sentence-transformers/all-mpnet-base-v2' is: 384. "
+ "Argument tokens_per_chunk=500 > maximum token limit."
+ )
+
+ def test_oversized_chunk_size_raises_actionable_error(self):
+ """chunk_size over the cap yields a clear, chunk_size-oriented error."""
+ from local_deep_research.embeddings.splitters.text_splitter_registry import (
+ get_text_splitter,
+ )
+
+ with patch(
+ "langchain_text_splitters.sentence_transformers.SentenceTransformersTokenTextSplitter",
+ side_effect=ValueError(self._CAP_MESSAGE),
+ ):
+ with pytest.raises(ValueError) as exc_info:
+ get_text_splitter(splitter_type="sentence", chunk_size=500)
+
+ msg = str(exc_info.value)
+ # Names the setting the user controls, not the internal tokenizer.
+ assert "chunk_size=500" in msg
+ assert "384" in msg
+ # Points at the actionable alternatives.
+ assert "recursive" in msg
+ assert "token" in msg
+ assert "semantic" in msg
+
+ def test_valid_chunk_size_returns_splitter(self):
+ """chunk_size within the cap constructs and returns the splitter."""
+ from local_deep_research.embeddings.splitters.text_splitter_registry import (
+ get_text_splitter,
+ )
+
+ mock_splitter = MagicMock()
+ with patch(
+ "langchain_text_splitters.sentence_transformers.SentenceTransformersTokenTextSplitter",
+ return_value=mock_splitter,
+ ) as mock_class:
+ splitter = get_text_splitter(
+ splitter_type="sentence", chunk_size=256
+ )
+
+ assert splitter is mock_splitter
+ assert mock_class.call_args[1]["tokens_per_chunk"] == 256
+
+ def test_unrelated_value_error_is_reraised_unchanged(self):
+ """A non-cap ValueError from the splitter is not swallowed or rewritten."""
+ from local_deep_research.embeddings.splitters.text_splitter_registry import (
+ get_text_splitter,
+ )
+
+ with patch(
+ "langchain_text_splitters.sentence_transformers.SentenceTransformersTokenTextSplitter",
+ side_effect=ValueError("something else went wrong"),
+ ):
+ with pytest.raises(ValueError, match="something else went wrong"):
+ get_text_splitter(splitter_type="sentence", chunk_size=500)
+
+ def test_cap_is_read_from_message_not_hardcoded(self):
+ """The reported cap tracks the active tokenizer, not a fixed 384.
+
+ LangChain derives the limit from model.max_seq_length, so a different
+ tokenizer reports a different cap. The translated message must surface
+ that model-specific number and must not leak the default 384.
+ """
+ from local_deep_research.embeddings.splitters.text_splitter_registry import (
+ get_text_splitter,
+ )
+
+ cap_message = (
+ "The token limit of the models 'BAAI/bge-large-en-v1.5' "
+ "is: 512. Argument tokens_per_chunk=900 > maximum token limit."
+ )
+ with patch(
+ "langchain_text_splitters.sentence_transformers.SentenceTransformersTokenTextSplitter",
+ side_effect=ValueError(cap_message),
+ ):
+ with pytest.raises(ValueError) as exc_info:
+ get_text_splitter(splitter_type="sentence", chunk_size=900)
+
+ msg = str(exc_info.value)
+ assert "chunk_size=900" in msg
+ assert "512" in msg
+ assert "384" not in msg
+
+ def test_cap_error_without_a_parseable_limit_still_translates(self):
+ """A cap error whose number cannot be parsed still gets a clear message.
+
+ The gate is the 'maximum token limit' phrase; if LangChain's wording
+ ever drops the 'is: N' clause we still translate to a settings-side
+ message (without inventing a number) instead of leaking the raw error.
+ """
+ from local_deep_research.embeddings.splitters.text_splitter_registry import (
+ get_text_splitter,
+ )
+
+ with patch(
+ "langchain_text_splitters.sentence_transformers.SentenceTransformersTokenTextSplitter",
+ side_effect=ValueError(
+ "tokens_per_chunk exceeds maximum token limit"
+ ),
+ ):
+ with pytest.raises(ValueError) as exc_info:
+ get_text_splitter(splitter_type="sentence", chunk_size=900)
+
+ msg = str(exc_info.value)
+ assert "chunk_size=900" in msg
+ assert "recursive" in msg
+
+
class TestGetTextSplitterSemantic:
"""Tests for get_text_splitter with semantic type."""