From 24576a39e56c6d3a27914718ba9a8d9542f7c1bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=9E=E6=B3=95=E6=93=8D=E4=BD=9C?= Date: Wed, 22 May 2024 14:28:52 +0800 Subject: [PATCH] fix: some google search result raise exception (#4567) --- .../tools/provider/builtin/google/tools/google_search.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/api/core/tools/provider/builtin/google/tools/google_search.py b/api/core/tools/provider/builtin/google/tools/google_search.py index 0b1978ad3e..b5f15464c3 100644 --- a/api/core/tools/provider/builtin/google/tools/google_search.py +++ b/api/core/tools/provider/builtin/google/tools/google_search.py @@ -99,8 +99,11 @@ class SerpAPI: ): toret = res["knowledge_graph"]["description"] + "\n" if "snippet" in res["organic_results"][0].keys(): - for item in res["organic_results"]: - toret += "content: " + item["snippet"] + "\n" + "link: " + item["link"] + "\n" + toret = "\n".join( + f"content: {item['snippet']}\nlink: {item['link']}" + for item in res["organic_results"] + if "snippet" in item and "link" in item + ) if ( "images_results" in res.keys() and "thumbnail" in res["images_results"][0].keys()