Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: start overlapping document chunks on a whole word (#15442)

Nilayabaa1b595314
diff --git a/studio/backend/plugins/data-designer-unstructured-seed/src/data_designer_unstructured_seed/chunking.py b/studio/backend/plugins/data-designer-unstructured-seed/src/data_designer_unstructured_seed/chunking.py
index 5362f541c6b..b569ecf3d8c 100644
--- a/studio/backend/plugins/data-designer-unstructured-seed/src/data_designer_unstructured_seed/chunking.py
+++ b/studio/backend/plugins/data-designer-unstructured-seed/src/data_designer_unstructured_seed/chunking.py
@@ -16,6 +16,9 @@ DEFAULT_CHUNK_OVERLAP = 200
MAX_CHUNK_SIZE = 20000
_MIN_BREAK_RATIO = 0.6
_CACHE_DIR = LazyPath(unstructured_seed_cache_root)
+_WHITESPACE = re.compile(r"\s+")
+# Bump when chunk output changes so cached Parquet seeds are rebuilt.
+_CHUNKER_VERSION = 2
_PANDAS = None
@@ -238,6 +241,11 @@ def split_text_into_chunks(*, text: str, chunk_size: int, chunk_overlap: int) ->
break
next_start = end - chunk_overlap
+ if 0 < next_start < end and not text[next_start - 1].isspace():
+ boundary = _WHITESPACE.search(text, next_start, end)
+ # No space in the overlap (CJK, one long token): keep the full overlap.
+ if boundary:
+ next_start = boundary.end()
if next_start <= start:
next_start = end
start = max(0, next_start)
@@ -273,6 +281,7 @@ def _compute_cache_key(*, source_path: Path, chunk_size: int, chunk_overlap: int
str(stat.st_mtime_ns),
str(chunk_size),
str(chunk_overlap),
+ f"v{_CHUNKER_VERSION}",
]
).encode("utf-8")
return hashlib.sha256(payload).hexdigest()
@@ -285,6 +294,6 @@ def _compute_multi_file_cache_key(
for path, name in sorted(file_entries, key = lambda e: e[1]):
st = path.stat()
parts.append(f"{path}|{st.st_size}|{st.st_mtime_ns}|{name}")
- parts.append(f"cs={chunk_size}|co={chunk_overlap}")
+ parts.append(f"cs={chunk_size}|co={chunk_overlap}|v={_CHUNKER_VERSION}")
raw = "\n".join(parts)
return hashlib.sha256(raw.encode()).hexdigest()
diff --git a/studio/backend/tests/test_data_recipe_books_template.py b/studio/backend/tests/test_data_recipe_books_template.py
index d3f78b38517..4c7f773d7ab 100644
--- a/studio/backend/tests/test_data_recipe_books_template.py
+++ b/studio/backend/tests/test_data_recipe_books_template.py
@@ -33,3 +33,57 @@ def test_books_template_turns_extracted_text_into_text_rows_without_a_model(tmp_
assert len(rows) == 2
assert rows["text"][0].startswith("It was a bright cold day")
assert all(0 < len(text) <= source["chunk_size"] for text in rows["text"])
+
+
+def test_books_template_rows_start_on_a_word_boundary(tmp_path):
+ pytest.importorskip("data_designer")
+ pytest.importorskip("data_designer_unstructured_seed")
+ from core.data_recipe.service import build_config_builder, create_data_designer
+
+ paragraphs = (" ".join(f"word{p}x{w}" + "o" * (w % 5) for w in range(150)) for p in range(30))
+ book = "\n\n".join(paragraphs)
+ extracted = tmp_path / "book.extracted.txt"
+ extracted.write_text(book, encoding = "utf-8")
+ recipe = json.loads(_TEMPLATE.read_text(encoding = "utf-8"))["recipe"]
+ source = recipe["seed_config"]["source"]
+ assert source["chunk_overlap"] > 0
+ source.pop("path")
+ source["paths"] = [str(extracted)]
+
+ designer = create_data_designer(recipe, artifact_path = str(tmp_path / "artifacts"))
+ rows = designer.preview(build_config_builder(recipe), num_records = 4).dataset
+
+ assert len(rows) == 4
+ for text in rows["text"]:
+ start = book.index(text)
+ assert start == 0 or book[start - 1].isspace(), text[:30]
+
+
+def test_overlap_is_kept_when_text_has_no_spaces():
+ pytest.importorskip("data_designer")
+ chunking = pytest.importorskip("data_designer_unstructured_seed.chunking")
+
+ text = "".join(chr(0x4E00 + (i * 7919) % 20000) for i in range(20000))
+ chunks = chunking.split_text_into_chunks(text = text, chunk_size = 6000, chunk_overlap = 600)
+
+ assert len(chunks) > 1
+ for previous, current in zip(chunks, chunks[1:]):
+ assert previous.endswith(current[:600])
+
+
+def test_seed_cache_is_rebuilt_when_the_chunker_changes(tmp_path, monkeypatch):
+ pytest.importorskip("data_designer")
+ chunking = pytest.importorskip("data_designer_unstructured_seed.chunking")
+
+ monkeypatch.setattr(chunking, "_CACHE_DIR", tmp_path / "cache")
+ source = tmp_path / "book.txt"
+ source.write_text("alpha beta gamma " * 50, encoding = "utf-8")
+ kwargs = dict(file_entries = [(source, "book.txt")], chunk_size = 300, chunk_overlap = 50)
+
+ monkeypatch.setattr(chunking, "_CHUNKER_VERSION", chunking._CHUNKER_VERSION - 1)
+ old_path, _ = chunking.materialize_multi_file_unstructured_seed(**kwargs)
+ monkeypatch.undo()
+ monkeypatch.setattr(chunking, "_CACHE_DIR", tmp_path / "cache")
+ new_path, _ = chunking.materialize_multi_file_unstructured_seed(**kwargs)
+
+ assert new_path != old_path