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(llm): add _get_optional_setting helper for NoSettingsContextError pattern (DRY) (#4707)

LearningCircuit5165a94ab00c
diff --git a/src/local_deep_research/config/thread_settings.py b/src/local_deep_research/config/thread_settings.py
index fb029fc6f3..05c0a3a320 100644
--- a/src/local_deep_research/config/thread_settings.py
+++ b/src/local_deep_research/config/thread_settings.py
@@ -178,3 +178,58 @@ def get_bool_setting_from_snapshot(
)
return to_bool(value, default)
+
+
+def _get_optional_setting(
+ param_dict,
+ param_name,
+ setting_key,
+ settings_snapshot,
+ *,
+ cast=None,
+ check="not_none",
+):
+ """Fetch a setting and, when present, write it into ``param_dict``.
+
+ Consolidates the recurring
+ ``try: get_setting_from_snapshot(...) ... except NoSettingsContextError: pass``
+ pattern used by the LLM provider ``create_llm`` factories.
+ ``NoSettingsContextError`` is silently swallowed because these
+ parameters are always optional.
+
+ Args:
+ param_dict: Target dict (e.g. ``llm_params``) mutated in place.
+ param_name: Key to set on ``param_dict`` when a value is present.
+ setting_key: Dotted settings path to retrieve.
+ settings_snapshot: Snapshot dict passed through to
+ :func:`get_setting_from_snapshot`.
+ cast: Optional callable applied to the value before assignment
+ (e.g. ``int`` for ``max_tokens``).
+ check: ``"not_none"`` (default) writes the value when it is not
+ ``None``. ``"falsy"`` writes only when the value is truthy.
+ The falsy mode is an intentional safeguard for fields where
+ a falsy stored value (``organization=""``) must be dropped
+ rather than forwarded.
+
+ The ``max_tokens`` blocks that resolve their value through
+ ``compute_max_tokens`` / ``get_context_window_for_provider`` (rather
+ than a single settings lookup) do not fit this helper and are left
+ in place. The ``api_base`` block in ``openai.py`` is also left in
+ place because it wraps an SSRF validation side effect.
+ """
+ try:
+ value = get_setting_from_snapshot(
+ setting_key,
+ default=None,
+ settings_snapshot=settings_snapshot,
+ )
+ except NoSettingsContextError:
+ return
+ if check == "not_none":
+ if value is not None:
+ param_dict[param_name] = cast(value) if cast else value
+ elif check == "falsy":
+ if value:
+ param_dict[param_name] = cast(value) if cast else value
+ else:
+ raise ValueError(f"Unknown check mode: {check!r}")
diff --git a/src/local_deep_research/llm/providers/implementations/openai.py b/src/local_deep_research/llm/providers/implementations/openai.py
index 34e3e79a63..6426154898 100644
--- a/src/local_deep_research/llm/providers/implementations/openai.py
+++ b/src/local_deep_research/llm/providers/implementations/openai.py
@@ -44,6 +44,7 @@ class OpenAIProvider(OpenAICompatibleProvider):
ValueError: If API key is not configured
"""
from ....config.thread_settings import (
+ _get_optional_setting,
get_setting_from_snapshot,
NoSettingsContextError,
)
@@ -93,49 +94,36 @@ class OpenAIProvider(OpenAICompatibleProvider):
except NoSettingsContextError:
pass # Optional parameter
- try:
- organization = get_setting_from_snapshot(
- "llm.openai.organization",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if organization:
- openai_params["openai_organization"] = organization
- except NoSettingsContextError:
- pass # Optional parameter
+ # organization uses the falsy check intentionally — an empty string
+ # must be dropped rather than forwarded to the OpenAI client.
+ _get_optional_setting(
+ openai_params,
+ "openai_organization",
+ "llm.openai.organization",
+ settings_snapshot,
+ check="falsy",
+ )
- try:
- streaming = get_setting_from_snapshot(
- "llm.streaming",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if streaming is not None:
- openai_params["streaming"] = streaming
- except NoSettingsContextError:
- pass # Optional parameter
+ _get_optional_setting(
+ openai_params,
+ "streaming",
+ "llm.streaming",
+ settings_snapshot,
+ )
- try:
- max_retries = get_setting_from_snapshot(
- "llm.max_retries",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if max_retries is not None:
- openai_params["max_retries"] = max_retries
- except NoSettingsContextError:
- pass # Optional parameter
+ _get_optional_setting(
+ openai_params,
+ "max_retries",
+ "llm.max_retries",
+ settings_snapshot,
+ )
- try:
- request_timeout = get_setting_from_snapshot(
- "llm.request_timeout",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if request_timeout is not None:
- openai_params["request_timeout"] = request_timeout
- except NoSettingsContextError:
- pass # Optional parameter
+ _get_optional_setting(
+ openai_params,
+ "request_timeout",
+ "llm.request_timeout",
+ settings_snapshot,
+ )
# Apply context-window-aware max_tokens cap (was previously only
# applied in dead code in llm_config.get_llm).
diff --git a/src/local_deep_research/llm/providers/openai_base.py b/src/local_deep_research/llm/providers/openai_base.py
index 04d8781cb9..022170bccf 100644
--- a/src/local_deep_research/llm/providers/openai_base.py
+++ b/src/local_deep_research/llm/providers/openai_base.py
@@ -46,7 +46,7 @@ class OpenAICompatibleProvider(BaseLLMProvider):
ValueError: If API key is not configured
"""
from ...config.thread_settings import (
- get_setting_from_snapshot,
+ _get_optional_setting,
NoSettingsContextError,
)
@@ -110,40 +110,28 @@ class OpenAICompatibleProvider(BaseLLMProvider):
pass # Optional parameter
# Add streaming if specified
- try:
- streaming = get_setting_from_snapshot(
- "llm.streaming",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if streaming is not None:
- llm_params["streaming"] = streaming
- except NoSettingsContextError:
- pass # Optional parameter
+ _get_optional_setting(
+ llm_params,
+ "streaming",
+ "llm.streaming",
+ settings_snapshot,
+ )
# Add max_retries if specified
- try:
- max_retries = get_setting_from_snapshot(
- "llm.max_retries",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if max_retries is not None:
- llm_params["max_retries"] = max_retries
- except NoSettingsContextError:
- pass # Optional parameter
+ _get_optional_setting(
+ llm_params,
+ "max_retries",
+ "llm.max_retries",
+ settings_snapshot,
+ )
# Add request_timeout if specified
- try:
- request_timeout = get_setting_from_snapshot(
- "llm.request_timeout",
- default=None,
- settings_snapshot=settings_snapshot,
- )
- if request_timeout is not None:
- llm_params["request_timeout"] = request_timeout
- except NoSettingsContextError:
- pass # Optional parameter
+ _get_optional_setting(
+ llm_params,
+ "request_timeout",
+ "llm.request_timeout",
+ settings_snapshot,
+ )
# Request usage stats on streamed responses (stream_options.
# include_usage). Opt-in via subclass kwargs because some