@@ -51,8 +51,16 @@ def run(
5151 else :
5252 return {"error" : f"Unknown action: { action } " }
5353
54- def search (self , query : str , max_results : int = 5 ) -> List [Dict [str , Any ]]:
55- """Search the web."""
54+ def search (self , query : str , max_results : int = 5 , retries : int = 3 ) -> List [Dict [str , Any ]]:
55+ """Search the web with retry logic.
56+
57+ Args:
58+ query: Search query
59+ max_results: Maximum results to return
60+ retries: Number of retry attempts on failure
61+ """
62+ import time
63+
5664 if not query :
5765 return [{"error" : "query is required" }]
5866
@@ -61,21 +69,40 @@ def search(self, query: str, max_results: int = 5) -> List[Dict[str, Any]]:
6169 except ImportError :
6270 return [{"error" : "duckduckgo-search not installed. Install with: pip install duckduckgo-search" }]
6371
64- try :
65- with DDGS (proxy = self .proxy , timeout = self .timeout ) as ddgs :
66- results = list (ddgs .text (query , max_results = max_results ))
67-
68- return [
69- {
70- "title" : r .get ("title" ),
71- "url" : r .get ("href" ),
72- "snippet" : r .get ("body" ),
73- }
74- for r in results
75- ]
76- except Exception as e :
77- logger .error (f"DuckDuckGo search error: { e } " )
78- return [{"error" : str (e )}]
72+ last_error = None
73+ retry_delay = 1.0
74+
75+ for attempt in range (retries ):
76+ try :
77+ with DDGS (proxy = self .proxy , timeout = self .timeout ) as ddgs :
78+ results = list (ddgs .text (query , max_results = max_results ))
79+
80+ if results :
81+ return [
82+ {
83+ "title" : r .get ("title" ),
84+ "url" : r .get ("href" ),
85+ "snippet" : r .get ("body" ),
86+ }
87+ for r in results
88+ ]
89+
90+ # Empty results - retry
91+ if attempt < retries - 1 :
92+ logger .debug (f"DuckDuckGo returned empty, retrying ({ attempt + 1 } /{ retries } )..." )
93+ time .sleep (retry_delay * (attempt + 1 ))
94+
95+ except Exception as e :
96+ last_error = e
97+ logger .debug (f"DuckDuckGo attempt { attempt + 1 } failed: { e } " )
98+ if attempt < retries - 1 :
99+ time .sleep (retry_delay * (attempt + 1 ))
100+
101+ # All retries exhausted
102+ if last_error :
103+ logger .error (f"DuckDuckGo search error after { retries } attempts: { last_error } " )
104+ return [{"error" : str (last_error )}]
105+ return [{"error" : f"No results after { retries } attempts" }]
79106
80107 def news (self , query : str , max_results : int = 5 ) -> List [Dict [str , Any ]]:
81108 """Get news articles."""
0 commit comments