~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(llm): fail loud when an OpenAI-compatible provider omits provider_key (#5175)
Sebastian Cao6a00e635b08c
diff --git a/changelog.d/4546.bugfix.md b/changelog.d/4546.bugfix.mdnew file mode 100644index 0000000000..805b6e17de--- /dev/null+++ b/changelog.d/4546.bugfix.md@@ -0,0 +1 @@+OpenAI-compatible LLM providers now fail loud when a provider subclass omits the required `provider_key`: context-window resolution reads `cls.provider_key` directly instead of silently defaulting to `""`, which previously fell through into cloud context-window resolution. A misconfigured custom provider now raises `AttributeError` at construction rather than getting the wrong context window.diff --git a/src/local_deep_research/llm/providers/openai_base.py b/src/local_deep_research/llm/providers/openai_base.pyindex 7422b4839a..edf536e7aa 100644--- a/src/local_deep_research/llm/providers/openai_base.py+++ b/src/local_deep_research/llm/providers/openai_base.py@@ -22,6 +22,11 @@ class OpenAICompatibleProvider(BaseLLMProvider):"""# Override these in subclasses+ # Context-window / auto-discovery key (e.g. "OPENAI"). Annotated without+ # a value on purpose: a subclass that omits it raises AttributeError at+ # construction instead of silently resolving the cloud context window+ # (issue #4546).+ provider_key: strprovider_name = "openai_endpoint" # Name used in logsapi_key_setting = "llm.openai_endpoint.api_key" # Settings key for API keyurl_setting = None # Settings key for URL (e.g., "llm.lmstudio.url")@@ -97,7 +102,7 @@ class OpenAICompatibleProvider(BaseLLMProvider):try:context_window_size = get_context_window_for_provider(- getattr(cls, "provider_key", "").lower(),+ cls.provider_key.lower(),settings_snapshot=settings_snapshot,)max_tokens = compute_max_tokens(@@ -198,7 +203,7 @@ class OpenAICompatibleProvider(BaseLLMProvider):try:context_window_size = get_context_window_for_provider(- getattr(cls, "provider_key", "").lower(),+ cls.provider_key.lower(),settings_snapshot=settings_snapshot,)max_tokens = compute_max_tokens(diff --git a/tests/llm/providers/test_openai_base.py b/tests/llm/providers/test_openai_base.pyindex 2f157b806f..f96772d8cc 100644--- a/tests/llm/providers/test_openai_base.py+++ b/tests/llm/providers/test_openai_base.py@@ -13,6 +13,24 @@ from unittest.mock import Mock, patchimport pytest[email protected](autouse=True)+def _supply_base_provider_key(monkeypatch):+ """Supply a provider_key on the abstract base for these direct tests.++ OpenAICompatibleProvider intentionally declares no ``provider_key``+ (issue #4546) so concrete subclasses that forget one fail loudly instead+ of silently getting cloud context-window resolution. These tests drive+ the base class directly, so supply the key the way every registered+ provider does. ``"openai_endpoint"`` keeps the pre-fix cloud routing.+ """+ monkeypatch.setattr(+ "local_deep_research.llm.providers.openai_base."+ "OpenAICompatibleProvider.provider_key",+ "openai_endpoint",+ raising=False,+ )++class TestOpenAICompatibleProviderAttributes:"""Tests for OpenAICompatibleProvider class attributes."""diff --git a/tests/llm/providers/test_openai_base_deep_coverage.py b/tests/llm/providers/test_openai_base_deep_coverage.pyindex 282dd71924..ac26890078 100644--- a/tests/llm/providers/test_openai_base_deep_coverage.py+++ b/tests/llm/providers/test_openai_base_deep_coverage.py@@ -19,6 +19,24 @@ MODULE = "local_deep_research.llm.providers.openai_base"SETTINGS_MODULE = "local_deep_research.config.thread_settings"[email protected](autouse=True)+def _supply_base_provider_key(monkeypatch):+ """Supply a provider_key on the abstract base for these direct tests.++ OpenAICompatibleProvider intentionally declares no ``provider_key``+ (issue #4546) so concrete subclasses that forget one fail loudly instead+ of silently getting cloud context-window resolution. These tests drive+ the base class (and throwaway subclasses of it) directly, so supply the+ key the way every registered provider does. ``"openai_endpoint"`` keeps+ the pre-fix cloud routing.+ """+ monkeypatch.setattr(+ f"{MODULE}.OpenAICompatibleProvider.provider_key",+ "openai_endpoint",+ raising=False,+ )++class TestCreateLlmApiKeyHandling:@patch(f"{MODULE}.ChatOpenAI")@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot")diff --git a/tests/llm/test_openai_base.py b/tests/llm/test_openai_base.pyindex 4b67f95c01..ede2dd4ba2 100644--- a/tests/llm/test_openai_base.py+++ b/tests/llm/test_openai_base.py@@ -12,6 +12,24 @@ from unittest.mock import Mock, patchimport pytest[email protected](autouse=True)+def _supply_base_provider_key(monkeypatch):+ """Supply a provider_key on the abstract base for these direct tests.++ OpenAICompatibleProvider intentionally declares no ``provider_key``+ (issue #4546) so concrete subclasses that forget one fail loudly instead+ of silently getting cloud context-window resolution. These tests drive+ the base class directly, so supply the key the way every registered+ provider does. ``"openai_endpoint"`` keeps the pre-fix cloud routing.+ """+ monkeypatch.setattr(+ "local_deep_research.llm.providers.openai_base."+ "OpenAICompatibleProvider.provider_key",+ "openai_endpoint",+ raising=False,+ )++class TestOpenAICompatibleProviderInit:"""Tests for OpenAICompatibleProvider class attributes."""diff --git a/tests/llm/test_provider_base_url_ssrf.py b/tests/llm/test_provider_base_url_ssrf.pyindex 6bea532ace..10be86b2d3 100644--- a/tests/llm/test_provider_base_url_ssrf.py+++ b/tests/llm/test_provider_base_url_ssrf.py@@ -24,6 +24,25 @@ from unittest.mock import MagicMock, Mockimport pytest[email protected](autouse=True)+def _supply_base_provider_key(monkeypatch):+ """Supply a provider_key on the abstract base for these direct tests.++ OpenAICompatibleProvider intentionally declares no ``provider_key``+ (issue #4546) so concrete subclasses that forget one fail loudly instead+ of silently getting cloud context-window resolution. Some tests here+ drive throwaway subclasses of it directly, so supply the key the way+ every registered provider does. ``"openai_endpoint"`` keeps the pre-fix+ cloud routing and does not affect the SSRF guard under test.+ """+ monkeypatch.setattr(+ "local_deep_research.llm.providers.openai_base."+ "OpenAICompatibleProvider.provider_key",+ "openai_endpoint",+ raising=False,+ )++@pytest.mark.parametrize("ip",[diff --git a/tests/llm_providers/test_openai_base.py b/tests/llm_providers/test_openai_base.pyindex 6451cd6472..b60133fcf8 100644--- a/tests/llm_providers/test_openai_base.py+++ b/tests/llm_providers/test_openai_base.py@@ -10,6 +10,24 @@ from local_deep_research.llm.providers.openai_base import ()[email protected](autouse=True)+def _supply_base_provider_key(monkeypatch):+ """Supply a provider_key on the abstract base for these direct tests.++ OpenAICompatibleProvider intentionally declares no ``provider_key``+ (issue #4546) so concrete subclasses that forget one fail loudly instead+ of silently getting cloud context-window resolution. These tests drive+ the base class directly, so supply the key the way every registered+ provider does. ``"openai_endpoint"`` keeps the pre-fix cloud routing.+ """+ monkeypatch.setattr(+ OpenAICompatibleProvider,+ "provider_key",+ "openai_endpoint",+ raising=False,+ )++class TestOpenAICompatibleProviderMetadata:"""Tests for OpenAICompatibleProvider class metadata."""diff --git a/tests/llm_providers/test_openai_base_coverage.py b/tests/llm_providers/test_openai_base_coverage.pyindex 90d24f4f20..f87fd3bc12 100644--- a/tests/llm_providers/test_openai_base_coverage.py+++ b/tests/llm_providers/test_openai_base_coverage.py@@ -25,6 +25,24 @@ from local_deep_research.llm.providers.openai_base import ()[email protected](autouse=True)+def _supply_base_provider_key(monkeypatch):+ """Supply a provider_key on the abstract base for these direct tests.++ OpenAICompatibleProvider intentionally declares no ``provider_key``+ (issue #4546) so concrete subclasses that forget one fail loudly instead+ of silently getting cloud context-window resolution. The throwaway+ subclasses below omit it, so supply the key the way every registered+ provider does. ``"openai_endpoint"`` keeps the pre-fix cloud routing.+ """+ monkeypatch.setattr(+ OpenAICompatibleProvider,+ "provider_key",+ "openai_endpoint",+ raising=False,+ )++# ---------------------------------------------------------------------------# Subclass helpers for testing# ---------------------------------------------------------------------------diff --git a/tests/llm_providers/test_provider_key_required.py b/tests/llm_providers/test_provider_key_required.pynew file mode 100644index 0000000000..e3b35594b7--- /dev/null+++ b/tests/llm_providers/test_provider_key_required.py@@ -0,0 +1,53 @@+"""Regression test for issue #4546.++``OpenAICompatibleProvider.create_llm`` / ``_create_llm_instance`` resolve the+context window from ``cls.provider_key``. Previously they used+``getattr(cls, "provider_key", "")`` which silently yielded ``""`` for a+subclass missing ``provider_key`` — falling through into the cloud+context-window branch instead of failing. A subclass that forgets to declare+``provider_key`` must now fail loudly (``AttributeError``) at construction.+"""++from unittest.mock import patch++import pytest++from local_deep_research.llm.providers.openai_base import (+ OpenAICompatibleProvider,+)++MODULE = "local_deep_research.llm.providers.openai_base"+SETTINGS_MODULE = "local_deep_research.config.thread_settings"+++class _ProviderWithoutKey(OpenAICompatibleProvider):+ """A misconfigured provider that omits the required provider_key."""++ provider_name = "no_provider_key"+ # Keyless so construction reaches context-window resolution rather than+ # raising earlier for a missing API key.+ api_key_setting = None+ default_base_url = "http://localhost:1234/v1"+ default_model = "test-model"+++@patch(f"{MODULE}.ChatOpenAI")+@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot", return_value=None)+def test_create_llm_without_provider_key_raises(mock_get_setting, mock_chat):+ """create_llm fails loud when provider_key is missing (issue #4546)."""+ with pytest.raises(AttributeError):+ _ProviderWithoutKey.create_llm(+ model_name="test-model", settings_snapshot={}+ )+++@patch(f"{MODULE}.ChatOpenAI")+@patch(f"{SETTINGS_MODULE}.get_setting_from_snapshot", return_value=None)+def test_create_llm_instance_without_provider_key_raises(+ mock_get_setting, mock_chat+):+ """_create_llm_instance fails loud when provider_key is missing."""+ with pytest.raises(AttributeError):+ _ProviderWithoutKey._create_llm_instance(+ model_name="test-model", settings_snapshot={}+ )