From 042f1c26fd2403b86b06edef75a259cea96d4cb3 Mon Sep 17 00:00:00 2001 From: adavyas Date: Thu, 30 Jul 2026 13:09:59 -0400 Subject: [PATCH] fix: resolve tiktoken encoding without constructing the embedding client 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 07197f43..d66af318 100644 --- a/src/embedding_client.py +++ b/src/embedding_client.py @@ -674,8 +674,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