SerpAPIWrapper connects LangChain agents to SerpAPI, providing real-time web-search results through a utility wrapper. An agent cannot invoke the wrapper directly; embed its run method in a Tool with a clear name and description. section 9.3.1
Expose search to an agent
Set SERPAPI_API_KEY, instantiate SerpAPIWrapper, and create a Tool whose function is serpapi.run. Give it a description such as “query real-time information from the web,” then add it to an agent. The agent can search questions about current events or retrieve information beyond the model’s training data. section 9.3.1 section 9.4.3
from langchain_core.tools import Tool
from langchain_community.utilities import SerpAPIWrapper
os.environ["SERPAPI_API_KEY"] = "<SERPAPI_KEY>" #1
serpapi = SerpAPIWrapper() #2
search_tool = Tool(
name = "SerpAPI", #3
func = serpapi.run, #4
description = "A search engine tool to query real-time information
from the web."
)
Runnable setup showing the API key, wrapper instance, Tool name, function binding, and search description. section 9.4.3
