~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(db): break domain_classifier.models ↔ database.models circular import (#4910)
LearningCircuit99aa3523b1ec
diff --git a/changelog.d/4910.bugfix.md b/changelog.d/4910.bugfix.mdnew file mode 100644index 0000000000..0d43f2e509--- /dev/null+++ b/changelog.d/4910.bugfix.md@@ -0,0 +1 @@+Fixed a circular import that made `import local_deep_research.domain_classifier.models` (and importing the `domain_classifier` package before `database.models`) crash with `ImportError: cannot import name 'DomainClassification' from partially initialized module`. The declarative SQLAlchemy `Base` now lives in a dependency-free `database.base` leaf that model modules import without triggering the full `database.models` package, so import order no longer matters. `Base` identity and `Base.metadata` are unchanged.diff --git a/src/local_deep_research/database/base.py b/src/local_deep_research/database/base.pynew file mode 100644index 0000000000..7be229bfb0--- /dev/null+++ b/src/local_deep_research/database/base.py@@ -0,0 +1,19 @@+"""Canonical SQLAlchemy declarative ``Base`` for the application schema.++This lives in a dependency-free leaf (it imports only SQLAlchemy) so that model+modules — including ones in *other* top-level packages, e.g.+``domain_classifier.models`` — can import ``Base`` without pulling in the+``database.models`` package. ``database.models`` eagerly imports every model to+register it on ``Base.metadata``; if a model reached ``Base`` *through* that+package it would create an import cycle (``domain_classifier.models`` ->+``database.models`` -> ``domain_classifier.models``). Importing from here avoids+that entirely.++``database.models.base`` re-exports this same object for backwards+compatibility, so ``from .base import Base`` inside the models package keeps+working and there is exactly one ``Base`` / one ``metadata``.+"""++from sqlalchemy.orm import declarative_base++Base = declarative_base()diff --git a/src/local_deep_research/database/models/base.py b/src/local_deep_research/database/models/base.pyindex cfdf47c2c5..8d33421db1 100644--- a/src/local_deep_research/database/models/base.py+++ b/src/local_deep_research/database/models/base.py@@ -1,7 +1,14 @@"""Base model configuration for SQLAlchemy.++The canonical ``Base`` now lives in the dependency-free ``database.base`` leaf so+that models in other top-level packages (e.g. ``domain_classifier.models``) can+import it without triggering the ``database.models`` package ``__init__`` — which+eagerly imports every model and would otherwise form an import cycle. This module+re-exports the same object, so ``from .base import Base`` throughout the models+package is unchanged and there is exactly one ``Base`` / one ``metadata``."""-from sqlalchemy.orm import declarative_base+from ..base import Base-Base = declarative_base()+__all__ = ["Base"]diff --git a/src/local_deep_research/domain_classifier/models.py b/src/local_deep_research/domain_classifier/models.pyindex 824713ff45..33d9758624 100644--- a/src/local_deep_research/domain_classifier/models.py+++ b/src/local_deep_research/domain_classifier/models.py@@ -2,7 +2,14 @@from sqlalchemy import Column, String, Text, Float, Integer, Indexfrom sqlalchemy_utc import UtcDateTime, utcnow-from ..database.models import Base++# Import Base from the dependency-free database.base leaf, NOT the+# ..database.models package. database.models eagerly imports every model —+# including this one — so importing Base through it would re-enter a+# partially-initialized database.models and raise "cannot import name+# 'DomainClassification'". database.base holds the same Base object, so metadata+# registration is unchanged. Regression: tests/test_domain_classifier_no_import_cycle.py+from ..database.base import Baseclass DomainClassification(Base):diff --git a/tests/test_domain_classifier_no_import_cycle.py b/tests/test_domain_classifier_no_import_cycle.pynew file mode 100644index 0000000000..acdec9b22f--- /dev/null+++ b/tests/test_domain_classifier_no_import_cycle.py@@ -0,0 +1,62 @@+"""Regression: ``domain_classifier.models`` must import without a cycle.++``domain_classifier.models`` defines the ``DomainClassification`` model and+needs SQLAlchemy's ``Base``. It used to import ``Base`` from the+``..database.models`` *package*, whose ``__init__`` eagerly imports every model+— including ``DomainClassification`` back from this module. Importing+``domain_classifier.models`` first (before ``database.models`` was cached)+crashed at import time::++ ImportError: cannot import name 'DomainClassification' from partially+ initialized module 'local_deep_research.domain_classifier.models'++The fix imports ``Base`` from the dependency-free ``database.base`` leaf+instead. Because ``database`` is a namespace package (no ``__init__.py``),+importing ``database.base`` never triggers ``database.models.__init__`` — so the+eager re-import of ``DomainClassification`` is never reached. (Importing from+``database.models.base`` would NOT work: pulling any submodule of+``database.models`` first runs that package's ``__init__``, re-triggering the+cycle.) These checks import the cycle-critical modules in FRESH interpreters —+pytest's module cache would mask a real cycle within a single process (whatever+imports ``database.models`` first hides it) — and assert they load cleanly.+"""++import subprocess+import sys++import pytest++# Each statement must import on its own in a clean interpreter. The first is the+# one that regressed; the others guard the surrounding cycle members.+CYCLE_CRITICAL = [+ "import local_deep_research.domain_classifier.models",+ "from local_deep_research.domain_classifier.models import DomainClassification",+ "import local_deep_research.database.models",+ "import local_deep_research.domain_classifier",+]++[email protected]("stmt", CYCLE_CRITICAL)+def test_cycle_critical_module_imports_clean(stmt):+ result = subprocess.run(+ [sys.executable, "-c", stmt],+ capture_output=True,+ text=True,+ timeout=120,+ )+ assert result.returncode == 0, (+ "Import failed (possible reintroduced circular import):\n"+ f" statement: {stmt}\n{result.stderr}"+ )+++def test_domain_classification_shares_the_canonical_base():+ """The model must register on the same Base as the rest of the schema."""+ from local_deep_research.database.models import Base+ from local_deep_research.database.models.base import Base as LeafBase+ from local_deep_research.domain_classifier.models import (+ DomainClassification,+ )++ assert Base is LeafBase+ assert DomainClassification.__table__.metadata is Base.metadata