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: Extract and share duplicated get_setting_from_snapshot function

LearningCircuitc73cbc4d1e30
diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.py
index 1c1e8d5b01..03b99d138a 100644
--- a/src/local_deep_research/config/llm_config.py
+++ b/src/local_deep_research/config/llm_config.py
@@ -11,7 +11,9 @@ from loguru import logger
from ..llm import get_llm_from_registry, is_llm_registered
from ..utilities.search_utilities import remove_think_tags
from ..utilities.url_utils import normalize_url
-from .thread_settings import _thread_local
+from .thread_settings import (
+ get_setting_from_snapshot as _get_setting_from_snapshot,
+)
# Valid provider options
VALID_PROVIDERS = [
@@ -29,32 +31,12 @@ VALID_PROVIDERS = [
def get_setting_from_snapshot(
key, default=None, username=None, settings_snapshot=None
):
- """Get setting from context only - no database access from threads."""
- # First check if we have settings_snapshot passed directly
- if settings_snapshot and key in settings_snapshot:
- value = settings_snapshot[key]
- # Extract value from dict structure if needed
- if isinstance(value, dict) and "value" in value:
- return value["value"]
- return value
-
- # Check if we have a settings context in this thread
- if (
- hasattr(_thread_local, "settings_context")
- and _thread_local.settings_context
- ):
- return _thread_local.settings_context.get_setting(key, default)
+ """Get setting from context only - no database access from threads.
- # In CI/test environment with fallback LLM, return default values
- if os.environ.get("LDR_USE_FALLBACK_LLM", ""):
- logger.debug(
- f"Using default value for {key} in fallback LLM environment"
- )
- return default
-
- # If no context, raise an error - threads should never access database directly
- raise RuntimeError(
- f"No settings context available in thread for key '{key}'. All settings must be passed via settings_snapshot."
+ This is a wrapper around the shared function that enables fallback LLM check.
+ """
+ return _get_setting_from_snapshot(
+ key, default, username, settings_snapshot, check_fallback_llm=True
)
diff --git a/src/local_deep_research/config/search_config.py b/src/local_deep_research/config/search_config.py
index cd71b44bc0..6f412e375d 100644
--- a/src/local_deep_research/config/search_config.py
+++ b/src/local_deep_research/config/search_config.py
@@ -5,35 +5,13 @@ from ..web_search_engines.search_engine_factory import (
get_search as factory_get_search,
)
from .llm_config import get_llm
-from .thread_settings import _thread_local
+from .thread_settings import get_setting_from_snapshot
# Whether to check the quality search results using the LLM.
QUALITY_CHECK_DDG_URLS = True
-def get_setting_from_snapshot(
- key, default=None, username=None, settings_snapshot=None
-):
- """Get setting from context only - no database access from threads."""
- # First check if we have settings_snapshot passed directly
- if settings_snapshot and key in settings_snapshot:
- value = settings_snapshot[key]
- # Extract value from dict structure if needed
- if isinstance(value, dict) and "value" in value:
- return value["value"]
- return value
-
- # Then check if we have a settings context in this thread
- if (
- hasattr(_thread_local, "settings_context")
- and _thread_local.settings_context
- ):
- return _thread_local.settings_context.get_setting(key, default)
-
- # If no context, raise an error - threads should never access database directly
- raise RuntimeError(
- f"No settings context available in thread for key '{key}'. All settings must be passed via settings_snapshot."
- )
+# get_setting_from_snapshot is now imported from thread_settings
def get_search_snippets_only_setting(username=None):
diff --git a/src/local_deep_research/config/thread_settings.py b/src/local_deep_research/config/thread_settings.py
index 59949ba44a..8e19743a00 100644
--- a/src/local_deep_research/config/thread_settings.py
+++ b/src/local_deep_research/config/thread_settings.py
@@ -4,6 +4,7 @@ This module provides a single thread-local storage instance that can be
shared across different modules to maintain settings context in threads.
"""
+import os
import threading
# Shared thread-local storage for settings context
@@ -20,3 +21,55 @@ def get_settings_context():
if hasattr(_thread_local, "settings_context"):
return _thread_local.settings_context
return None
+
+
+def get_setting_from_snapshot(
+ key,
+ default=None,
+ username=None,
+ settings_snapshot=None,
+ check_fallback_llm=False,
+):
+ """Get setting from context only - no database access from threads.
+
+ Args:
+ key: Setting key to retrieve
+ default: Default value if setting not found
+ username: Username (unused, kept for backward compatibility)
+ settings_snapshot: Optional settings snapshot dict
+ check_fallback_llm: Whether to check LDR_USE_FALLBACK_LLM env var
+
+ Returns:
+ Setting value or default
+
+ Raises:
+ RuntimeError: If no settings context is available
+ """
+ # First check if we have settings_snapshot passed directly
+ if settings_snapshot and key in settings_snapshot:
+ value = settings_snapshot[key]
+ # Extract value from dict structure if needed
+ if isinstance(value, dict) and "value" in value:
+ return value["value"]
+ return value
+
+ # Check if we have a settings context in this thread
+ if (
+ hasattr(_thread_local, "settings_context")
+ and _thread_local.settings_context
+ ):
+ return _thread_local.settings_context.get_setting(key, default)
+
+ # In CI/test environment with fallback LLM, return default values
+ if check_fallback_llm and os.environ.get("LDR_USE_FALLBACK_LLM", ""):
+ from loguru import logger
+
+ logger.debug(
+ f"Using default value for {key} in fallback LLM environment"
+ )
+ return default
+
+ # If no context, raise an error - threads should never access database directly
+ raise RuntimeError(
+ f"No settings context available in thread for key '{key}'. All settings must be passed via settings_snapshot."
+ )