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.

fix(llm): honor explicit OpenAI endpoint URL (#5383)

Ziyang Guo733b20a0d808
diff --git a/changelog.d/+openai-endpoint-override.bugfix.md b/changelog.d/+openai-endpoint-override.bugfix.md
new file mode 100644
index 0000000000..813477c330
--- /dev/null
+++ b/changelog.d/+openai-endpoint-override.bugfix.md
@@ -0,0 +1 @@
+Honor per-call OpenAI-compatible endpoint URL overrides in research and benchmark paths.
diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.py
index f1f5882527..9a70c88a21 100644
--- a/src/local_deep_research/config/llm_config.py
+++ b/src/local_deep_research/config/llm_config.py
@@ -67,13 +67,9 @@ def get_llm(
model_name: Name of the model to use (if None, uses database setting)
temperature: Model temperature (if None, uses database setting)
provider: Provider to use (if None, uses database setting)
- openai_endpoint_url: NON-FUNCTIONAL, kept for API compatibility.
- The endpoint URL is read exclusively from the
- ``llm.openai_endpoint.url`` setting by
- CustomOpenAIEndpointProvider. This parameter was already
- ignored before the procedural-chain removal (the registry
- dispatch never consumed it); honoring or removing it is
- tracked as a follow-up.
+ openai_endpoint_url: Per-call URL override for the
+ ``openai_endpoint`` provider. When omitted, the provider uses
+ the ``llm.openai_endpoint.url`` setting.
research_id: Optional research ID for token tracking
research_context: Optional research context for enhanced token tracking
@@ -106,6 +102,22 @@ def get_llm(
# Normalize provider: convert to lowercase canonical form
provider = normalize_provider(provider)
+ had_settings_snapshot = settings_snapshot is not None
+
+ # The endpoint override must reach both the egress-policy gate and the
+ # provider factory. Overlay it onto a per-call copy so those paths share
+ # one effective value without mutating the caller's settings snapshot.
+ if provider == "openai_endpoint" and openai_endpoint_url is not None:
+ if (
+ not isinstance(openai_endpoint_url, str)
+ or not openai_endpoint_url.strip()
+ ):
+ raise ValueError("openai_endpoint_url must be a non-empty string")
+ settings_snapshot = dict(settings_snapshot or {})
+ settings_snapshot["llm.openai_endpoint.url"] = (
+ openai_endpoint_url.strip()
+ )
+
# Egress policy PEP for LLM endpoints. Fires here (before the registered-
# LLM dispatch) because all built-in providers are auto-registered via
# discover_providers(), so the registered branch handles every real LLM
@@ -120,6 +132,8 @@ def get_llm(
if provider:
from ..security.egress.policy import (
Decision,
+ EgressContext,
+ EgressScope,
PolicyDeniedError,
_LOCAL_DEFAULT_LLM_PROVIDERS,
_is_user_registered_llm,
@@ -128,11 +142,31 @@ def get_llm(
resolve_run_primary_engine,
)
- if settings_snapshot is None:
+ if not had_settings_snapshot:
+ if provider == "openai_endpoint" and settings_snapshot is not None:
+ # A per-call endpoint is enough to classify this otherwise
+ # snapshot-less provider. Preserve the fail-closed contract:
+ # only a local endpoint may proceed without a policy snapshot.
+ ctx = EgressContext(
+ scope=EgressScope.PRIVATE_ONLY,
+ primary_engine="snapshotless-explicit-endpoint",
+ require_local_llm=True,
+ require_local_embeddings=True,
+ )
+ decision = evaluate_llm_endpoint(
+ provider, ctx, settings_snapshot=settings_snapshot
+ )
+ if not decision.allowed:
+ logger.bind(policy_audit=True).warning(
+ "Snapshot-less explicit LLM endpoint denied",
+ provider=provider,
+ reason=decision.reason,
+ )
+ raise PolicyDeniedError(decision, target=provider)
# User-registered in-process LLMs are exempt here for the same
# reason evaluate_llm_endpoint allows them: no endpoint to
# classify, operator-injected, audit-hook backstopped.
- if provider not in _LOCAL_DEFAULT_LLM_PROVIDERS and not (
+ elif provider not in _LOCAL_DEFAULT_LLM_PROVIDERS and not (
_is_user_registered_llm(provider)
):
logger.bind(policy_audit=True).warning(
diff --git a/tests/config/test_get_llm_dispatch_integration.py b/tests/config/test_get_llm_dispatch_integration.py
index 8ff0c4bc1a..8b467f5b4a 100644
--- a/tests/config/test_get_llm_dispatch_integration.py
+++ b/tests/config/test_get_llm_dispatch_integration.py
@@ -120,6 +120,70 @@ class TestCloudProviderDispatch:
)
+class TestOpenAIEndpointOverride:
+ def test_explicit_url_overrides_snapshot_without_mutating_it(self):
+ snapshot = _snapshot(
+ **{
+ "llm.openai_endpoint.url": "http://localhost:9000/v1",
+ }
+ )
+
+ with patch(OPENAI_BASE_CHAT, return_value=_fake_llm()) as mock_chat:
+ get_llm(
+ provider="openai_endpoint",
+ model_name="custom-model",
+ openai_endpoint_url="http://localhost:8000/v1",
+ settings_snapshot=snapshot,
+ )
+
+ assert (
+ mock_chat.call_args.kwargs["base_url"] == "http://localhost:8000/v1"
+ )
+ assert snapshot["llm.openai_endpoint.url"] == "http://localhost:9000/v1"
+
+ def test_explicit_local_url_works_without_snapshot(self):
+ with patch(OPENAI_BASE_CHAT, return_value=_fake_llm()) as mock_chat:
+ get_llm(
+ provider="openai_endpoint",
+ model_name="custom-model",
+ openai_endpoint_url="http://localhost:8000/v1",
+ settings_snapshot=None,
+ )
+
+ assert (
+ mock_chat.call_args.kwargs["base_url"] == "http://localhost:8000/v1"
+ )
+
+ def test_snapshot_url_is_used_when_override_is_omitted(self):
+ snapshot = _snapshot(
+ **{
+ "llm.openai_endpoint.url": "http://localhost:9000/v1",
+ }
+ )
+
+ with patch(OPENAI_BASE_CHAT, return_value=_fake_llm()) as mock_chat:
+ get_llm(
+ provider="openai_endpoint",
+ model_name="custom-model",
+ settings_snapshot=snapshot,
+ )
+
+ assert (
+ mock_chat.call_args.kwargs["base_url"] == "http://localhost:9000/v1"
+ )
+
+ def test_empty_explicit_url_is_rejected(self):
+ with pytest.raises(
+ ValueError, match="openai_endpoint_url must be a non-empty string"
+ ):
+ get_llm(
+ provider="openai_endpoint",
+ model_name="custom-model",
+ openai_endpoint_url=" ",
+ settings_snapshot=_snapshot(),
+ )
+
+
class TestLocalProviderDispatch:
def test_lmstudio_appends_v1_suffix(self):
"""#4532: a URL without /v1 gets the suffix on the live path."""
diff --git a/tests/security/test_egress_pep_llm_embeddings_callsite.py b/tests/security/test_egress_pep_llm_embeddings_callsite.py
index 1347e70ee7..99777ab44f 100644
--- a/tests/security/test_egress_pep_llm_embeddings_callsite.py
+++ b/tests/security/test_egress_pep_llm_embeddings_callsite.py
@@ -201,6 +201,57 @@ class TestGetLlmLocalAllowed:
mock_registry.assert_not_called()
+class TestGetLlmExplicitEndpointPolicy:
+ def test_explicit_remote_url_overrides_local_snapshot_before_policy_gate(
+ self,
+ ):
+ snapshot = {
+ **PRIVATE_ONLY,
+ "llm.openai_endpoint.url": "http://localhost:8000/v1",
+ }
+
+ def classify_host(host, _ctx):
+ return host == "localhost"
+
+ with patch(
+ "local_deep_research.config.llm_config.get_llm_from_registry"
+ ) as mock_registry:
+ mock_registry.return_value = Mock(spec=BaseChatModel)
+ with patch(
+ "local_deep_research.security.egress.policy._classify_host",
+ side_effect=classify_host,
+ ):
+ with pytest.raises(PolicyDeniedError) as exc:
+ get_llm(
+ provider="openai_endpoint",
+ model_name="custom-model",
+ openai_endpoint_url="https://api.example.com/v1",
+ settings_snapshot=snapshot,
+ )
+
+ assert exc.value.decision.reason == "provider_remote"
+ mock_registry.assert_not_called()
+
+ def test_snapshotless_explicit_remote_url_remains_denied(self):
+ with patch(
+ "local_deep_research.config.llm_config.get_llm_from_registry"
+ ) as mock_registry:
+ with patch(
+ "local_deep_research.security.egress.policy._classify_host",
+ return_value=False,
+ ):
+ with pytest.raises(PolicyDeniedError) as exc:
+ get_llm(
+ provider="openai_endpoint",
+ model_name="custom-model",
+ openai_endpoint_url="https://api.example.com/v1",
+ settings_snapshot=None,
+ )
+
+ assert exc.value.decision.reason == "provider_remote"
+ mock_registry.assert_not_called()
+
+
# ---------------------------------------------------------------------------
# get_llm — user-registered in-process LLM is exempt (allowed)
# ---------------------------------------------------------------------------