-
Notifications
You must be signed in to change notification settings - Fork 306
Description
Hello, langchain-community maintainers.
First off, thank you for creating and maintaining such a great project. I'm writing to suggest a small improvement I noticed while using langchain-community.
Description
The SearchApiAPIWrapper class assumes that every search result returned from the SearchApi service will contain a description key. However, based on the SearchApi documentation and real-world usage, the description field is optional and may not be present in all search results.
What I expect to happen:
The wrapper should handle cases where a search result is missing the description key gracefully. Instead of raising a KeyError, it should either use a default value (like an empty string or the snippet field as a fallback) or simply skip the description for that particular result when formatting the output.
What is currently happening:
When SearchApiAPIWrapper.run() processes a search result that lacks a description key, it attempts to access result["knowledge_graph"]["description"] directly. This results in an unhandled KeyError, causing the application to crash. The provided minimal reproducible example demonstrates this behavior.
It seems there are other optional keys as well, not just 'description'.
langchain_community/utilities/searchapi.py - 106 line~
@staticmethod
def _result_as_string(result: dict) -> str:
toret = "No good search result found"
if "answer_box" in result.keys() and "answer" in result["answer_box"].keys():
toret = result["answer_box"]["answer"]
elif "answer_box" in result.keys() and "snippet" in result["answer_box"].keys():
toret = result["answer_box"]["snippet"]
elif "knowledge_graph" in result.keys():
toret = result["knowledge_graph"]["description"]
Error Message
File "/lib/python3.12/site-packages/langchain_community/utilities/searchapi.py", line 113, in _result_as_string
toret = result["knowledge_graph"]["description"]
~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'description'
A simple fix would be to use toret = result["knowledge_graph"].get("description", "") to provide a default value if the key is missing.
System Info
python: 3.12
langchain-community: 0.3.29