fix: resolve tiktoken encoding without requiring an embedding API key - #955
fix: resolve tiktoken encoding without requiring an embedding API key#955adavyas wants to merge 1 commit into
Conversation
EmbeddingClient.encoding forced full client construction, which raises 'OpenAI API key is required' even though tiktoken needs no credentials. The document dedup tie-break (src/crud/document.py) only needs .encoding for token counting, so any test hitting that path fails in environments without embedding keys — notably CI for pull requests from forks, where repo secrets are unavailable (e.g. #908's test-python job failing on tests/crud/test_document.py::test_duplicate_rejection_reinforces_existing). Resolve the encoding from the configured model directly, falling back to cl100k_base, and only reuse the underlying client's encoding when it has already been constructed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Walkthrough
ChangesEmbedding encoding resolution
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/embedding_client.py`:
- Around line 684-685: Update the encoding accessor branch in the relevant
embedding client method to use _get_client().encoding instead of returning
_instance.encoding directly, ensuring configuration changes trigger the existing
signature-based client refresh and keep encoding behavior aligned with chunking
and embedding.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 76c223cf-f8bd-4571-8b79-e715eff37f0b
📒 Files selected for processing (1)
src/embedding_client.py
| if self._instance is not None: | ||
| return self._instance.encoding |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Preserve configuration freshness for cached encodings.
This branch returns _instance.encoding without checking _instance_signature. If embedding settings change after initialization, _get_client() rebuilds for the new configuration, but encoding continues returning the old encoding, causing token counts to diverge from chunking and embedding behavior. Reuse _get_client().encoding here, or perform the same signature check before returning the cached value.
Proposed fix
- if self._instance is not None:
- return self._instance.encoding
+ if self._instance is not None:
+ return self._get_client().encoding📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if self._instance is not None: | |
| return self._instance.encoding | |
| if self._instance is not None: | |
| return self._get_client().encoding |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/embedding_client.py` around lines 684 - 685, Update the encoding accessor
branch in the relevant embedding client method to use _get_client().encoding
instead of returning _instance.encoding directly, ensuring configuration changes
trigger the existing signature-based client refresh and keep encoding behavior
aligned with chunking and embedding.
Summary
EmbeddingClient.encodingforced full construction of the underlying embedding client, which raisesValueError: OpenAI API key is required— even though tiktoken needs no credentials. The document dedup tie-break (src/crud/document.py, unique-token comparison) only needs.encodingfor token counting, so any test exercising that path fails in environments with no embedding keys.Concretely: CI for pull requests from forks gets no repo secrets, so
test-pythonfails ontests/crud/test_document.py::TestDocumentCRUD::test_duplicate_rejection_reinforces_existingfor every fork PR (currently the only red check on #908, which is fork-based).Fix
The wrapper's
encodingproperty now resolves tiktoken directly from the configured model (falling back tocl100k_base), and only delegates to the underlying client's encoding when that client has already been constructed. No behavior change when credentials are present.Testing
mainwith noLLM_OPENAI_API_KEYset; after the fix,tests/crud/test_document.pypasses (21/21) with no key.tests/llm/test_embedding_client.py,tests/deriver/test_vector_reconciliation.py,tests/deriver/test_embed_now.py: 52 passed.ruffclean,basedpyright0 errors.🤖 Generated with Claude Code
Summary by CodeRabbit