DuckDuckGoSearchException Traceback (most recent call last)
Cell In[13], line 2
1 # What is the price of a MacBook Pro?
----> 2 agent_executor.invoke(
3 {
4 "input": "What is the current price of a MacBook Pro in USD? How much would it cost in EUR if the exchange rate is 0.85 EUR for 1 USD."
5 }
6 )
File ~\OneDrive - TCS COM PROD\Documents\python_env\legal_work_env\Lib\site-packages\langchain\chains\base.py:170, in Chain.invoke(self, input, config, **kwargs)
168 except BaseException as e:
169 run_manager.on_chain_error(e)
--> 170 raise e
171 run_manager.on_chain_end(outputs)
173 if include_run_info:
Caused by:
0: client error (Connect)
1: TLS handshake failed: cert verification failed - unable to get local issuer certificate [CERTIFICATE_VERIFY_FAILED]
2: [CERTIFICATE_VERIFY_FAILED]
3: [CERTIFICATE_VERIFY_FAILED]
The error you're encountering is a DuckDuckGoSearchException caused by a TLS certificate verification failure when the DuckDuckGo search tool tries to make an HTTPS request to https://lite.duckduckgo.com/lite/.
What is happening?
Your code uses LangChain with a DuckDuckGo Search API wrapper.
When the search tool tries to perform a search query, the underlying HTTP client fails to verify the SSL certificate of the DuckDuckGo endpoint.
This leads to the HTTP request failing with a CERTIFICATE_VERIFY_FAILED error.
Why does this happen?
Most likely, it's because your Python environment does not have up-to-date or proper CA certificates installed or configured, so Python's requests or the HTTP client used by duckduckgo_search cannot verify DuckDuckGo's SSL certificate.
This is common on some systems, or with some Python installations, especially on Windows or if you are using a custom or virtual environment.
How to fix this?
1. Ensure Python has the proper Root CA Certificates installed
Not recommended for production or sensitive data due to security issues.
If you want to quickly test and verify this is the SSL cert issue, you might disable SSL verification for requests.
You can monkeypatch or modify the duckduckgo_search package call or your HTTP client to disable cert verification. But beware of the security risk.
Additional suggestions
Make sure your duckduckgo_search and langchain_community packages are up to date, as newer versions might fix SSL or backend issues.
If behind a corporate firewall / proxy, ensure the proxy does SSL interception properly and the certificates of proxy are trusted by your system.
Test your environment's SSL with other HTTPS requests (like requests.get("https://www.google.com")) to confirm it's not a general SSL problem.
Summary
The root issue is SSL certificate verification failure on your machine when the DuckDuckGo search tool tries to access the search URL. Fix your environment's SSL certificate bundle installation, upgrade certifi, or set a proper SSL context to resolve this.
If you want, I can provide code snippets or commands to help with the certificate installation for your platform.
Yes, you can use other search engines instead of DuckDuckGo with LangChain, but it depends on which tools or wrappers are available and supported in your environment.
Here are some alternatives you can consider:
1. SerpAPI
SerpAPI provides a Google Search API and other search engine APIs.
It is reliable and widely used, but requires an API key and may have usage costs depending on your plan.
from langchain.utilities import SerpAPIWrapper
serpapi = SerpAPIWrapper(serpapi_api_key="YOUR_SERPAPI_API_KEY")
result = serpapi.run("What is the current price of a MacBook Pro in USD?")
print(result)
2. Google Custom Search API
Google Custom Search allows you to create a search engine and query it via API.
Requires Google API credentials and enabling the Custom Search API.
Can be used via custom code or wrappers.
3. Bing Search API
Microsoft's Bing Search API is another alternative.
Requires Azure subscription and API key.
Can be integrated into your code by calling the Bing search REST API directly or via wrappers if available.
4. Others
You can also create your own wrappers for other search APIs or scrapers.
Some open tools wrap engines like Google, Bing, or others, but be mindful of usage policies and legal issues.
How to switch your LangChain agent to SerpAPI
If you want to replace DuckDuckGo with SerpAPI in your existing agent, update your tools list accordingly.
Example (assuming an agent executor setup):
python
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.utilities import SerpAPIWrapper
serpapi = SerpAPIWrapper(serpapi_api_key="YOUR_SERPAPI_API_KEY")
search_tool = Tool(
name="Search",
func=serpapi.run,
description="Useful for answering questions about current events or general knowledge."
)
llm = OpenAI(temperature=0)
agent = initialize_agent([search_tool], llm, agent="zero-shot-react-description", verbose=True)
agent.run("What is the current price of a MacBook Pro in USD?")
Summary
Yes, you can definitely use other search engines/tools such as SerpAPI (Google Search API) which is well-supported in LangChain. Just be aware that these might require API keys and have associated costs.
If you'd like, I can help you set up any specific alternative search API.