Lesson_2_Student getting 401 Client Error: Unauthorized for url: https://api.tavily.com/search

Using Google Colab.

on the cell:
tavily_api_key=“tavily_api_key”

messages = [HumanMessage(content=“What is the weather in sf?”)]

result = abot.graph.invoke({“messages”: messages})

Got:

Calling: {‘name’: ‘tavily_search_results_json’, ‘args’: {‘query’: ‘weather in San Francisco’}, ‘id’: ‘call_cEb7nvJ0bxAXV2OmCwjEUFd3’, ‘type’: ‘tool_call’}
Back to the model!
Calling: {‘name’: ‘tavily_search_results_json’, ‘args’: {‘query’: ‘weather in San Francisco’}, ‘id’: ‘call_Ru5wcKggRlF7PBu3XXCR1vHC’, ‘type’: ‘tool_call’}
Back to the model!

Then issued int he next cell:
result

Then no results is return with
ToolMessage(content=“HTTPError(‘401 Client Error: Unauthorized for url: https://api.tavily.com/search’)”, name=‘tavily_search_results_json’, tool_call_id=‘call_cEb7nvJ0bxAXV2OmCwjEUFd3’),

Is it related to tavily_api_key?

Thanks.

Peter

error clearly states there is no authorisation to use api key from the url you have used.

make sure when you running codes locally you have your own api keys and also sometimes when a multi interference api keys is set up, chances are your own openai api key might work. You might need to go through the documentation for the codes you are trying to run for understanding the authorisation part.

Deepti:

I have included %env TAVILY_API_KEY= “tavily_api_key” in the previous cell.

But I still got the error.

If I can see the source code from Tavily I can debug it.

Thanks.

Peter

that’s what is unauthorised for learners to use locally, you need to reach tavily for source code :crazy_face:

Thanks.

Looks like we could not fix this problem even with correct tavily_api_key.

Peter

is the tavily api key your account api key from tavily?

Deepti:

The tavily api key I got from Tavily site.

Thanks.

Peter

oh ok then that should work, can you please post rather screenshot of the error you are encountering, rather than copy paste.

Also did you raise a ticket at tavily site about this error. because seems like the authorisation is blocked from there, probably they will know if there is any issue with your api key.

I also check if in your coded if the API keys are in discrepancy with some other module, even that can cause error.

I worked locally and I confirm that it works properly :slightly_smiling_face:.

My suggestions to solve your issue are the followng :

  • From your message, it seems that you are using the default TAVILY_API_KEY. This should work, however it may have limited access and it may only work in specific scenarios. Therefore, my suggestion is to Generate Your Own TAVILY_API_KEY.

  • Another suggestion to solve the problem is to have a look how you have set up the value of TAVILY_API_KEY in the env. Remember it should be without quotes :

TAVILY_API_KEY=tvly-your-valid-api-key

Then,

# Load environment variables
load_dotenv()

# Print the API key to verify it is loaded correctly
print(f"TAVILY_API_KEY: {os.getenv('TAVILY_API_KEY')}")

Remember it should be appear without quotes!

Hoping this could help you!

1 Like

Michela:

Thanks for your help.

I created a new TAVILY_API_KEY, and it worked. After one day, it did not work again.

I do not know what the actual variable name. Like openai_api_key I can just give the key variable directly and it worked.

I was able to print TAVILY_API_KEY.

I did use quotes around TAVILY_API_KEY. I am going to try without the quotes.

Thanks again.

The journey to learn how to create AI agents is not easy. I am going to try n8n also.

Tzechung Kao (Peter)

Step by step! Please, continue to keep up-to-dated about this topic. Glad to share insights together to solve asap the issue.