Skip to content

fix: resolve tiktoken encoding without requiring an embedding API key - #955

Open
adavyas wants to merge 1 commit into
mainfrom
fix/embedding-encoding-without-api-key
Open

fix: resolve tiktoken encoding without requiring an embedding API key#955
adavyas wants to merge 1 commit into
mainfrom
fix/embedding-encoding-without-api-key

Conversation

@adavyas

@adavyas adavyas commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Summary

EmbeddingClient.encoding forced full construction of the underlying embedding client, which raises ValueError: 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 .encoding for 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-python fails on tests/crud/test_document.py::TestDocumentCRUD::test_duplicate_rejection_reinforces_existing for every fork PR (currently the only red check on #908, which is fork-based).

Fix

The wrapper's encoding property now resolves tiktoken directly from the configured model (falling back to cl100k_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

  • Reproduced the exact CI failure locally on main with no LLM_OPENAI_API_KEY set; after the fix, tests/crud/test_document.py passes (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.
  • ruff clean, basedpyright 0 errors.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved encoding lookup to avoid unnecessary client initialization.
    • Added a fallback encoding for unsupported models, improving reliability.

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

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

EmbeddingClient.encoding now resolves token encodings without constructing the underlying client unless it already exists. It uses the cached encoding when available, otherwise resolves the configured model and falls back to cl100k_base.

Changes

Embedding encoding resolution

Layer / File(s) Summary
Lazy encoding lookup
src/embedding_client.py
EmbeddingClient.encoding returns the existing client’s cached encoding or resolves the runtime model encoding directly, with a cl100k_base fallback.

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

Possibly related PRs

Suggested reviewers: rajat-ahuja1997

Poem

I’m a bunny with tokens, hopping light,
No client wakes unless the time is right.
Cached encodings lead the way,
Models guide the rest of the day,
And cl100k_base saves the byte!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly captures the main change: resolving tiktoken encoding without needing an embedding API key.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/embedding-encoding-without-api-key

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4d3ab1c and 042f1c2.

📒 Files selected for processing (1)
  • src/embedding_client.py

Comment thread src/embedding_client.py
Comment on lines +684 to +685
if self._instance is not None:
return self._instance.encoding

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 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.

Suggested change
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.

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.

1 participant