Repository navigation
fix(core): fit chunk sizes to the embedding model's sequence limit - #434
Merged
Merged
Conversation
leann build used fixed 256/512-token chunks regardless of the model. The macOS default all-MiniLM-L6-v2 reads only 256 tokens, so ~43% of indexed code text was never embedded. - cli: cap doc/code chunk size at 70% of the model's limit (WordPiece counts ~30% above tiktoken and SentenceSplitter overshoots on unsplittable spans) - embedding_compute: read max_seq_length from Hugging Face sentence-transformers repos instead of defaulting them to 2048 - chunking_utils: max_tokens_per_chunk only shrinks chunk_size (it used to replace it, growing 256 to ~1700 for unknown models) Code retrieval on leann-core sources (18 queries, real indexes): MRR@10 0.30 -> 0.54, hit@1 0.11 -> 0.44. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first SentenceSplitter use in CI loads NLTK stopwords, which the runner's path guard can refuse (hard-linked file), so a size comparison depended on test order. Record the chunk_size/overlap passed to the splitter instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
leann buildused fixed chunk sizes (doc 256, code 512 tiktoken tokens) regardless of the embedding model. The macOS defaultall-MiniLM-L6-v2reads only 256 tokens, so on a realleann buildofleann-coresources 43% of the indexed code text was never embedded (median code chunk = 461 MiniLM tokens).Two related bugs:
get_model_token_limit()didn't know Hugging Face sentence-transformers models and fell back to 2048.create_text_chunks(max_tokens_per_chunk=...)replacedchunk_sizeinstead of capping it, so an explicit 256 grew to ~1700 for those models.Fix
--doc-chunk-size/--code-chunk-sizeat 70% of the model's limit (overlap capped to half). 70% because WordPiece counts ~30% more tokens than tiktoken on web text/code, andSentenceSplitterovershoots on spans it can't split (tables, long lines). Prints📏 … chunk size doc 256 → 179, code 512 → 179when it applies. Models with large limits (OpenAI, nomic, Qwen3) are unaffected.max_seq_lengthfrom the repo'ssentence_bert_config.jsonfor local HF models (only when nobase_url, name contains/).max_tokens_per_chunkonly shrinkschunk_size(traditional and AST paths).Results (all-MiniLM-L6-v2)
Real
leann build+LeannSearcheronleann-coresources, 18 natural-language code queries (relevant = chunk containing the targetdef):Offline sweep (brute-force cosine): code MRR 0.258 @512 → 0.607 @256 → 0.658 @179. On 42 web pages / 22 queries, doc sizes 179–256 are within noise (MRR 0.62–0.73), so the doc cap is about guaranteeing text gets embedded rather than a quality jump.
Trade-off: more, smaller chunks for small models (≈4.8× on code), so builds embed more passages.
Tests
tests/test_chunk_size_cap.py(cap only shrinks; large limit doesn't grow chunks; HF discovery and its failure path, no Hub call withbase_urlor bare names).CI=true pytest tests/: 463 passed; 7 failures are pre-existing onmainlocally (diskann not installed,test_cli_prompt_templatesync pickle).Existing indexes are untouched;
leann build --forcere-chunks.🤖 Generated with Claude Code