From f3508228df3a367c85c66b0d510a8c1eea1294ca Mon Sep 17 00:00:00 2001 From: Jonas Ehrenstein Date: Thu, 2 Feb 2023 08:37:52 +0100 Subject: [PATCH] Minor fix for google search util: it's uncertain if "snippet" in results exists (#830) The results from Google search may not always contain a "snippet". Example: `{'kind': 'customsearch#result', 'title': 'FEMA Flood Map', 'htmlTitle': 'FEMA Flood Map', 'link': 'https://msc.fema.gov/portal/home', 'displayLink': 'msc.fema.gov', 'formattedUrl': 'https://msc.fema.gov/portal/home', 'htmlFormattedUrl': 'https://msc.fema.gov/portal/home'}` This will cause a KeyError at line 99 `snippets.append(result["snippet"])`. --- langchain/utilities/google_search.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/langchain/utilities/google_search.py b/langchain/utilities/google_search.py index e1b7fb0aeef..19b8d46493c 100644 --- a/langchain/utilities/google_search.py +++ b/langchain/utilities/google_search.py @@ -96,7 +96,8 @@ class GoogleSearchAPIWrapper(BaseModel): if len(results) == 0: return "No good Google Search Result was found" for result in results: - snippets.append(result["snippet"]) + if "snippet" in result: + snippets.append(result["snippet"]) return " ".join(snippets) @@ -119,10 +120,11 @@ class GoogleSearchAPIWrapper(BaseModel): return [{"Result": "No good Google Search Result was found"}] for result in results: metadata_result = { - "snippet": result["snippet"], "title": result["title"], "link": result["link"], } + if "snippet" in result: + metadata_result["snippet"] = result["snippet"] metadata_results.append(metadata_result) return metadata_results