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(routes): extract _research_not_found helper (DRY) (#4713)

LearningCircuit1e58038abb04
diff --git a/src/local_deep_research/web/routes/api_routes.py b/src/local_deep_research/web/routes/api_routes.py
index a1da380da8..9ab56aa1f4 100644
--- a/src/local_deep_research/web/routes/api_routes.py
+++ b/src/local_deep_research/web/routes/api_routes.py
@@ -14,6 +14,7 @@ from ...constants import ResearchStatus
from ...utilities.url_utils import normalize_url
from ...security.decorators import require_json_body
from ..auth.decorators import login_required
+from ..routes.research_routes import _research_not_found
from ..services.research_service import (
cancel_research,
)
@@ -104,7 +105,7 @@ def api_research_status(research_id):
)
if research is None:
- return jsonify({"error": "Research not found"}), 404
+ return _research_not_found(research_id)
# Extract attributes while session is active
# to avoid DetachedInstanceError after the with block exits
@@ -231,9 +232,7 @@ def api_add_resource(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ return _research_not_found(research_id)
# Add the resource
resource_id = add_resource(
diff --git a/src/local_deep_research/web/routes/history_routes.py b/src/local_deep_research/web/routes/history_routes.py
index 94665e7d42..dacd5c325c 100644
--- a/src/local_deep_research/web/routes/history_routes.py
+++ b/src/local_deep_research/web/routes/history_routes.py
@@ -18,6 +18,7 @@ from ..models.database import (
get_total_logs_for_research,
)
from ..routes.globals import get_active_research_snapshot
+from ..routes.research_routes import _research_not_found
from ..services.research_service import get_research_strategy
from ...security.rate_limiter import limiter
from ...security import filter_research_metadata
@@ -156,9 +157,7 @@ def get_research_status(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ return _research_not_found(research_id)
# Extract attributes while session is active
# to avoid DetachedInstanceError after the with block exits
@@ -220,9 +219,7 @@ def get_research_details(research_id):
if not research:
logger.error(f"Research not found for id: {research_id}")
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ return _research_not_found(research_id)
# Extract all needed attributes while session is active
# to avoid DetachedInstanceError after the with block exits
@@ -302,9 +299,7 @@ def get_report(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Report not found"}
- ), 404
+ return _research_not_found(research_id, message="Report not found")
try:
# research.report_content holds the answer-only string;
@@ -320,9 +315,9 @@ def get_report(research_id):
# above already returns 404 for that. An empty-but-found row
# (no body, no sources, no metrics) returns "" and is valid.
if content is None:
- return jsonify(
- {"status": "error", "message": "Report content not found"}
- ), 404
+ return _research_not_found(
+ research_id, message="Report content not found"
+ )
stored_metadata = research.research_meta or {}
@@ -372,9 +367,7 @@ def get_markdown(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Report not found"}
- ), 404
+ return _research_not_found(research_id, message="Report not found")
try:
from ..services.report_assembly_service import (
@@ -383,9 +376,9 @@ def get_markdown(research_id):
content = assemble_full_report(research, db_session)
if content is None:
- return jsonify(
- {"status": "error", "message": "Report content not found"}
- ), 404
+ return _research_not_found(
+ research_id, message="Report content not found"
+ )
return jsonify({"status": "success", "content": content})
except Exception:
@@ -424,9 +417,7 @@ def get_research_logs(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ return _research_not_found(research_id)
logs = get_logs_for_research(research_id, limit=limit)
diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.py
index 41b4d2fc34..0573e1b646 100644
--- a/src/local_deep_research/web/routes/research_routes.py
+++ b/src/local_deep_research/web/routes/research_routes.py
@@ -345,6 +345,22 @@ def _apply_policy_overrides(settings_snapshot, params):
)
+def _research_not_found(research_id, message="Research not found"):
+ """Return a consistent 404 JSON for a missing research.
+
+ Emits ``status``, ``error`` and ``message`` so the body is a strict
+ superset of both historical 404 shapes — every frontend reader and
+ existing test keeps working without changes:
+ - Shape A readers read ``data.error``
+ - Shape B readers read ``data.status`` (``== "error"``) and/or
+ ``data.message``
+ """
+ return (
+ jsonify({"status": "error", "error": message, "message": message}),
+ 404,
+ )
+
+
def _queue_research(
db_session: Session,
username,
@@ -979,9 +995,7 @@ def terminate_research(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ return _research_not_found(research_id)
status = research.status
@@ -1099,9 +1113,7 @@ def delete_research(research_id):
)
if not research:
- return jsonify(
- {"status": "error", "message": "Research not found"}
- ), 404
+ return _research_not_found(research_id)
status = research.status
report_path = research.report_path
@@ -1424,7 +1436,7 @@ def get_research_details(research_id):
)
if not research:
- return jsonify({"error": "Research not found"}), 404
+ return _research_not_found(research_id)
return jsonify(
{
@@ -1474,7 +1486,7 @@ def get_research_logs(research_id):
.first()
)
if not research:
- return jsonify({"error": "Research not found"}), 404
+ return _research_not_found(research_id)
# Get logs from research_logs table
log_query = db_session.query(ResearchLog).filter_by(
@@ -1537,7 +1549,7 @@ def get_research_report(research_id):
)
if research is None:
- return jsonify({"error": "Research not found"}), 404
+ return _research_not_found(research_id)
# Parse metadata if it exists
metadata = research.research_meta
@@ -1554,7 +1566,9 @@ def get_research_report(research_id):
# Only None means "research not found" — guarded above.
# Empty-but-found rows return "" and are valid responses.
if content is None:
- return jsonify({"error": "Report not found"}), 404
+ return _research_not_found(
+ research_id, message="Report not found"
+ )
# Sources live in the research_resources table, not research_meta.
# The post-refactor save path never writes the legacy
@@ -1628,7 +1642,7 @@ def export_research_report(research_id, format):
.first()
)
if not research:
- return jsonify({"error": "Research not found"}), 404
+ return _research_not_found(research_id)
# Build the full assembled report (answer + Sources +
# Metrics) so exporters get the same shape they did
@@ -1639,7 +1653,9 @@ def export_research_report(research_id, format):
report_content = assemble_full_report(research, db_session)
if report_content is None:
- return jsonify({"error": "Report content not found"}), 404
+ return _research_not_found(
+ research_id, message="Report content not found"
+ )
# Export to requested format (all in memory)
try:
@@ -1700,7 +1716,7 @@ def get_research_status(research_id):
)
if research is None:
- return jsonify({"error": "Research not found"}), 404
+ return _research_not_found(research_id)
status = research.status
progress = research.progress
@@ -1861,9 +1877,9 @@ def get_queue_position(research_id):
position = QueueManager.get_queue_position(username, research_id)
if position is None:
- return jsonify(
- {"status": "error", "message": "Research not found in queue"}
- ), 404
+ return _research_not_found(
+ research_id, message="Research not found in queue"
+ )
return jsonify({"status": "success", "position": position})
except Exception:
diff --git a/tests/web/routes/test_history_routes_extended.py b/tests/web/routes/test_history_routes_extended.py
index 7072e5b33d..620bd0ccc0 100644
--- a/tests/web/routes/test_history_routes_extended.py
+++ b/tests/web/routes/test_history_routes_extended.py
@@ -223,7 +223,12 @@ class TestGetResearchStatus:
assert response.status_code == 404
data = response.get_json()
+ # The shared _research_not_found helper emits a superset of both
+ # historical 404 shapes: Shape B readers rely on "status"/"message",
+ # Shape A readers rely on "error". All three must be present.
assert data["status"] == "error"
+ assert data["message"] == "Research not found"
+ assert data["error"] == "Research not found"
def test_returns_progress_from_active_research(self, app):
"""When research_id is in active_research, progress comes from there."""