Skip to content

Commit f350822

Browse files
authored
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://<b>msc</b>.fema.gov/portal/home'}` This will cause a KeyError at line 99 `snippets.append(result["snippet"])`.
1 parent b4eb043 commit f350822

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

langchain/utilities/google_search.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def run(self, query: str) -> str:
9696
if len(results) == 0:
9797
return "No good Google Search Result was found"
9898
for result in results:
99-
snippets.append(result["snippet"])
99+
if "snippet" in result:
100+
snippets.append(result["snippet"])
100101

101102
return " ".join(snippets)
102103

@@ -119,10 +120,11 @@ def results(self, query: str, num_results: int) -> List[Dict]:
119120
return [{"Result": "No good Google Search Result was found"}]
120121
for result in results:
121122
metadata_result = {
122-
"snippet": result["snippet"],
123123
"title": result["title"],
124124
"link": result["link"],
125125
}
126+
if "snippet" in result:
127+
metadata_result["snippet"] = result["snippet"]
126128
metadata_results.append(metadata_result)
127129

128130
return metadata_results

0 commit comments

Comments
 (0)