Skip to content

fix(embedding): encode literal special-token spellings as ordinary text in truncate_to_token_limit - #497

Open
rtmalikian wants to merge 3 commits into
StarTrail-org:mainfrom
rtmalikian:fix/496-truncate-special-token
Open

rtmalikian wants to merge 3 commits into
StarTrail-org:mainfrom
rtmalikian:fix/496-truncate-special-token

Conversation

@rtmalikian

Copy link
Copy Markdown
Contributor

What does this PR do?

truncate_to_token_limit (packages/leann-core/src/leann/embedding_compute.py) called enc.encode(text) with no disallowed_special argument. tiktoken defaults to disallowed_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) raised ValueError and 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 in chunking_utils.py.

Related Issues

Fixes #496

Changes Made

File Change
packages/leann-core/src/leann/embedding_compute.py enc.encode(text) -> enc.encode(text, disallowed_special=()) in truncate_to_token_limit, with a comment explaining why
tests/test_token_truncation.py 3 new regression tests: literal spelling under limit (verbatim), over limit (truncates to 16 tokens), mixed sentence (verbatim)

Testing

  • Repro before fix: truncate_to_token_limit(["<|endoftext|>"]', 2048) raised ValueError: 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).
  • Negative control: all 3 new tests fail on the pre-fix code (same ValueError).
  • Neighbor suite 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 / uv are not available on this host, so ruff format / ruff check, full uv run pytest, and pre-commit run --all-files are left to CI; the changed lines are within the repo's 100-char line limit and py_compile passes 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

  • Tests pass (uv run pytest)
  • Code formatted (ruff format and ruff check)
  • Pre-commit hooks pass (pre-commit run --all-files)

…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>
@rtmalikian

Copy link
Copy Markdown
Contributor Author

Fixed the Lint and Format Check failure — ruff format on tests/test_token_truncation.py (one call joined onto a single line). Pushed as a2d7bac; ruff check and ruff format --check are clean now.

ASuresh0524 added a commit that referenced this pull request Dec 3, 2026
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>
@ASuresh0524

Copy link
Copy Markdown
Collaborator

This is green on its own merits and just needs main merged in. One command, and it will not ask you to resolve anything:

git fetch origin main && git merge origin/main && git push

Two things changed on main that make this painless now:

Once it is pushed I will merge — no re-review needed, your change itself has not moved.

🤖 Generated with Claude Code

@rtmalikian

Copy link
Copy Markdown
Contributor Author

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>
@rtmalikian

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

truncate_to_token_limit crashes on literal special-token spellings in document text

2 participants