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: Enhance error handling in research service with better error detection and fallback mechanisms

hashedviking9441ce972c9d
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index f12d400e4c..2927a308b5 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -288,7 +288,8 @@ def run_research_process(
settings.llm.openai_endpoint_url = custom_endpoint
# Get LLM with the overridden settings
- system.model = get_llm()
+ # Explicitly create the model with parameters to avoid fallback issues
+ system.model = get_llm(model_name=model, provider=model_provider)
# Restore original settings
settings.llm.model = original_model
@@ -423,6 +424,72 @@ def run_research_process(
if isinstance(
raw_formatted_findings, str
) and raw_formatted_findings.startswith("Error:"):
+ logger.warning(
+ f"Detected error in formatted findings: {raw_formatted_findings[:100]}..."
+ )
+
+ # Determine error type for better user feedback
+ error_type = "unknown"
+ error_message = raw_formatted_findings.lower()
+
+ if (
+ "token limit" in error_message
+ or "context length" in error_message
+ ):
+ error_type = "token_limit"
+ # Log specific error type
+ logger.warning("Detected token limit error in synthesis")
+
+ # Update progress with specific error type
+ progress_callback(
+ "Synthesis hit token limits. Attempting fallback...",
+ 87,
+ {"phase": "synthesis_error", "error_type": error_type},
+ )
+ elif "timeout" in error_message or "timed out" in error_message:
+ error_type = "timeout"
+ logger.warning("Detected timeout error in synthesis")
+ progress_callback(
+ "Synthesis timed out. Attempting fallback...",
+ 87,
+ {"phase": "synthesis_error", "error_type": error_type},
+ )
+ elif "rate limit" in error_message:
+ error_type = "rate_limit"
+ logger.warning("Detected rate limit error in synthesis")
+ progress_callback(
+ "LLM rate limit reached. Attempting fallback...",
+ 87,
+ {"phase": "synthesis_error", "error_type": error_type},
+ )
+ elif "connection" in error_message or "network" in error_message:
+ error_type = "connection"
+ logger.warning("Detected connection error in synthesis")
+ progress_callback(
+ "Connection issue with LLM. Attempting fallback...",
+ 87,
+ {"phase": "synthesis_error", "error_type": error_type},
+ )
+ elif (
+ "llm error" in error_message
+ or "final answer synthesis fail" in error_message
+ ):
+ error_type = "llm_error"
+ logger.warning("Detected general LLM error in synthesis")
+ progress_callback(
+ "LLM error during synthesis. Attempting fallback...",
+ 87,
+ {"phase": "synthesis_error", "error_type": error_type},
+ )
+ else:
+ # Generic error
+ logger.warning("Detected unknown error in synthesis")
+ progress_callback(
+ "Error during synthesis. Attempting fallback...",
+ 87,
+ {"phase": "synthesis_error", "error_type": "unknown"},
+ )
+
# Extract synthesized content from findings if available
synthesized_content = ""
for finding in results.get("findings", []):
@@ -431,18 +498,53 @@ def run_research_process(
break
# Use synthesized content as fallback
- if synthesized_content:
+ if synthesized_content and not synthesized_content.startswith(
+ "Error:"
+ ):
+ logger.info("Using existing synthesized content as fallback")
raw_formatted_findings = synthesized_content
+
# Or use current_knowledge as another fallback
elif results.get("current_knowledge"):
+ logger.info("Using current_knowledge as fallback")
raw_formatted_findings = results["current_knowledge"]
+
# Or combine all finding contents as last resort
elif results.get("findings"):
- raw_formatted_findings = "\n\n".join(
+ logger.info("Combining all findings as fallback")
+ # First try to use any findings that are not errors
+ valid_findings = [
f"## {finding.get('phase', 'Finding')}\n\n{finding.get('content', '')}"
for finding in results.get("findings", [])
if finding.get("content")
- )
+ and not finding.get("content", "").startswith("Error:")
+ ]
+
+ if valid_findings:
+ raw_formatted_findings = (
+ "# Research Results (Fallback Mode)\n\n"
+ )
+ raw_formatted_findings += "\n\n".join(valid_findings)
+ raw_formatted_findings += (
+ f"\n\n## Error Information\n{raw_formatted_findings}"
+ )
+ else:
+ # Last resort: use everything including errors
+ raw_formatted_findings = (
+ "# Research Results (Emergency Fallback)\n\n"
+ )
+ raw_formatted_findings += "The system encountered errors during final synthesis.\n\n"
+ raw_formatted_findings += "\n\n".join(
+ f"## {finding.get('phase', 'Finding')}\n\n{finding.get('content', '')}"
+ for finding in results.get("findings", [])
+ if finding.get("content")
+ )
+
+ progress_callback(
+ f"Using fallback synthesis due to {error_type} error",
+ 88,
+ {"phase": "synthesis_fallback", "error_type": error_type},
+ )
logger.info(
"Found formatted_findings of length: %s",