Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

feat: disable counting tokens and send strings as part of testing openai api embedding models (#4229)

S345150e7b405
diff --git a/src/local_deep_research/embeddings/providers/implementations/openai.py b/src/local_deep_research/embeddings/providers/implementations/openai.py
index 4f60487341..553ade1a99 100644
--- a/src/local_deep_research/embeddings/providers/implementations/openai.py
+++ b/src/local_deep_research/embeddings/providers/implementations/openai.py
@@ -1,6 +1,7 @@
"""OpenAI embedding provider."""
from typing import Any, Dict, List, Optional
+from urllib.parse import urlparse
from langchain_core.embeddings import Embeddings
from loguru import logger
@@ -127,6 +128,13 @@ class OpenAIEmbeddingsProvider(BaseEmbeddingProvider):
if base_url:
params["openai_api_base"] = base_url
+ # Disable client-side context length checks only for non-OpenAI
+ # hosts (LM Studio, vLLM, llama.cpp, etc.) which may lack tiktoken
+ # model entries or reject tokenized inputs. Keep the LangChain
+ # default for api.openai.com so the guard stays in place for users
+ # who set base_url explicitly to the real OpenAI endpoint.
+ if urlparse(base_url).hostname != "api.openai.com":
+ params["check_embedding_ctx_length"] = False
# For text-embedding-3 models, dimensions can be customized
if dimensions and model.startswith("text-embedding-3"):
diff --git a/tests/embeddings/test_openai_embeddings.py b/tests/embeddings/test_openai_embeddings.py
index 1dfa819589..7272434142 100644
--- a/tests/embeddings/test_openai_embeddings.py
+++ b/tests/embeddings/test_openai_embeddings.py
@@ -97,6 +97,8 @@ class TestOpenAIEmbeddingsProviderCreateEmbeddings:
call_kwargs = mock_class.call_args[1]
assert call_kwargs["model"] == "text-embedding-3-small"
assert call_kwargs["openai_api_key"] == "test-api-key"
+ # No base_url → LangChain default ctx-check is preserved.
+ assert "check_embedding_ctx_length" not in call_kwargs
def test_create_embeddings_missing_api_key_raises(self):
"""Test that missing API key raises ValueError."""
@@ -165,6 +167,39 @@ class TestOpenAIEmbeddingsProviderCreateEmbeddings:
call_kwargs["openai_api_base"]
== "https://custom.openai.com"
)
+ assert call_kwargs["check_embedding_ctx_length"] is False
+
+ def test_create_embeddings_with_official_openai_base_url_keeps_ctx_check(
+ self,
+ ):
+ """Explicit base_url pointing at api.openai.com must NOT disable the
+ client-side context-length check; that guard is only meant to be
+ skipped for non-OpenAI hosts (LM Studio, vLLM, llama.cpp, etc.)."""
+ from local_deep_research.embeddings.providers.implementations.openai import (
+ OpenAIEmbeddingsProvider,
+ )
+
+ mock_embeddings = MagicMock()
+
+ with patch(
+ "local_deep_research.embeddings.providers.implementations.openai.get_setting_from_snapshot",
+ return_value=None,
+ ):
+ with patch(
+ "langchain_openai.OpenAIEmbeddings",
+ return_value=mock_embeddings,
+ ) as mock_class:
+ OpenAIEmbeddingsProvider.create_embeddings(
+ api_key="test-key",
+ base_url="https://api.openai.com/v1",
+ )
+
+ call_kwargs = mock_class.call_args[1]
+ assert (
+ call_kwargs["openai_api_base"]
+ == "https://api.openai.com/v1"
+ )
+ assert "check_embedding_ctx_length" not in call_kwargs
def test_create_embeddings_with_dimensions(self):
"""Test creating embeddings with custom dimensions for v3 model."""
@@ -462,6 +497,7 @@ class TestOpenAIEmbeddingsProviderCompatibleEndpoint:
call_kwargs["openai_api_base"]
== "http://host.docker.internal:1234/v1"
)
+ assert call_kwargs["check_embedding_ctx_length"] is False
def test_create_embeddings_error_message_mentions_base_url(self):
"""When both api_key and base_url are missing, the error must