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.

refactor(settings): drop dead hand-rolled model-listing, single discovery path (H7) (#4646)

LearningCircuit3576349643c3
diff --git a/changelog.d/4646.misc.md b/changelog.d/4646.misc.md
new file mode 100644
index 0000000000..78d8ab9b06
--- /dev/null
+++ b/changelog.d/4646.misc.md
@@ -0,0 +1 @@
+The available-models endpoint no longer hand-rolls Ollama/OpenAI/Anthropic model listing that was immediately overwritten by provider auto-discovery — auto-discovery is now the single fetch path. This removes a redundant per-refresh network round-trip to each provider, and local-only model listing now goes through the provider classes (which validate the URL for SSRF and send auth headers, so an auth-protected local Ollama now lists its models).
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 33928ec140..271635ff1f 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -1402,314 +1402,12 @@ def api_get_available_models():
logger.warning("Error reading cached models from database")
# Continue to fetch fresh data
- # Try to get Ollama models
- ollama_models = []
- try:
- import json
- import re
-
- import requests
-
- # Try to query the Ollama API directly
- try:
- logger.info("Attempting to connect to Ollama API")
-
- raw_base_url = _get_setting_from_session(
- "llm.ollama.url", DEFAULT_OLLAMA_URL
- )
- base_url = (
- normalize_url(raw_base_url)
- if raw_base_url
- else DEFAULT_OLLAMA_URL
- )
-
- ollama_fetch_start = time.perf_counter()
- ollama_response = safe_get(
- f"{base_url}/api/tags",
- timeout=5,
- allow_localhost=True,
- allow_private_ips=True,
- )
- ollama_fetch_ms = (
- time.perf_counter() - ollama_fetch_start
- ) * 1000
- if ollama_fetch_ms > 1000:
- logger.info(
- f"Ollama /api/tags fetch took {ollama_fetch_ms:.0f}ms"
- )
- else:
- logger.debug(
- f"Ollama /api/tags fetch took {ollama_fetch_ms:.0f}ms"
- )
-
- logger.debug(
- f"Ollama API response: Status {ollama_response.status_code}"
- )
-
- # Try to parse the response even if status code is not 200 to help with debugging
- response_text = ollama_response.text
- logger.debug(
- f"Ollama API raw response: {response_text[:500]}..."
- )
-
- if ollama_response.status_code == 200:
- try:
- ollama_data = ollama_response.json()
- logger.debug(
- f"Ollama API JSON data: {json.dumps(ollama_data)[:500]}..."
- )
-
- if "models" in ollama_data:
- # Format for newer Ollama API
- logger.info(
- f"Found {len(ollama_data.get('models', []))} models in newer Ollama API format"
- )
- for model in ollama_data.get("models", []):
- # Extract name correctly from the model object
- name = model.get("name", "")
- if name:
- # Improved display name formatting
- display_name = re.sub(
- r"[:/]", " ", name
- ).strip()
- display_name = " ".join(
- word.capitalize()
- for word in display_name.split()
- )
- # Create the model entry with value and label
- ollama_models.append(
- {
- "value": name, # Original model name as value (for API calls)
- "label": f"{display_name} (Ollama)", # Pretty name as label
- "provider": "ollama", # Add provider field for consistency
- }
- )
- logger.debug(
- f"Added Ollama model: {name} -> {display_name}"
- )
- else:
- # Format for older Ollama API
- logger.info(
- f"Found {len(ollama_data)} models in older Ollama API format"
- )
- for model in ollama_data:
- name = model.get("name", "")
- if name:
- # Improved display name formatting
- display_name = re.sub(
- r"[:/]", " ", name
- ).strip()
- display_name = " ".join(
- word.capitalize()
- for word in display_name.split()
- )
- ollama_models.append(
- {
- "value": name,
- "label": f"{display_name} (Ollama)",
- "provider": "ollama", # Add provider field for consistency
- }
- )
- logger.debug(
- f"Added Ollama model: {name} -> {display_name}"
- )
-
- except json.JSONDecodeError as json_err:
- logger.exception(
- f"Failed to parse Ollama API response as JSON: {json_err}"
- )
- raise ValueError(
- f"Ollama API returned invalid JSON: {json_err}"
- )
- else:
- logger.warning(
- f"Ollama API returned non-200 status code: {ollama_response.status_code}"
- )
- raise ValueError(
- f"Ollama API returned status code {ollama_response.status_code}"
- )
-
- except requests.exceptions.RequestException:
- logger.warning("Could not connect to Ollama API")
- # No fallback models - just return empty list
- logger.info("Ollama not available - no models to display")
- ollama_models = []
-
- # Always set the ollama_models in providers, whether we got real or fallback models
- providers["ollama_models"] = ollama_models
- logger.info(f"Final Ollama models count: {len(ollama_models)}")
-
- # Log some model names for debugging
- if ollama_models:
- model_names = [m["value"] for m in ollama_models[:5]]
- logger.info(f"Sample Ollama models: {', '.join(model_names)}")
-
- except Exception:
- logger.exception("Error getting Ollama models")
- # No fallback models - just return empty list
- logger.info("Error getting Ollama models - no models to display")
- providers["ollama_models"] = []
-
- # Note: OpenAI-Compatible Endpoint models are fetched via auto-discovery
- # (see the auto-discovery loop below which handles OPENAI_ENDPOINT provider)
-
- # Get OpenAI models using the OpenAI package
- openai_models = []
- try:
- logger.info(
- "Attempting to connect to OpenAI API using OpenAI package"
- )
-
- # Get the API key from settings
- api_key = _get_setting_from_session("llm.openai.api_key", "")
-
- # Egress policy: if the user is local-only, don't reach out to the
- # OpenAI model list (which sends the API key off-machine). Gate on
- # the EFFECTIVE posture — the require_local toggle OR a private
- # egress scope — so a PRIVATE_ONLY user who never ticked the toggle
- # still doesn't leak. Return an empty list so the UI degrades
- # gracefully (the dropdown just shows no OpenAI models).
- require_local = _model_list_local_only()
- if require_local:
- logger.bind(policy_audit=True).info(
- "skipping OpenAI model list (local-only egress posture)"
- )
- api_key = ""
-
- if api_key:
- import openai
- from openai import OpenAI
-
- # Create OpenAI client
- client = OpenAI(api_key=api_key)
-
- try:
- # Fetch models using the client
- logger.debug("Fetching models from OpenAI API")
- openai_fetch_start = time.perf_counter()
- models_response = client.models.list()
- openai_fetch_ms = (
- time.perf_counter() - openai_fetch_start
- ) * 1000
- if openai_fetch_ms > 1000:
- logger.info(
- f"OpenAI models.list() took {openai_fetch_ms:.0f}ms"
- )
- else:
- logger.debug(
- f"OpenAI models.list() took {openai_fetch_ms:.0f}ms"
- )
-
- # Process models from the response
- for model in models_response.data:
- model_id = model.id
- if model_id:
- # Create a clean display name
- display_name = model_id.replace("-", " ").strip()
- display_name = " ".join(
- word.capitalize()
- for word in display_name.split()
- )
-
- openai_models.append(
- {
- "value": model_id,
- "label": f"{display_name} (OpenAI)",
- "provider": "openai",
- }
- )
- logger.debug(
- f"Added OpenAI model: {model_id} -> {display_name}"
- )
-
- # Keep original order from OpenAI - their models are returned in a
- # meaningful order (newer/more capable models first)
-
- except openai.APIError as api_err:
- logger.exception(f"OpenAI API error: {api_err!s}")
- logger.info("No OpenAI models found due to API error")
-
- else:
- logger.info(
- "OpenAI API key not configured, no models available"
- )
-
- except Exception:
- logger.exception("Error getting OpenAI models")
- logger.info("No OpenAI models available due to error")
-
- # Always set the openai_models in providers (will be empty array if no models found)
- providers["openai_models"] = openai_models
- logger.info(f"Final OpenAI models count: {len(openai_models)}")
-
- # Try to get Anthropic models using the Anthropic package
- anthropic_models = []
- try:
- logger.info(
- "Attempting to connect to Anthropic API using Anthropic package"
- )
-
- # Get the API key from settings
- api_key = _get_setting_from_session("llm.anthropic.api_key", "")
-
- # Egress policy: skip the Anthropic model list (cloud-only) when
- # the effective posture is local-only (toggle OR private scope).
- require_local = _model_list_local_only()
- if require_local:
- logger.bind(policy_audit=True).info(
- "skipping Anthropic model list (local-only egress posture)"
- )
- api_key = ""
-
- if api_key:
- # Import Anthropic package here to avoid dependency issues if not installed
- from anthropic import Anthropic
-
- # Create Anthropic client
- anthropic_client = Anthropic(api_key=api_key)
-
- try:
- # Fetch models using the client
- logger.debug("Fetching models from Anthropic API")
- models_response = anthropic_client.models.list()
-
- # Process models from the response
- for model in models_response.data:
- model_id = model.id
- if model_id:
- # Create a clean display name
- display_name = model_id.replace("-", " ").strip()
- display_name = " ".join(
- word.capitalize()
- for word in display_name.split()
- )
-
- anthropic_models.append(
- {
- "value": model_id,
- "label": f"{display_name} (Anthropic)",
- "provider": "anthropic",
- }
- )
- logger.debug(
- f"Added Anthropic model: {model_id} -> {display_name}"
- )
-
- except Exception as api_err:
- logger.exception(f"Anthropic API error: {api_err!s}")
- else:
- logger.info("Anthropic API key not configured")
-
- except ImportError:
- logger.warning(
- "Anthropic package not installed. No models will be available."
- )
- except Exception:
- logger.exception("Error getting Anthropic models")
-
- # Set anthropic_models in providers (could be empty if API call failed)
- providers["anthropic_models"] = anthropic_models
- logger.info(f"Final Anthropic models count: {len(anthropic_models)}")
+ # Ollama / OpenAI / Anthropic model listing is handled by the
+ # auto-discovery loop below (their provider classes' list_models_for_api),
+ # which is the single fetch path. The previous hand-rolled per-provider
+ # blocks here were dead in normal mode (overwritten by discovery) and an
+ # inferior duplicate (no SSRF validation, no auth headers); local-only mode
+ # now keeps the local providers via the LOCAL_PROVIDERS filter below.
# Fetch models from auto-discovered providers
from ...llm.providers import discover_providers
@@ -1717,21 +1415,34 @@ def api_get_available_models():
discovered_providers = discover_providers()
# Egress policy: when the effective posture is local-only the user
- # has opted into local-only inference; skip every auto-discovered
- # provider list (OpenRouter, Google, XAI, IonOS, ...). Provider
- # URLs are not all classifiable cheaply here, so we fail closed
- # — the user can still see ollama/lmstudio/llamacpp models from
- # the dedicated local provider branches above. Use the scope-aware
- # helper (matching the OpenAI/Anthropic branches) so a PRIVATE_ONLY /
- # adaptive-private user is covered even if the raw toggle is False.
+ # has opted into local-only inference, so don't list cloud providers
+ # (OpenRouter, Google, XAI, IonOS, OpenAI, Anthropic, ...). We still
+ # list the known-local providers (ollama/llamacpp/lmstudio) via their
+ # provider classes — which validate the URL (SSRF) and support auth
+ # headers — by filtering the discovered set to LOCAL_PROVIDERS rather
+ # than skipping discovery entirely. Use the scope-aware helper so a
+ # PRIVATE_ONLY / adaptive-private user is covered even if the raw
+ # toggle is False.
require_local_for_discovered = _model_list_local_only()
if require_local_for_discovered:
+ from ...llm.providers._helpers import LOCAL_PROVIDERS
+
+ local_only_providers = {
+ key: info
+ for key, info in discovered_providers.items()
+ if normalize_provider(key) in LOCAL_PROVIDERS
+ }
logger.bind(policy_audit=True).info(
- "skipping auto-discovered provider model lists "
- "(effective local-only egress posture)",
- providers=list(discovered_providers.keys()),
+ "local-only egress posture: limiting discovered model lists "
+ "to local providers",
+ kept=list(local_only_providers.keys()),
+ skipped=[
+ key
+ for key in discovered_providers
+ if key not in local_only_providers
+ ],
)
- discovered_providers = {}
+ discovered_providers = local_only_providers
for provider_key, provider_info in discovered_providers.items():
provider_models = []
diff --git a/tests/web/routes/test_settings_routes_gaps_coverage.py b/tests/web/routes/test_settings_routes_gaps_coverage.py
index 2bd534017c..584fa82e9c 100644
--- a/tests/web/routes/test_settings_routes_gaps_coverage.py
+++ b/tests/web/routes/test_settings_routes_gaps_coverage.py
@@ -762,3 +762,85 @@ class TestApiGetAvailableModels:
assert values.count("LLAMACPP") == 1
# No provider value should be duplicated.
assert len(values) == len(set(values))
+
+ @staticmethod
+ def _discovered(name, models):
+ """Build a discovered-provider info whose class returns ``models``."""
+ provider_class = MagicMock()
+ provider_class.api_key_setting = "x.api_key"
+ provider_class.url_setting = None # falsy -> no base-url fetch
+ provider_class.list_models_for_api.return_value = models
+ info = MagicMock()
+ info.provider_name = name
+ info.provider_class = provider_class
+ return info
+
+ def _run_models(self, local_only):
+ app = _create_test_app()
+
+ @contextmanager
+ def _fake_session(*a, **kw):
+ yield MagicMock()
+
+ discovered = {
+ "OLLAMA": self._discovered(
+ "Ollama", [{"value": "llama3", "label": "llama3 (Ollama)"}]
+ ),
+ "OPENAI": self._discovered(
+ "OpenAI", [{"value": "gpt-4", "label": "GPT-4 (OpenAI)"}]
+ ),
+ }
+
+ with _authenticated_client(app) as client:
+ with (
+ patch(
+ f"{MODULE}.get_user_db_session", side_effect=_fake_session
+ ),
+ patch(
+ "local_deep_research.llm.providers.get_discovered_provider_options",
+ return_value=[],
+ ),
+ patch(
+ "local_deep_research.llm.providers.discover_providers",
+ return_value=discovered,
+ ),
+ patch(
+ f"{MODULE}._model_list_local_only", return_value=local_only
+ ),
+ patch(f"{MODULE}._get_setting_from_session", return_value=""),
+ ):
+ resp = client.get(
+ "/settings/api/available-models?force_refresh=true"
+ )
+ assert resp.status_code == 200
+ return resp.get_json()["providers"]
+
+ def test_non_local_lists_all_discovered_providers(self):
+ """Normal posture: discovery is the single path and lists every
+ provider (incl. Ollama via its provider class — no hand-rolled block)."""
+ providers = self._run_models(local_only=False)
+ assert providers["ollama_models"] == [
+ {
+ "value": "llama3",
+ "label": "llama3 (Ollama)",
+ "provider": "OLLAMA",
+ }
+ ]
+ assert providers["openai_models"] == [
+ {"value": "gpt-4", "label": "GPT-4 (OpenAI)", "provider": "OPENAI"}
+ ]
+
+ def test_local_only_lists_only_local_providers(self):
+ """Local-only posture: discovery is filtered to LOCAL_PROVIDERS, so
+ Ollama is still listed (via its provider class) but the cloud provider
+ is not — replacing the removed hand-rolled local-only fallback."""
+ providers = self._run_models(local_only=True)
+ assert providers["ollama_models"] == [
+ {
+ "value": "llama3",
+ "label": "llama3 (Ollama)",
+ "provider": "OLLAMA",
+ }
+ ]
+ # Cloud provider filtered out entirely (key absent, not just empty).
+ assert "openai_models" not in providers