mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-21 12:01:47 +00:00
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"])`.
This commit is contained in:
parent
b4eb043b81
commit
f3508228df
@ -96,6 +96,7 @@ class GoogleSearchAPIWrapper(BaseModel):
|
|||||||
if len(results) == 0:
|
if len(results) == 0:
|
||||||
return "No good Google Search Result was found"
|
return "No good Google Search Result was found"
|
||||||
for result in results:
|
for result in results:
|
||||||
|
if "snippet" in result:
|
||||||
snippets.append(result["snippet"])
|
snippets.append(result["snippet"])
|
||||||
|
|
||||||
return " ".join(snippets)
|
return " ".join(snippets)
|
||||||
@ -119,10 +120,11 @@ class GoogleSearchAPIWrapper(BaseModel):
|
|||||||
return [{"Result": "No good Google Search Result was found"}]
|
return [{"Result": "No good Google Search Result was found"}]
|
||||||
for result in results:
|
for result in results:
|
||||||
metadata_result = {
|
metadata_result = {
|
||||||
"snippet": result["snippet"],
|
|
||||||
"title": result["title"],
|
"title": result["title"],
|
||||||
"link": result["link"],
|
"link": result["link"],
|
||||||
}
|
}
|
||||||
|
if "snippet" in result:
|
||||||
|
metadata_result["snippet"] = result["snippet"]
|
||||||
metadata_results.append(metadata_result)
|
metadata_results.append(metadata_result)
|
||||||
|
|
||||||
return metadata_results
|
return metadata_results
|
||||||
|
Loading…
Reference in New Issue
Block a user