Google Search API for python

Question:
I keep getting an error when i try to run the google search api. i currently have the api variable in my main.py but want to use it in another file. I am using another api the same way and do not have any issues.

Error:

File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/os.py", line 680, in __getitem__
    raise KeyError(key) from None
KeyError: 'GOOGLE_CSE_ID'

Question:
I have set up everything on the google cloud. I just want to use the google search api and nothing else. what am i missing? do i have to install/import dependencies to use the google search api?

thanks in advance.

Assuming that GOOGLE_CSE_ID is an API key for the Google Search API, did you create the secret beforehand and reference the key correctly? (os.environ["caseSensitiveName"])

3 Likes

I did secret first. here is the code my_google_api_key = os.environ[‘GOOGLE_API_KEY’]

I use my_google_api_key in my other python file. this works for my other api.

do i need to use it in a different file? i understand the google search data comes in json. but i haven’t created a http/json file yet for my landing page.

Could you please give us the link to your repl

1 Like

You specified GOOGLE_API_KEY, but the error is talking about GOOGLE_CSI_ID. Rename GOOGLE_API_KEY to GOOGLE_CSI_ID both in your code and in your repl secrets, and see if it works.

this is the same exact error i am receiving here: Python Google API error - googleapiclient.errors.UnknownApiNameOrVersion: name: customsearch version: v1

Code:

import sys 
import os 

from googleapiclient.discovery import build

my_google_api_key = os.environ['GOOGLE_API_KEY']
my_google_cse_id = os.environ['GOOGLE_CSE_ID']

service = build("customsearch", "v1", developerKey=my_google_api_key, static_discovery=False, cache_discovery=False)

Error:

Traceback (most recent call last):
  File "/home/runner/WeeChef/app/nonworking/agent.py", line 21, in <module>
    tools = load_tools([
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/langchain/agents/load_tools.py", line 439, in load_tools
    tool = _get_tool_func(**sub_kwargs)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/langchain/agents/load_tools.py", line 198, in _get_google_search
    return GoogleSearchRun(api_wrapper=GoogleSearchAPIWrapper(**kwargs))
  File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 1102, in pydantic.main.validate_model
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/langchain/utilities/google_search.py", line 86, in validate_environment
    service = build("customsearch", "v1", developerKey=google_api_key)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/googleapiclient/discovery.py", line 287, in build
    content = _retrieve_discovery_doc(
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/googleapiclient/discovery.py", line 404, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion(
googleapiclient.errors.UnknownApiNameOrVersion: name: customsearch  version: v1

That is a very different error from this one, did you perhaps post the wrong code?

sorry i’ve been playing around with so much code i may have. here is the latest from the google api section

Code:

import sys 
import os 

from googleapiclient.discovery import build

google_api_key = os.environ['GOOGLE_API_KEY']
google_cse_id = os.environ['GOOGLE_CSE_ID']

service = build("customsearch", "v1", developerKey=google_api_key, static_discovery=False, cache_discovery=False)

Error:

File "/home/runner/WeeChef/app/nonworking/agent.py", line 16, in <module>
    google_cse_id = os.environ['GOOGLE_CSE_ID']
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/os.py", line 680, in __getitem__
    raise KeyError(key) from None
KeyError: 'GOOGLE_CSE_ID'

Check that you have GOOGLE_CSE_ID set in the Secrets tab of your repl, and that error implies that you didn’t set one.

i have it saved. i just resaved it, ran the shell, and got the same error.

is there an issue with the nix file?

Probably not, KeyError: 'GOOGLE_CSE_ID' on line 16 is a python error that directly translates to: “os.environ does not have a value corresponding to the key == ‘GOOGLE_CSE_ID’” Did you have a typo? Capitalization? Underscore misinterpreted as hyphen? perhaps screenshot the secrets tab (but with the token masked)? There is probably no other explanation than a human mistake, or a big (but extremely unlikely) misconfiguration.

Also, what did you change (generally) between your error from before to the googleapiclient.errors.UnknownApiNameOrVersion error? Did you accidentally post the wrong error? Try running the program again

3 Likes

here is the full code i want to run. a simple langchain agent with google-search capabilities. I’ve also attached a screenshot of the GOOGLE_CSE_ID. thank you for your patience with this.

sorry updated code and secret.

CODE:

import os
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI

google_api_key = os.environ['GOOGLE_API_KEY']
google_cse_id = os.environ['GOOGLE_CSE_ID']

memory = ConversationBufferMemory()
llm = ChatOpenAI()
tools = load_tools(["google-search"], llm=llm)

agent = initialize_agent(
  tools,
  llm,
  load_tools,
  agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
  verbose=True,
  memory=memory
)

agent.run("What is the capital of France?")

ERROR

Traceback (most recent call last):
  File "/home/runner/WeeChef/app/nonworking/agent2.py", line 14, in <module>
    tools = load_tools(["google-search"], llm=llm)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/langchain/agents/load_tools.py", line 439, in load_tools
    tool = _get_tool_func(**sub_kwargs)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/langchain/agents/load_tools.py", line 198, in _get_google_search
    return GoogleSearchRun(api_wrapper=GoogleSearchAPIWrapper(**kwargs))
  File "pydantic/main.py", line 339, in pydantic.main.BaseModel.__init__
  File "pydantic/main.py", line 1102, in pydantic.main.validate_model
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/langchain/utilities/google_search.py", line 86, in validate_environment
    service = build("customsearch", "v1", developerKey=google_api_key)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/googleapiclient/discovery.py", line 287, in build
    content = _retrieve_discovery_doc(
  File "/home/runner/WeeChef/venv/lib/python3.10/site-packages/googleapiclient/discovery.py", line 404, in _retrieve_discovery_doc
    raise UnknownApiNameOrVersion(
googleapiclient.errors.UnknownApiNameOrVersion: name: customsearch  version: v1

According to the error the API client doesn’t recognize the API name and version that you’re trying to use.

Make sure that your GOOGLE_API_KEY and GOOGLE_CSE_ID are correct.
You print the Secrets from the Google_CS_ID but are you sure that the Google API KEY is set in the Secrets too?

Thanks for the response.

Yes, they are both set correctly in my main.py. I’ve even run the file printing out the key and id to confirm.

It doesn’t happen with my openAI api key, only google stuff

is customsearch v1 supposed to exist/work? This seems like a google api issue, so I can’t help you that much anymore

It’s possible that the Google API client library version that he’s using is incompatible with the current code. Try updating the Google API client library to its latest version