Skip to content

fix: preserve serialized tokenizer truncation direction - #775

Merged
joein merged 2 commits into
qdrant:mainfrom
hulkbig:fix/preserve-tokenizer-truncation-direction
Oct 4, 2026
Merged

joein merged 2 commits into
qdrant:mainfrom
hulkbig:fix/preserve-tokenizer-truncation-direction

Conversation

@hulkbig

@hulkbig hulkbig commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #774.

Loading a tokenizer saved with left truncation currently switches it to right truncation when load_tokenizer reapplies the context limit. For one two three four five and a three-token limit, the loaded tokenizer keeps one two three instead of the saved tokenizer's three four five.

Preserve the serialized truncation direction, defaulting to right when no truncation settings were saved. Existing context-limit resolution, stride reset, and padding behavior remain unchanged; no new public argument is added.

The 15 offline regression cases use real, tiny WordLevel tokenizers and cover left/right/unset direction, smaller/equal/larger context limits, the stricter config limit, mixed-length batches through OnnxTextModel, stride reset, and independent left padding.

Validation on Python 3.13.3 / macOS arm64, tokenizers 0.23.2:

  • Unmodified upstream loader: 8 regression failures, 7 passing controls; standalone example reproduces the wrong retained tokens.
  • This branch: 22 passed (tests/test_tokenizer_truncation.py and tests/test_common.py), with outbound socket connections forbidden and Hugging Face offline mode enabled. The standalone example retains the expected tokens.
  • Repository mypy command: no issues in 65 source files; pyright tests/type_stub.py: no errors.
  • Pre-commit-pinned Ruff 0.3.4 lint/format and git diff --check: passed.
  • Poetry sdist/wheel build: passed; packaged loader matches this commit.

The pretrained-model suite, including test_preprocessor_utils.py whose fixture constructs/downloads a model, was not run. No model weights, ONNX session, inference, or embedding-quality evaluation was used. The OS/Python CI matrix and upstream's newer lockfile environment remain unverified locally. An initial collection attempt failed because the interrupted dependency installation had not installed FastEmbed's package metadata; installing the locally built wheel without dependencies resolved that setup problem before the red/green runs above.

All Submissions

Pre-commit hooks were not installed; their pinned Ruff checks were run directly. AI assistance (OpenAI Codex) was used for implementation review, regression validation, and preparing this submission.

@hulkbig
hulkbig requested a review from joein as a code owner October 4, 2026 14:11
@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 49089272-5fd2-4398-afd5-b66ddc715b9a
📥 Commits

Reviewing files that changed from the base of the PR and between 8409d6e and 90889ca.

📒 Files selected for processing (2)
  • fastembed/common/preprocessor_utils.py
  • tests/test_preprocessor_utils.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

load_tokenizer now resolves the truncation direction from tokenizer_config.json’s truncation_side, then tokenizer.json’s serialized direction, and defaults to right. It passes the resolved direction when enabling truncation. Parameterized tests cover the default, each source set alone, and conflicting settings.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 90889

The tokenizer direction change appears ready to merge after normal checks.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Issue #774 requires load_tokenizer to retain the truncation direction stored in tokenizer.json. The loader checks tokenizer_config.json's truncation_side first. The added conflict test confirm… When tokenizer.json contains a truncation direction, preserve it while applying the context limit. Add or update a test for conflicting directions. Use a fallback only when the tokenizer JSON has no saved direction.
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving the truncation direction saved with a tokenizer.
Description check ✅ Passed The description explains the truncation-direction bug, the fix, and the reported validation. It is directly related to the changeset.
Out of Scope Changes check ✅ Passed The loader change and its tests address truncation-direction resolution, which is the subject of issue #774. The config fallback and precedence test remain within that scope. No unrelated changes appe…
Full details: Linked Issues check

Explanation

Issue #774 requires load_tokenizer to retain the truncation direction stored in tokenizer.json. The loader checks tokenizer_config.json's truncation_side first. The added conflict test confirms that this value overrides the direction in tokenizer.json, so a saved left direction is not retained when the config says right.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@joein joein left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for addressing this!

@joein
joein merged commit 7d1e76b into qdrant:main Oct 4, 2026
17 checks passed
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.

[Bug]: Loading a tokenizer resets serialized left truncation to right

2 participants