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.

Centralize boolean conversion logic in utilities/type_utils.py

LearningCircuita6fc0fc14ec2
diff --git a/src/local_deep_research/api/settings_utils.py b/src/local_deep_research/api/settings_utils.py
index 47719e2218..01bd2ea7bc 100644
--- a/src/local_deep_research/api/settings_utils.py
+++ b/src/local_deep_research/api/settings_utils.py
@@ -13,6 +13,7 @@ from loguru import logger
from ..settings import SettingsManager
from ..settings.base import ISettingsManager
+from ..utilities.type_utils import to_bool # noqa: F401
class InMemorySettingsManager(ISettingsManager):
@@ -332,51 +333,6 @@ def extract_setting_value(
return default
-def to_bool(value: Any, default: bool = False) -> bool:
- """
- Convert a value to boolean, handling string representations.
-
- This is a standalone utility for converting any value to boolean,
- centralizing the string-to-boolean conversion logic that was
- previously scattered throughout the codebase.
-
- Handles truthy string representations that may come from:
- - API requests
- - Configuration files
- - SQLite (which lacks native boolean type)
- - Environment variables
-
- Args:
- value: The value to convert
- default: Default boolean if value is None
-
- Returns:
- Boolean value
-
- Examples:
- >>> to_bool("true")
- True
- >>> to_bool("yes")
- True
- >>> to_bool("1")
- True
- >>> to_bool("false")
- False
- >>> to_bool(1)
- True
- >>> to_bool(None, default=True)
- True
- """
- if isinstance(value, bool):
- return value
- if isinstance(value, str):
- return value.lower() in ("true", "1", "yes", "on", "enabled")
- if value is None:
- return default
- # For other types (int, etc.), use Python's bool conversion
- return bool(value)
-
-
def extract_bool_setting(
settings_snapshot: Dict[str, Any], key: str, default: bool = False
) -> bool:
diff --git a/src/local_deep_research/config/thread_settings.py b/src/local_deep_research/config/thread_settings.py
index 7f990fa186..d6f8878537 100644
--- a/src/local_deep_research/config/thread_settings.py
+++ b/src/local_deep_research/config/thread_settings.py
@@ -8,6 +8,7 @@ import os
import threading
from ..settings.manager import get_typed_setting_value
+from ..utilities.type_utils import to_bool
class NoSettingsContextError(Exception):
@@ -157,11 +158,4 @@ def get_bool_setting_from_snapshot(
check_fallback_llm=check_fallback_llm,
)
- if isinstance(value, bool):
- return value
- if isinstance(value, str):
- return value.lower() in ("true", "1", "yes", "on", "enabled")
- if value is None:
- return default
- # For other types (int, etc.), use Python's bool conversion
- return bool(value)
+ return to_bool(value, default)
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.py
index 0923875a1a..a15925ff12 100644
--- a/src/local_deep_research/research_library/services/library_rag_service.py
+++ b/src/local_deep_research/research_library/services/library_rag_service.py
@@ -26,6 +26,7 @@ from ...database.models.library import (
EmbeddingProvider,
)
from ...database.session_context import get_user_db_session
+from ...utilities.type_utils import to_bool
from ...embeddings.splitters import get_text_splitter
from ...web_search_engines.engines.search_engine_local import (
LocalEmbeddingManager,
@@ -90,8 +91,6 @@ class LibraryRAGService:
)
self.distance_metric = distance_metric
# Ensure normalize_vectors is always a proper boolean
- from local_deep_research.api.settings_utils import to_bool
-
self.normalize_vectors = to_bool(normalize_vectors, default=True)
self.index_type = index_type
diff --git a/src/local_deep_research/utilities/type_utils.py b/src/local_deep_research/utilities/type_utils.py
new file mode 100644
index 0000000000..b9fee88f2e
--- /dev/null
+++ b/src/local_deep_research/utilities/type_utils.py
@@ -0,0 +1,54 @@
+"""
+Type conversion utilities.
+
+This module provides type conversion functions that are used throughout
+the codebase. It is intentionally kept free of internal dependencies to
+avoid circular import issues.
+"""
+
+from typing import Any
+
+
+def to_bool(value: Any, default: bool = False) -> bool:
+ """
+ Convert a value to boolean, handling string representations.
+
+ This is a standalone utility for converting any value to boolean,
+ centralizing the string-to-boolean conversion logic that was
+ previously scattered throughout the codebase.
+
+ Handles truthy string representations that may come from:
+ - API requests
+ - Configuration files
+ - SQLite (which lacks native boolean type)
+ - Environment variables
+
+ Args:
+ value: The value to convert
+ default: Default boolean if value is None
+
+ Returns:
+ Boolean value
+
+ Examples:
+ >>> to_bool("true")
+ True
+ >>> to_bool("yes")
+ True
+ >>> to_bool("1")
+ True
+ >>> to_bool("false")
+ False
+ >>> to_bool(1)
+ True
+ >>> to_bool(None, default=True)
+ True
+ """
+ if isinstance(value, bool):
+ return value
+ if isinstance(value, str):
+ return value.lower() in ("true", "1", "yes", "on", "enabled")
+ if value is None:
+ return default
+ # For other types (int, etc.), use Python's bool conversion
+ return bool(value)
diff --git a/src/local_deep_research/web/services/settings_manager.py b/src/local_deep_research/web/services/settings_manager.py
index 0146381cb3..f6d3e5555d 100644
--- a/src/local_deep_research/web/services/settings_manager.py
+++ b/src/local_deep_research/web/services/settings_manager.py
@@ -10,6 +10,7 @@ from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from ... import defaults
+from ...utilities.type_utils import to_bool
from ...__version__ import __version__ as package_version
from ...database.models import Setting, SettingType
from ..models.settings import (
@@ -313,14 +314,7 @@ class SettingsManager:
"""
value = self.get_setting(key, default, check_env)
- if isinstance(value, bool):
- return value
- if isinstance(value, str):
- return value.lower() in ("true", "1", "yes", "on", "enabled")
- if value is None:
- return default
- # For other types (int, etc.), use Python's bool conversion
- return bool(value)
+ return to_bool(value, default)
def set_setting(self, key: str, value: Any, commit: bool = True) -> bool:
"""