From ca32f507974523a0c6cdb5859a5412b62fbce4c9 Mon Sep 17 00:00:00 2001 From: adavyas <121313528+adavyas@users.noreply.github.com> Date: Wed, 5 Aug 2026 12:28:39 -0700 Subject: [PATCH] fix: resolve tiktoken encoding without constructing the embedding client (#955) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/embedding_client.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/embedding_client.py b/src/embedding_client.py index 208d9d71..92edab68 100644 --- a/src/embedding_client.py +++ b/src/embedding_client.py @@ -677,8 +677,19 @@ class EmbeddingClient: @property def encoding(self) -> tiktoken.Encoding: - """Get the tiktoken encoding.""" - return self._get_client().encoding + """Get the tiktoken encoding. + + Resolved without constructing the underlying client: tiktoken needs no + API key, and token-counting callers (e.g. the document dedup tie-break) + must work in environments with no embedding credentials, such as CI for + pull requests from forks. + """ + if self._instance is not None: + return self._instance.encoding + try: + return tiktoken.encoding_for_model(self._resolve_runtime_config().model) + except KeyError: + return tiktoken.get_encoding("cl100k_base") # Shared singleton embedding client instance