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(engines): unify API key placeholder list (DRY) (#4715)

LearningCircuite86951e420d6
diff --git a/src/local_deep_research/web_search_engines/search_engine_base.py b/src/local_deep_research/web_search_engines/search_engine_base.py
index a4ed9b66c8..132fc34c2c 100644
--- a/src/local_deep_research/web_search_engines/search_engine_base.py
+++ b/src/local_deep_research/web_search_engines/search_engine_base.py
@@ -24,6 +24,51 @@ from .rate_limiting import RateLimitError, get_tracker
from ..constants import DEFAULT_SEARCH_TOOL
+# Common placeholder values that should never be treated as a real API key.
+# Three call sites previously had inconsistent subsets of this list, which
+# let invalid placeholders silently through the production path in
+# search_engines_config.py. Centralizing here so all three stay in sync.
+API_KEY_PLACEHOLDERS = frozenset(
+ {
+ "",
+ "None",
+ "null",
+ "PLACEHOLDER",
+ "YOUR_API_KEY_HERE",
+ "YOUR_API_KEY",
+ "API_KEY",
+ "your_api_key",
+ "your-api-key",
+ }
+)
+
+
+def _is_api_key_placeholder(api_key: Optional[str]) -> bool:
+ """Return True if ``api_key`` looks like a placeholder, not a real key.
+
+ Catches:
+ - Exact-match placeholders (see ``API_KEY_PLACEHOLDERS``)
+ - Environment-variable-style names ending in ``_API_KEY``
+ (e.g. ``BRAVE_API_KEY``)
+ - Templates starting with ``YOUR_``
+ - Angle-bracket templates: ``<key>`` or ``${KEY}``
+ """
+ if not api_key:
+ return True
+ api_key = api_key.strip()
+ if api_key in API_KEY_PLACEHOLDERS:
+ return True
+ if api_key.endswith("_API_KEY"):
+ return True
+ if api_key.startswith("YOUR_"):
+ return True
+ if api_key.startswith("<") and api_key.endswith(">"):
+ return True
+ if api_key.startswith("${") and api_key.endswith("}"):
+ return True
+ return False
+
+
class AdaptiveWait(wait_base):
"""Custom wait strategy that uses adaptive rate limiting."""
@@ -234,15 +279,7 @@ class BaseSearchEngine(ABC):
api_key = config.get("api_key", "").strip()
# Check for common placeholder values
- if (
- not api_key
- or api_key in ["", "None", "PLACEHOLDER", "YOUR_API_KEY_HERE"]
- or api_key.endswith(
- "_API_KEY"
- ) # Default placeholders like BRAVE_API_KEY
- or api_key.startswith("YOUR_")
- or api_key == "null"
- ):
+ if _is_api_key_placeholder(api_key):
logger.debug(
f"Skipping {name} - requires API key but none configured"
)
@@ -795,42 +832,7 @@ class BaseSearchEngine(ABC):
>>> BaseSearchEngine._is_valid_api_key("YOUR_API_KEY_HERE")
False
"""
- if not api_key:
- return False
-
- api_key = api_key.strip()
-
- # Empty or whitespace-only
- if not api_key:
- return False
-
- # Common placeholder values
- placeholders = {
- "",
- "None",
- "null",
- "PLACEHOLDER",
- "YOUR_API_KEY_HERE",
- "YOUR_API_KEY",
- "API_KEY",
- "your_api_key",
- "your-api-key",
- }
-
- if api_key in placeholders:
- return False
-
- # Patterns that indicate placeholders
- if api_key.endswith("_API_KEY"):
- return False
- if api_key.startswith("YOUR_"):
- return False
- if api_key.startswith("<") and api_key.endswith(">"):
- return False
- if api_key.startswith("${") and api_key.endswith("}"):
- return False
-
- return True
+ return not _is_api_key_placeholder(api_key)
@staticmethod
def _extract_display_link(url: str, fallback: str = "") -> str:
diff --git a/src/local_deep_research/web_search_engines/search_engines_config.py b/src/local_deep_research/web_search_engines/search_engines_config.py
index 50aa9d881f..ebee4c2c91 100644
--- a/src/local_deep_research/web_search_engines/search_engines_config.py
+++ b/src/local_deep_research/web_search_engines/search_engines_config.py
@@ -10,6 +10,7 @@ from loguru import logger
from ..config.thread_settings import get_setting_from_snapshot
from ..utilities.db_utils import get_settings_manager
+from .search_engine_base import _is_api_key_placeholder
def _get_setting(
@@ -371,12 +372,7 @@ def _resolve_api_key(
# Reject common placeholder values
api_key_str = str(api_key).strip()
- if (
- not api_key_str
- or api_key_str in ("None", "PLACEHOLDER", "YOUR_API_KEY_HERE", "null")
- or api_key_str.endswith("_API_KEY")
- or api_key_str.startswith("YOUR_")
- ):
+ if _is_api_key_placeholder(api_key_str):
return None
return api_key_str