From b4ec340fb34a79597b4cb1ef8ff5b3febe199701 Mon Sep 17 00:00:00 2001 From: Evgenii Molov Date: Wed, 3 Jan 2024 01:17:21 +0100 Subject: [PATCH] Fix failing serpapi response processing for Google Maps API (#14817) **Description:** Fix for processing for serpapi response for Google Maps API **Issue:** Due to the fact corresponding [api](https://serpapi.com/google-maps-api) returns 'local_results' as list, and old version requested `res["local_results"].keys()` of the list. As the result we got exception: ```AttributeError: 'list' object has no attribute 'keys'```. Way to reproduce wrong behaviour: ``` params = { "engine": "google_maps", "type": "search", "google_domain": "google.de", "ll": "@51.1917,10.525,14z", "hl": "de", "gl": "de", } search = SerpAPIWrapper(params=params) results = search.run("cafe") ``` --------- Co-authored-by: Harrison Chase Co-authored-by: Ran --- libs/community/langchain_community/utilities/serpapi.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/utilities/serpapi.py b/libs/community/langchain_community/utilities/serpapi.py index f78b4afc90b..2404df9bcfa 100644 --- a/libs/community/langchain_community/utilities/serpapi.py +++ b/libs/community/langchain_community/utilities/serpapi.py @@ -211,9 +211,14 @@ class SerpAPIWrapper(BaseModel): if "buying_guide" in res.keys(): snippets.append(res["buying_guide"]) - if "local_results" in res.keys() and "places" in res["local_results"].keys(): + if "local_results" in res and isinstance(res["local_results"], list): + snippets += res["local_results"] + if ( + "local_results" in res.keys() + and isinstance(res["local_results"], dict) + and "places" in res["local_results"].keys() + ): snippets.append(res["local_results"]["places"]) - if len(snippets) > 0: return str(snippets) else: