Skip to content

The agent engineering platform.

Fix a bug that shows "KeyError 'items'" (#1118)

yakigac1ed708391e80
diff --git a/langchain/utilities/google_search.py b/langchain/utilities/google_search.py
index 19b8d46493..95ded0d688 100644
--- a/langchain/utilities/google_search.py
+++ b/langchain/utilities/google_search.py
@@ -61,7 +61,7 @@ class GoogleSearchAPIWrapper(BaseModel):
.list(q=search_term, cx=self.google_cse_id, **kwargs)
.execute()
)
- return res["items"]
+ return res.get("items", [])
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
diff --git a/tests/integration_tests/test_googlesearch_api.py b/tests/integration_tests/test_googlesearch_api.py
index 7bbef2cb0a..3693e212b3 100644
--- a/tests/integration_tests/test_googlesearch_api.py
+++ b/tests/integration_tests/test_googlesearch_api.py
@@ -7,3 +7,13 @@ def test_call() -> None:
search = GoogleSearchAPIWrapper()
output = search.run("What was Obama's first name?")
assert "Barack Hussein Obama II" in output
+
+
+def test_no_result_call() -> None:
+ """Test that call gives no result."""
+ search = GoogleSearchAPIWrapper()
+ output = search.run(
+ "NORESULTCALL_NORESULTCALL_NORESULTCALL_NORESULTCALL_NORESULTCALL_NORESULTCALL"
+ )
+ print(type(output))
+ assert "No good Google Search Result was found" == output