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.

feat: Add Serper search engine integration (#885)

LearningCircuite826ade3a5ab
diff --git a/src/local_deep_research/defaults/default_settings.json b/src/local_deep_research/defaults/default_settings.json
index 2fd3102ce7..be28c20ae0 100644
--- a/src/local_deep_research/defaults/default_settings.json
+++ b/src/local_deep_research/defaults/default_settings.json
@@ -1012,6 +1012,10 @@
"label": "SerpAPI (Google)",
"value": "serpapi"
},
+ {
+ "label": "Serper (Google)",
+ "value": "serper"
+ },
{
"label": "SearXNG (Self-hosted)",
"value": "searxng"
diff --git a/src/local_deep_research/defaults/settings/search_engines/serper.json b/src/local_deep_research/defaults/settings/search_engines/serper.json
new file mode 100644
index 0000000000..ebd688b30c
--- /dev/null
+++ b/src/local_deep_research/defaults/settings/search_engines/serper.json
@@ -0,0 +1,258 @@
+{
+ "search.engine.web.serper.display_name": {
+ "category": "serper",
+ "description": "Display name to use in the U.I. for this search engine.",
+ "editable": false,
+ "max_value": null,
+ "min_value": null,
+ "name": "Display Name",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "text",
+ "value": "Serper",
+ "visible": false
+ },
+ "search.engine.web.serper.description": {
+ "category": "serper",
+ "description": "Human-readable description of the search engine.",
+ "editable": false,
+ "max_value": null,
+ "min_value": null,
+ "name": "Description",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "text",
+ "value": "Fast and affordable Google Search API with real-time results.",
+ "visible": false
+ },
+ "search.engine.web.serper.api_key": {
+ "category": "serper",
+ "description": "The Serper API key to use. Get yours at https://serper.dev",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Api Key",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "password",
+ "value": "",
+ "visible": true
+ },
+ "search.engine.web.serper.class_name": {
+ "category": "serper",
+ "description": "Class name for the Serper search engine",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Class Name",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "text",
+ "value": "SerperSearchEngine",
+ "visible": true
+ },
+ "search.engine.web.serper.module_path": {
+ "category": "serper",
+ "description": "Module path for the Serper search engine",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Module Path",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "text",
+ "value": "local_deep_research.web_search_engines.engines.search_engine_serper",
+ "visible": true
+ },
+ "search.engine.web.serper.default_params.region": {
+ "category": "serper",
+ "description": "Country code for localized results (e.g., 'us', 'gb', 'fr')",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Region",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "text",
+ "value": "us",
+ "visible": true
+ },
+ "search.engine.web.serper.default_params.search_language": {
+ "category": "serper",
+ "description": "Language code for search results (e.g., 'en', 'es', 'fr')",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Search Language",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "text",
+ "value": "en",
+ "visible": true
+ },
+ "search.engine.web.serper.default_params.safe_search": {
+ "category": "serper",
+ "description": "Enable or disable safe search filtering",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Safe Search",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": true,
+ "visible": true
+ },
+ "search.engine.web.serper.default_params.time_period": {
+ "category": "serper",
+ "description": "Filter results by time period ('day', 'week', 'month', 'year', or null for all time)",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Time Period",
+ "options": [
+ {
+ "label": "All Time",
+ "value": null
+ },
+ {
+ "label": "Past Day",
+ "value": "day"
+ },
+ {
+ "label": "Past Week",
+ "value": "week"
+ },
+ {
+ "label": "Past Month",
+ "value": "month"
+ },
+ {
+ "label": "Past Year",
+ "value": "year"
+ }
+ ],
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "dropdown",
+ "value": null,
+ "visible": true
+ },
+ "search.engine.web.serper.default_params.include_full_content": {
+ "category": "serper",
+ "description": "Include full webpage content in results (may slow down search)",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Include Full Content",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": false,
+ "visible": true
+ },
+ "search.engine.web.serper.reliability": {
+ "category": "serper",
+ "description": "Reliability of the Serper search engine",
+ "editable": true,
+ "max_value": 1.0,
+ "min_value": 0.0,
+ "name": "Reliability",
+ "options": null,
+ "step": 0.05,
+ "type": "SEARCH",
+ "ui_element": "range",
+ "value": 0.95,
+ "visible": true
+ },
+ "search.engine.web.serper.requires_api_key": {
+ "category": "serper",
+ "description": "Whether this search engine requires an API key",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Requires Api Key",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": true,
+ "visible": false
+ },
+ "search.engine.web.serper.requires_llm": {
+ "category": "serper",
+ "description": "Whether this search engine requires an LLM for relevance filtering",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Requires LLM",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": false,
+ "visible": false
+ },
+ "search.engine.web.serper.supports_full_search": {
+ "category": "serper",
+ "description": "Whether this search engine supports full document search",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Supports Full Search",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": true,
+ "visible": false
+ },
+ "search.engine.web.serper.strengths": {
+ "category": "serper",
+ "description": "Strengths of this search engine",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Strengths",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "json",
+ "value": [
+ "Lightning-fast response times (1-2 seconds)",
+ "Very affordable pricing",
+ "High-quality Google search results",
+ "Real-time results",
+ "Knowledge graph and related searches",
+ "People Also Ask data",
+ "Simple pay-per-use pricing"
+ ],
+ "visible": false
+ },
+ "search.engine.web.serper.weaknesses": {
+ "category": "serper",
+ "description": "Weaknesses of this search engine",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Weaknesses",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "json",
+ "value": [
+ "Requires API key with usage limits",
+ "Not specialized for academic content"
+ ],
+ "visible": false
+ }
+}
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_serper.py b/src/local_deep_research/web_search_engines/engines/search_engine_serper.py
new file mode 100644
index 0000000000..ef387998f1
--- /dev/null
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_serper.py
@@ -0,0 +1,365 @@
+from loguru import logger
+from typing import Any, Dict, List, Optional
+import requests
+from urllib.parse import urlparse
+
+from langchain_core.language_models import BaseLLM
+
+from ..search_engine_base import BaseSearchEngine
+from ..rate_limiting import RateLimitError
+
+
+class SerperSearchEngine(BaseSearchEngine):
+ """Google search engine implementation using Serper API with two-phase approach"""
+
+ # Mark as public search engine
+ is_public = True
+ # Mark as generic search engine (general web search via Google)
+ is_generic = True
+
+ # Class constants
+ BASE_URL = "https://google.serper.dev/search"
+ DEFAULT_TIMEOUT = 30
+ DEFAULT_REGION = "us"
+ DEFAULT_LANGUAGE = "en"
+
+ def __init__(
+ self,
+ max_results: int = 10,
+ region: str = "us",
+ time_period: Optional[str] = None,
+ safe_search: bool = True,
+ search_language: str = "en",
+ api_key: Optional[str] = None,
+ llm: Optional[BaseLLM] = None,
+ include_full_content: bool = False,
+ max_filtered_results: Optional[int] = None,
+ settings_snapshot: Optional[Dict[str, Any]] = None,
+ **kwargs,
+ ):
+ """
+ Initialize the Serper search engine.
+
+ Args:
+ max_results: Maximum number of search results (default 10)
+ region: Country code for localized results (e.g., 'us', 'gb', 'fr')
+ time_period: Time filter for results ('day', 'week', 'month', 'year', or None for all time)
+ safe_search: Whether to enable safe search
+ search_language: Language code for results (e.g., 'en', 'es', 'fr')
+ api_key: Serper API key (can also be set in settings)
+ llm: Language model for relevance filtering
+ include_full_content: Whether to include full webpage content in results
+ max_filtered_results: Maximum number of results to keep after filtering
+ settings_snapshot: Settings snapshot for thread context
+ **kwargs: Additional parameters (ignored but accepted for compatibility)
+ """
+ # Initialize the BaseSearchEngine with LLM, max_filtered_results, and max_results
+ super().__init__(
+ llm=llm,
+ max_filtered_results=max_filtered_results,
+ max_results=max_results,
+ )
+ self.include_full_content = include_full_content
+ self.region = region
+ self.time_period = time_period
+ self.safe_search = safe_search
+ self.search_language = search_language
+
+ # Get API key - check params, env vars, or database
+ from ...config.search_config import get_setting_from_snapshot
+
+ serper_api_key = api_key
+ if not serper_api_key:
+ serper_api_key = get_setting_from_snapshot(
+ "search.engine.web.serper.api_key",
+ settings_snapshot=settings_snapshot,
+ )
+
+ if not serper_api_key:
+ raise ValueError(
+ "Serper API key not found. Please provide api_key parameter or set it in the UI settings."
+ )
+
+ self.api_key = serper_api_key
+ self.base_url = self.BASE_URL
+ # Note: self.engine_type is automatically set by parent BaseSearchEngine class
+
+ # If full content is requested, initialize FullSearchResults
+ if include_full_content:
+ # Import FullSearchResults only if needed
+ try:
+ from .full_search import FullSearchResults
+
+ self.full_search = FullSearchResults(
+ llm=llm,
+ web_search=None, # We'll handle the search ourselves
+ language=search_language,
+ max_results=max_results,
+ region=region,
+ time=time_period,
+ safesearch="Moderate" if safe_search else "Off",
+ )
+ except ImportError:
+ logger.warning(
+ "Warning: FullSearchResults not available. Full content retrieval disabled."
+ )
+ self.include_full_content = False
+
+ def _get_previews(self, query: str) -> List[Dict[str, Any]]:
+ """
+ Get preview information from Serper API.
+
+ Args:
+ query: The search query
+
+ Returns:
+ List of preview dictionaries
+ """
+ logger.info("Getting search results from Serper API")
+
+ try:
+ # Build request payload
+ payload = {
+ "q": query,
+ "num": self.max_results,
+ "gl": self.region,
+ "hl": self.search_language,
+ }
+
+ # Add optional parameters
+ if self.time_period:
+ # Map time periods to Serper's format
+ time_mapping = {
+ "day": "d",
+ "week": "w",
+ "month": "m",
+ "year": "y",
+ }
+ if self.time_period in time_mapping:
+ payload["tbs"] = f"qdr:{time_mapping[self.time_period]}"
+
+ # Apply rate limiting before request
+ self._last_wait_time = self.rate_tracker.apply_rate_limit(
+ self.engine_type
+ )
+
+ # Make API request
+ headers = {
+ "X-API-KEY": self.api_key,
+ "Content-Type": "application/json",
+ }
+
+ response = requests.post(
+ self.base_url,
+ headers=headers,
+ json=payload,
+ timeout=self.DEFAULT_TIMEOUT,
+ )
+
+ # Check for rate limits
+ if response.status_code == 429:
+ raise RateLimitError(
+ f"Serper rate limit hit: {response.status_code} - {response.text}"
+ )
+
+ response.raise_for_status()
+
+ data = response.json()
+
+ # Extract organic results
+ organic_results = data.get("organic", [])
+
+ # Format results as previews
+ previews = []
+ for idx, result in enumerate(organic_results):
+ # Extract display link safely using urlparse
+ display_link = ""
+ link = result.get("link", "")
+ if link:
+ try:
+ parsed_url = urlparse(link)
+ display_link = parsed_url.netloc or ""
+ except Exception:
+ logger.debug(
+ f"Failed to parse URL for display: {link[:50]}"
+ )
+ display_link = ""
+
+ preview = {
+ "id": idx,
+ "title": result.get("title", ""),
+ "link": link,
+ "snippet": result.get("snippet", ""),
+ "displayed_link": display_link,
+ "position": result.get("position", idx + 1),
+ }
+
+ # Store full Serper result for later
+ preview["_full_result"] = result
+
+ # Only include optional fields if present to avoid None values
+ # This keeps the preview dict cleaner and saves memory
+ if "sitelinks" in result:
+ preview["sitelinks"] = result["sitelinks"]
+
+ if "date" in result:
+ preview["date"] = result["date"]
+
+ if "attributes" in result:
+ preview["attributes"] = result["attributes"]
+
+ previews.append(preview)
+
+ # Store the previews for potential full content retrieval
+ self._search_results = previews
+
+ # Also store knowledge graph if available
+ if "knowledgeGraph" in data:
+ self._knowledge_graph = data["knowledgeGraph"]
+ logger.info(
+ f"Found knowledge graph for query: {data['knowledgeGraph'].get('title', 'Unknown')}"
+ )
+
+ # Store related searches and people also ask
+ if "relatedSearches" in data:
+ self._related_searches = data["relatedSearches"]
+
+ if "peopleAlsoAsk" in data:
+ self._people_also_ask = data["peopleAlsoAsk"]
+
+ return previews
+
+ except RateLimitError:
+ raise # Re-raise rate limit errors
+ except requests.exceptions.RequestException as e:
+ error_msg = str(e)
+ logger.exception("Error getting Serper API results")
+
+ # Check for rate limit patterns in error message
+ if any(
+ pattern in error_msg.lower()
+ for pattern in [
+ "429",
+ "rate limit",
+ "quota",
+ "too many requests",
+ ]
+ ):
+ raise RateLimitError(f"Serper rate limit hit: {error_msg}")
+
+ return []
+ except Exception:
+ logger.exception("Unexpected error getting Serper API results")
+ return []
+
+ def _get_full_content(
+ self, relevant_items: List[Dict[str, Any]]
+ ) -> List[Dict[str, Any]]:
+ """
+ Get full content for the relevant search results.
+ If include_full_content is True and FullSearchResults is available,
+ retrieves full webpage content for the results.
+
+ Args:
+ relevant_items: List of relevant preview dictionaries
+
+ Returns:
+ List of result dictionaries with full content if requested
+ """
+ # Check if we should get full content
+ from ...config import search_config
+
+ if (
+ hasattr(search_config, "SEARCH_SNIPPETS_ONLY")
+ and search_config.SEARCH_SNIPPETS_ONLY
+ ):
+ logger.info("Snippet-only mode, skipping full content retrieval")
+
+ # Return the relevant items with their full Serper information
+ results = []
+ for item in relevant_items:
+ # Use the full result if available, otherwise use the preview
+ if "_full_result" in item:
+ result = item["_full_result"].copy()
+ else:
+ result = item.copy()
+
+ # Clean up temporary fields
+ if "_full_result" in result:
+ del result["_full_result"]
+
+ results.append(result)
+
+ # Include knowledge graph and other metadata if this is the first call
+ if results and hasattr(self, "_knowledge_graph"):
+ results[0]["knowledge_graph"] = self._knowledge_graph
+
+ return results
+
+ # If full content retrieval is enabled
+ if self.include_full_content and hasattr(self, "full_search"):
+ logger.info("Retrieving full webpage content")
+
+ try:
+ # Use FullSearchResults to get full content
+ results_with_content = self.full_search._get_full_content(
+ relevant_items
+ )
+
+ return results_with_content
+
+ except Exception as e:
+ logger.info(f"Error retrieving full content: {e}")
+ # Fall back to returning the items without full content
+
+ # Return items with their full Serper information
+ results = []
+ for item in relevant_items:
+ # Use the full result if available, otherwise use the preview
+ if "_full_result" in item:
+ result = item["_full_result"].copy()
+ else:
+ result = item.copy()
+
+ # Clean up temporary fields
+ if "_full_result" in result:
+ del result["_full_result"]
+
+ results.append(result)
+
+ # Include knowledge graph and other metadata if this is the first call
+ if results and hasattr(self, "_knowledge_graph"):
+ results[0]["knowledge_graph"] = self._knowledge_graph
+
+ return results
+
+ def run(
+ self, query: str, research_context: Dict[str, Any] | None = None
+ ) -> List[Dict[str, Any]]:
+ """
+ Execute a search using Serper API with the two-phase approach.
+
+ Args:
+ query: The search query
+ research_context: Context from previous research to use.
+
+ Returns:
+ List of search results
+ """
+ logger.info("---Execute a search using Serper API (Google)---")
+
+ # Use the implementation from the parent class which handles all phases
+ # Note: super().run() internally calls our _get_previews() method
+ results = super().run(query, research_context=research_context)
+
+ # Clean up temporary attributes
+ if hasattr(self, "_search_results"):
+ del self._search_results
+ if hasattr(self, "_knowledge_graph"):
+ del self._knowledge_graph
+ if hasattr(self, "_related_searches"):
+ del self._related_searches
+ if hasattr(self, "_people_also_ask"):
+ del self._people_also_ask
+
+ return results