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: treat empty environment variables as unset to fix provider selection (#3362)

LearningCircuit83f632e0699d
diff --git a/docs/deployment/unraid.md b/docs/deployment/unraid.md
index e071ee0d74..eeddd4139b 100644
--- a/docs/deployment/unraid.md
+++ b/docs/deployment/unraid.md
@@ -163,6 +163,10 @@ Configure these based on your setup:
| `LDR_LLM_ANTHROPIC_API_KEY` | Lock Anthropic API key |
| `LDR_LLM_GOOGLE_API_KEY` | Lock Google API key |
+> **Important:** Unraid templates create all environment variables even when fields are left blank. Empty values (`""`) are treated as **not set** — they will not override or lock anything. Only non-empty values act as overrides. This means leaving a field blank in the Unraid template is safe and equivalent to not having the variable at all.
+>
+> **Security note:** Setting an API key variable to an empty string does **not** block or clear it. If a key is already stored in the database, it will still be used even when the env var is empty. To explicitly block a key, set it to any non-empty invalid value (e.g., `DISABLED`).
+
**Recommendation:** Configure these via the WebUI Settings page instead of environment variables for easier management.
### Network Configuration
diff --git a/docs/docker-compose-guide.md b/docs/docker-compose-guide.md
index 7cf97875d3..b8c6e229b9 100644
--- a/docs/docker-compose-guide.md
+++ b/docs/docker-compose-guide.md
@@ -53,6 +53,8 @@ The base configuration includes:
Most settings can be configured through the web UI at http://localhost:5000/settings. Environment variables **override** UI settings and lock them. For the complete list of all environment variables and their defaults, see [CONFIGURATION.md](CONFIGURATION.md).
> **⚠️ Warning:** Setting environment variables causes a hard override—the setting becomes read-only in the UI and cannot be changed until the environment variable is removed. For settings you may want to adjust later, use the web UI instead. Environment variables are best suited for deployment-specific values like `LDR_DATA_DIR` or API keys.
+>
+> **Note:** Empty environment variables (e.g., `LDR_LLM_PROVIDER=` or `LDR_LLM_PROVIDER=""`) are treated as **not set** — they will not override anything and the setting remains editable in the UI. This prevents Docker Compose templates with blank fields from accidentally locking settings. Only non-empty values act as overrides. Setting an API key to an empty string does **not** block or clear it — if a key exists in the database, it will still be used. To explicitly block a key, set it to any non-empty invalid value (e.g., `DISABLED`).
| Variable | Description |
|----------|-------------|
diff --git a/docs/env_configuration.md b/docs/env_configuration.md
index edf5c92764..d491959a22 100644
--- a/docs/env_configuration.md
+++ b/docs/env_configuration.md
@@ -25,12 +25,16 @@ For example:
- `search.tool` → `LDR_SEARCH_TOOL`
> **Important:** Environment variables **override** UI settings and lock them — the setting becomes read-only in the UI until the environment variable is removed. For settings you may want to adjust later, use the Web UI instead.
+>
+> **Note on empty values:** Empty environment variables (e.g., `LDR_LLM_PROVIDER=""`) are treated as **not set** — they will not override anything and the setting remains editable in the UI. This is by design: deployment tools like Unraid and Docker Compose templates often create all environment variables even when fields are left blank. Only non-empty values act as overrides. To remove an override, delete the environment variable entirely rather than setting it to an empty string. To explicitly block a setting, set it to any non-empty invalid value (e.g., `DISABLED`).
For the complete list of all settings, their environment variable names, and default values, see [CONFIGURATION.md](CONFIGURATION.md).
## API Keys
-API keys are best set using environment variables for security. Only the `LDR_` prefixed version is needed:
+API keys are best set using environment variables for security. Only the `LDR_` prefixed version is needed.
+
+> **Security note:** Setting an API key variable to an empty string (e.g., `LDR_LLM_OPENAI_API_KEY=""`) does **not** block or clear the key — it is treated as unset, and the key remains editable in the UI. If a key is already stored in the database, it will still be used. To explicitly block a key, set it to any non-empty invalid value (e.g., `DISABLED`).
```bash
# LLM API keys
diff --git a/src/local_deep_research/settings/manager.py b/src/local_deep_research/settings/manager.py
index 27836873c4..a5b741fa81 100644
--- a/src/local_deep_research/settings/manager.py
+++ b/src/local_deep_research/settings/manager.py
@@ -255,14 +255,37 @@ def check_env_setting(key: str) -> str | None:
Returns:
The setting from the environment variables, or None if the variable
- is not set.
+ is not set or is empty.
+
+ Note:
+ Empty environment variables ("") are treated as unset. This is standard
+ practice across the ecosystem — see CPython's official docs (PYTHON*
+ env vars require "a non-empty string"), botocore PR #1687, Pallets/Click
+ PR #2223, and Vercel Turborepo PR #6929. Orchestration tools like Unraid,
+ Terraform, and Kubernetes manifests often cannot conditionally omit env
+ var declarations, so they pass "" for unconfigured values. Treating ""
+ as unset prevents these empty strings from overriding database defaults.
+ See: https://github.com/LearningCircuit/local-deep-research/pull/3362
"""
env_variable_name = f"LDR_{'_'.join(key.split('.')).upper()}"
env_value = os.getenv(env_variable_name)
- if env_value is not None:
+ # Treat empty string as unset — orchestration tools (Unraid, Terraform, K8s)
+ # often cannot omit env var declarations and pass "" for unconfigured values.
+ if env_value is not None and env_value != "":
logger.debug(f"Overriding {key} setting from environment variable.")
- return env_value
+ return env_value
+ if env_value == "":
+ logger.warning(
+ "Environment variable {} is set but empty — "
+ "ignoring it and falling back to DB/default for setting '{}'. "
+ "This is expected on Unraid or Docker templates that create "
+ "all variables even when left blank. To suppress this warning, "
+ "remove the variable from your environment or set a value.",
+ env_variable_name,
+ key,
+ )
+ return None
class SettingsManager(ISettingsManager):
diff --git a/tests/settings/test_manager_behavior.py b/tests/settings/test_manager_behavior.py
index bf283025d4..878e1d699d 100644
--- a/tests/settings/test_manager_behavior.py
+++ b/tests/settings/test_manager_behavior.py
@@ -387,6 +387,53 @@ class TestCheckEnvSetting:
result = check_env_setting("search.brave.api_key")
assert result == "brave-key-123"
+ def test_returns_none_for_empty_string(self):
+ """Empty string env var is treated as unset, returns None, and logs warning."""
+ from loguru import logger
+
+ from local_deep_research.settings.manager import check_env_setting
+
+ warnings = []
+ logger.enable("local_deep_research")
+ sink_id = logger.add(
+ lambda msg: warnings.append(str(msg)), level="WARNING"
+ )
+ try:
+ with patch.dict(os.environ, {"LDR_LLM_PROVIDER": ""}):
+ result = check_env_setting("llm.provider")
+ assert result is None
+ assert any("LDR_LLM_PROVIDER" in w for w in warnings)
+ finally:
+ logger.remove(sink_id)
+ logger.disable("local_deep_research")
+
+ def test_empty_string_provider_key(self):
+ """Empty LDR_LLM_PROVIDER returns None (Unraid scenario)."""
+ from local_deep_research.settings.manager import check_env_setting
+
+ with patch.dict(os.environ, {"LDR_LLM_PROVIDER": ""}):
+ result = check_env_setting("llm.provider")
+ assert result is None
+
+ def test_empty_string_model_key(self):
+ """Empty LDR_LLM_MODEL returns None."""
+ from local_deep_research.settings.manager import check_env_setting
+
+ with patch.dict(os.environ, {"LDR_LLM_MODEL": ""}):
+ result = check_env_setting("llm.model")
+ assert result is None
+
+ def test_empty_string_multiple_keys(self):
+ """Multiple empty env vars each independently return None."""
+ from local_deep_research.settings.manager import check_env_setting
+
+ with patch.dict(
+ os.environ,
+ {"LDR_LLM_PROVIDER": "", "LDR_LLM_MODEL": ""},
+ ):
+ assert check_env_setting("llm.provider") is None
+ assert check_env_setting("llm.model") is None
+
class TestUIElementToSettingType:
"""Tests for UI_ELEMENT_TO_SETTING_TYPE mapping."""
diff --git a/tests/web/services/test_settings_manager_extended.py b/tests/web/services/test_settings_manager_extended.py
index 76c154a43e..e34ee42dba 100644
--- a/tests/web/services/test_settings_manager_extended.py
+++ b/tests/web/services/test_settings_manager_extended.py
@@ -374,7 +374,7 @@ class TestEnvironmentVariableEdgeCases:
del os.environ["LDR_LLM_TEMPERATURE"]
def test_env_override_empty_string(self):
- """Empty string env var is used as override."""
+ """Empty string env var is treated as unset (Docker/Unraid templates create blank entries)."""
from local_deep_research.settings.manager import (
SettingsManager,
)
@@ -392,10 +392,81 @@ class TestEnvironmentVariableEdgeCases:
manager = SettingsManager(db_session=mock_session)
result = manager.get_setting("llm.model", check_env=True)
- assert result == ""
+ # Empty env var should NOT override the DB value
+ assert result == "gpt-4"
finally:
del os.environ["LDR_LLM_MODEL"]
+ def test_env_override_empty_string_no_db_value(self):
+ """Empty env var with no DB value falls through to default.
+
+ This is the first-launch Unraid scenario: env vars are empty strings,
+ no DB row exists yet, so the default should be returned.
+ """
+ from local_deep_research.settings.manager import (
+ SettingsManager,
+ )
+
+ mock_session = Mock()
+ mock_query = Mock()
+ # No DB rows match this key
+ mock_query.filter.return_value.all.return_value = []
+ mock_session.query.return_value = mock_query
+
+ os.environ["LDR_LLM_PROVIDER"] = ""
+ try:
+ manager = SettingsManager(db_session=mock_session)
+ result = manager.get_setting(
+ "llm.provider", default="openai", check_env=True
+ )
+ assert result == "openai"
+ finally:
+ del os.environ["LDR_LLM_PROVIDER"]
+
+ def test_env_override_empty_string_checkbox(self):
+ """Empty env var for checkbox setting falls through to DB value."""
+ from local_deep_research.settings.manager import (
+ SettingsManager,
+ )
+
+ mock_session = Mock()
+ mock_query = Mock()
+ mock_setting = MockSetting(
+ key="app.debug", value=True, ui_element="checkbox"
+ )
+ mock_query.filter.return_value.all.return_value = [mock_setting]
+ mock_session.query.return_value = mock_query
+
+ os.environ["LDR_APP_DEBUG"] = ""
+ try:
+ manager = SettingsManager(db_session=mock_session)
+ result = manager.get_setting("app.debug", check_env=True)
+ assert result is True
+ finally:
+ del os.environ["LDR_APP_DEBUG"]
+
+ def test_env_override_empty_string_number(self):
+ """Empty env var for number setting falls through to DB value."""
+ from local_deep_research.settings.manager import (
+ SettingsManager,
+ )
+
+ mock_session = Mock()
+ mock_query = Mock()
+ mock_setting = MockSetting(
+ key="llm.temperature", value=0.7, ui_element="number"
+ )
+ mock_query.filter.return_value.all.return_value = [mock_setting]
+ mock_session.query.return_value = mock_query
+
+ os.environ["LDR_LLM_TEMPERATURE"] = ""
+ try:
+ manager = SettingsManager(db_session=mock_session)
+ result = manager.get_setting("llm.temperature", check_env=True)
+ assert result == 0.7
+ finally:
+ del os.environ["LDR_LLM_TEMPERATURE"]
+
def test_env_override_with_nested_key(self):
"""Nested key with multiple dots converts correctly."""
from local_deep_research.settings.manager import (