Handle missing search results safely
Guard against a None result or missing 'results' key by iterating over (result or {}).get('results', []) instead of result['results']. This prevents exceptions when the search backend returns no data and yields an empty output list while preserving existing behavior of limiting to SEARCH_ENGINE_RESULTS.
frdel committed
Mar 3, 2026 at 13:06 UTC
b5a6402e7299e1c74dbab38a184ef49c64e48a29
1 file changed
+1
-1
python/tools/search_engine.py
+1
-1
@@ -32,7 +32,7 @@ class SearchEngine(Tool):
32
return f"{source} search failed: {str(result)}"
33
34
outputs = []
35
- for item in result["results"]:
35
+ for item in (result or {}).get("results", []):
36
outputs.append(f"{item['title']}\n{item['url']}\n{item['content']}")
37
38
return "\n\n".join(outputs[:SEARCH_ENGINE_RESULTS]).strip()