Indexing hangs without an error when a chunklet exceeds the embedding content budget
In the late chunking path, when a single chunklet needs more tokens than the content budget available at its position, the while loop in embed_strings_with_late_chunking never advances: it spins in place, appends one tuple to segments per iteration, and raises nothing. I let it run for 25s before killing it — by then it had gone through 3.5M iterations and RSS had grown by 243 MB.
It's reachable from the public API on ordinary input — 5 of the 22 chapters in a Chinese corpus I was indexing hit it.
Where
In src/raglite/_embed.py:106-113 the loop only advances via content_start_index = segment_end_index. When segment_end_index == content_start_index, it stops moving.
And _create_segment (_embed.py:58-60) returns exactly that in this case: if num_tokens[content_start_index] > max_tokens_content, then cumsum_forwards[0] already overshoots the target, so np.searchsorted(..., side="right") returns 0.
Minimal reproduction
This is 11 lines, needs only numpy, and is a direct transcription of the logic above. Feed it one 497-token chunklet: the loop never exits. Change it to 496: it exits after one iteration.
import numpy as np
MAX_TOKENS, MAX_TOKENS_PREAMBLE = 496, 189 # @512 -> min(n_ctx, n_batch) - 16, and round(0.382 * 496)
num_tokens = np.array([497]) # a single chunklet
segments, content_start_index = [], 0
while content_start_index < len(num_tokens):
cumsum = np.cumsum(num_tokens[content_start_index:])
offset = np.searchsorted(cumsum, MAX_TOKENS - MAX_TOKENS_PREAMBLE, side="right")
segment_end_index = content_start_index + int(offset)
segments.append((content_start_index, segment_end_index))
content_start_index = segment_end_index
The boundary lands exactly on 496/497 because @512 gives max_tokens = min(n_ctx, n_batch) - 16 = 496, and at index 0 the whole preamble budget (189) rolls into content. Further along, the budget is lower (307–496), so the effective threshold moves with position.
I ran into this indexing a Chinese corpus (22 markdown chapters, ~620k chars). Using the real split_sentences + split_chunklets, 5 of the 22 chapters produced a chunklet above the budget at its position:
| chapter | chunklet | chars | tokens | budget there |
|---|---|---|---|---|
| ch04 | 105 | 1239 | 827 | 328 |
| ch11 | 201 | 1311 | 671 | 377 |
| ch14 | 3 | 535 | 358 | 348 |
Note ch14 — it stalls well below 496, because the budget shrinks as the preamble fills up.
Nothing exotic about the offending chunks: 4 of the 5 are markdown tables, the fifth is a footnote block. Only 5 of 5127 chunklets (0.10%) go over, but one bad chunklet hangs the whole document — and therefore the whole insert_documents() call.
_insert.py:239-246 wraps future.result() in except Exception. A hang raises nothing, so as_completed never yields that future and insert_documents() never returns. From the outside the tqdm bar just freezes — no error, no traceback.
Suggested fix
A guard makes this fail loudly instead of hanging:
if segment_end_index <= content_start_index:
raise ValueError(
f"Chunklet {content_start_index} needs {num_tokens[content_start_index]} tokens, "
f"which exceeds the remaining segment content budget. Increase the embedder's "
f"context size (e.g. `@8192`) or lower `chunk_max_size`."
)
One note for whoever picks this up: the obvious alternative (content_start_index + max(1, offset_segment)) does terminate the loop, but the oversized segment then goes straight into embedder.embed(), which truncates to n_batch tokens by default — as the comment at _embed.py:98-101 already says. That trades a hang for silent data loss, which I'd argue is worse. The root cause is that chunk_max_size is counted in characters while the embedder's budget is counted in tokens, with nothing converting between them (_config.py:87 vs _config.py:80). I'll open a separate issue for that rather than mixing it in here.
I checked existing issues first: #134 is a different bug in the same function (rounding in the segment→sentence split), fixed by #144 — this one is in the boundary computation above it. #24 is about window size and is waiting on upstream; mine isn't blocked on upstream, the chunk-side limit and the embedder window both live in this repo and simply disagree.
Happy to open a PR with the guard (5 lines, no behaviour change — it only turns the hang into a clear error), or to run whichever experiment is more useful to you first. Environment: local checkout at d92a452. The 25s / 3.5M iterations / 243 MB numbers come from calling the real embed_strings() with the real XLM-R tokenizer and n_ctx = n_batch = 512, but with the llama-cpp-python backend stubbed out (not installed here); the 11-line reproduction above needs no backend and no such substitution.