Repository navigation
fix(embedding): encode literal special-token spellings as ordinary text in truncate_to_token_limit - #497
fix(embedding): encode literal special-token spellings as ordinary text in truncate_to_token_limit#497rtmalikian wants to merge 3 commits into
Conversation
…xt in truncate_to_token_limit tiktoken defaults to disallowed_special="all", so a document containing a literal special-token spelling (e.g. "<|endoftext|>") raised ValueError and aborted the whole embedding batch on both embedding paths. Pass disallowed_special=() so such spellings are encoded as ordinary text; the chunking side already does this (see chunking_utils.py, StarTrail-org#461). Fixes StarTrail-org#496 Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
Fix the Lint and Format Check failure on PR StarTrail-org#497 (ruff format). Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
|
Fixed the Lint and Format Check failure — |
The native index.add() that update_index() makes aborts inside the FAISS
bindings on Windows exactly as it already does on macOS, taking the
whole pytest session with it:
Current thread 0x000002ec (most recent call first):
File "...\leann_backend_hnsw\faiss.py", line 7314 in add
File "...\leann\api.py", line 1242 in update_index
File "...\tests\test_update_index_auto_passage_ids.py", line 75 in _append
File "...\tests\test_update_index_auto_passage_ids.py", line 166 in
test_a_non_compact_hnsw_index_appends_without_ids_too
Fatal Python error: Aborted
On Windows it is intermittent, which is why it reads as a mystery rather
than a known issue: it failed #490 (windows-2022 3.13) and #497
(windows-2022 3.11) at that identical stack while #487, #499 and main
all passed the same job. Two unrelated pull requests -- a Gemini task
type and HFChat threading -- were being blocked at random by a crash
neither of them touches.
The existing macOS skip already records the cause and notes that "the
passage-ID decision under test happens before that call," so no coverage
of the behaviour under test is lost by skipping the platforms where the
native call dies. This only extends that decision to win32; the
underlying abort in the non-compact HNSW append path is untouched and
still wants a real fix.
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
|
This is green on its own merits and just needs git fetch origin main && git merge origin/main && git pushTwo things changed on
Once it is pushed I will merge — no re-review needed, your change itself has not moved. 🤖 Generated with Claude Code |
|
Thanks for checking this — really helpful, especially the CHANGELOG merge=union note. Merging main in is the right call; I'll push the merge and confirm once it's up. |
…x/496-truncate-special-token Signed-off-by: Raphael Malikian <rtmalikian@gmail.com>
|
Pushed — main is now merged into the branch (ab5d825) and the branch is up to date with main. Thanks again for verifying the merge on your side. |
What does this PR do?
truncate_to_token_limit(packages/leann-core/src/leann/embedding_compute.py) calledenc.encode(text)with nodisallowed_specialargument. tiktoken defaults todisallowed_special="all", so any document containing a literal special-token spelling (e.g.<|endoftext|>— ordinary corpus content such as a tokenizer tutorial, a blog post about LLMs, or pasted chat logs) raisedValueErrorand killed the whole embedding batch on both embedding paths (OpenAI batch path:989, Ollama/local path:1311).The fix passes
disallowed_special=()so such spellings are encoded as ordinary text and round-trip exactly. This mirrors the chunking side, which #461 already fixed the same way inchunking_utils.py.Related Issues
Fixes #496
Changes Made
packages/leann-core/src/leann/embedding_compute.pyenc.encode(text)->enc.encode(text, disallowed_special=())intruncate_to_token_limit, with a comment explaining whytests/test_token_truncation.pyTesting
truncate_to_token_limit(["<|endoftext|>"]', 2048)raisedValueError: Encountered text corresponding to disallowed special token '<|endoftext|>'(verified on the fork-tip tree).PYTHONPATH=packages/leann-core/src python -m pytest tests/test_token_truncation.py -q: 29 passed (26 existing + 3 new).ValueError).tests/test_chunk_size_cap.py: 9 passed, 2 failed — the 2 failures (HuggingFace discovery tests) fail identically on the pristine tree (pre-existing, network-dependent).ruff/uvare not available on this host, soruff format/ruff check, fulluv run pytest, andpre-commit run --all-filesare left to CI; the changed lines are within the repo's 100-char line limit andpy_compilepasses on both files.Additional Notes
Author: Raphael Malikian — rtmalikian@gmail.com. If you'd like similar help with your own project or codebase, I'm open to collaborate — feel free to reach out.
This PR was prepared with AI assistance.
Checklist
uv run pytest)ruff formatandruff check)pre-commit run --all-files)