From 28b0b0d863916272569010c11328f60173f1cd5f Mon Sep 17 00:00:00 2001 From: davidkgp Date: Tue, 30 Apr 2024 02:10:08 +0200 Subject: [PATCH] community[patch]: Fix for github issue #17690 (#20117) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …/17690 Thank you for contributing to LangChain! - [x] **Fix Google Lens knowledge graph issue**: "langchain: community" - Fix for [No "knowledge_graph" property in Google Lens API call from SerpAPI](https://github.com/langchain-ai/langchain/issues/17690) - [x] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** handled the existence of keys in the json response of Google Lens - **Issue:** [No "knowledge_graph" property in Google Lens API call from SerpAPI](https://github.com/langchain-ai/langchain/issues/17690) - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, hwchase17. Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> --- .../langchain_community/utilities/google_lens.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/utilities/google_lens.py b/libs/community/langchain_community/utilities/google_lens.py index 028c4092a5b..98b796fddad 100644 --- a/libs/community/langchain_community/utilities/google_lens.py +++ b/libs/community/langchain_community/utilities/google_lens.py @@ -65,7 +65,10 @@ class GoogleLensAPIWrapper(BaseModel): return "Google Lens search failed" xs = "" - if len(responseValue["knowledge_graph"]) > 0: + if ( + "knowledge_graph" in responseValue + and len(responseValue["knowledge_graph"]) > 0 + ): subject = responseValue["knowledge_graph"][0] xs += f"Subject:{subject['title']}({subject['subtitle']})\n" xs += f"Link to subject:{subject['link']}\n\n" @@ -74,10 +77,11 @@ class GoogleLensAPIWrapper(BaseModel): xs += f"Title: {image['title']}\n" xs += f"Source({image['source']}): {image['link']}\n" xs += f"Image: {image['thumbnail']}\n\n" - xs += ( - "Reverse Image Search" - + f"Link: {responseValue['reverse_image_search']['link']}\n" - ) + if "reverse_image_search" in responseValue: + xs += ( + "Reverse Image Search" + + f"Link: {responseValue['reverse_image_search']['link']}\n" + ) print(xs) # noqa: T201 docs = [xs]