langchain.tools.SerpAPIWrapper throws Invalid API Key despite valid key

langchain.tools.SerpAPIWrapper throws Invalid API Key despite valid key

Spotted this today while wiring a quick search tool.

from langchain.tools import SerpAPIWrapper
tool = SerpAPIWrapper()
print(tool.run("Sydney weather"))

Console spat back:

SerpApiError: Invalid API key

Key worked in curl. Quota fine.

Python 3.11, langchain 0.1.12, serpapi 0.6.2. Environment variable SERPAPI_API_KEY was set.

Problem: SerpAPI upgraded to require SERPAPI_API_KEY in the process environment and a serpapi_api_key parameter in the LangChain wrapper after 0.1.7.

Fix:

tool = SerpAPIWrapper(serpapi_api_key=os.environ["SERPAPI_API_KEY"])

or pin versions:

pip install "langchain<0.1.7" "serpapi<0.6"

After passing the key explicitly the request returned JSON as expected.

If the wrapper still raises check for hidden whitespace in the key; LangChain leaves it untrimmed.

comments powered by Disqus