feat: use token-based sizing for embedding chunking#749
Merged
Conversation
Contributor
There was a problem hiding this comment.
1 issue found across 5 files
Confidence score: 4/5
- This PR looks safe to merge with minimal risk overall, but there is a moderate performance concern (severity 5/10, high confidence) rather than a functional correctness break.
- In
open_notebook/utils/embedding.py, debug-metric tokenization runs unconditionally, which can add avoidable CPU/latency overhead on every embedding batch call. - Pay close attention to
open_notebook/utils/embedding.py- gate or defer debug tokenization so normal embedding paths do not pay the extra cost.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="open_notebook/utils/embedding.py">
<violation number="1" location="open_notebook/utils/embedding.py:124">
P2: Expensive tokenization for debug metrics is executed unconditionally, adding avoidable overhead on every embedding batch call.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
The previous 512-token default matched exactly the context window of BERT-family embedders like mxbai-embed-large, leaving no margin for: - tokenizer mismatch between our o200k_base measurement and the embedder's own WordPiece tokenizer - occasional splitter overshoot (RecursiveCharacterTextSplitter can emit chunks slightly above chunk_size when separators are sparse) - special tokens ([CLS], [SEP]) that consume context-window budget 400 tokens keeps ~20% headroom below 512 while still being a large improvement over the old character-based default for most content. Users with larger-context embedders can raise OPEN_NOTEBOOK_CHUNK_SIZE via env var. Also adds a CHANGELOG entry for the full PR behavior change.
Target release is 1.8.5 — moving the Changed section out of Unreleased.
dd94b8b to
05ce03d
Compare
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.
Description
This PR replaces character-based sizing with token-based sizing in the embedding chunking pipeline.
Specifically, it updates chunking and embedding threshold checks to use the repository's existing
token_count()contract instead oflen(...), which improves chunk sizing consistency for CJK and mixed-language content.As part of this behavior change, the default
OPEN_NOTEBOOK_CHUNK_SIZEis also reduced from1200to512, since chunk size semantics now refer to tokens rather than characters and512is a safer cross-provider baseline.Related Issue
Fixes #542
Type of Change
How Has This Been Tested?
uv run pytest)Test Details:
tests/test_chunking.pytests/test_embedding.pyto validate token-based long-text routingUV_CACHE_DIR=/tmp/uv-cache uv run pytest tests/test_chunking.pyUV_CACHE_DIR=/tmp/uv-cache uv run pytest tests/test_embedding.py50 passedDesign Alignment
Which design principles does this PR support? (See DESIGN_PRINCIPLES.md)
Explanation:
This change keeps the implementation simple by reusing the repository's existing
token_count()contract instead of introducing a second tokenizer path. It also better supports multi-provider embedding usage by using token-based chunk sizing and a more conservativedefault of
512tokens.Checklist
Code Quality
Testing
make rufforruff check . --fixmake lintoruv run python -m mypy .Documentation
/docs(if applicable)Database Changes
/migrations)Breaking Changes
Screenshots (if applicable)
N/A
Additional Context
This PR intentionally keeps the existing
token_count()fallback behavior unchanged. The goal here is to remove character-based chunk sizing from the embedding pipeline, not to redesign tokenizer fallback behavior in the same change set.Pre-Submission Verification
Before submitting, please verify: