Skip to main content

langchain-searchapi

PyPI version License: MIT Python 3.9+

An integration package connecting SearchApi.io and LangChain.

Why this package?

LangChain's original SearchApi integration lives in langchain_community (merged in 2023) but was never ported to the modern standalone package architecture. It lacks:

  • Multi-engine selection at call time — the old tool only accepts a query string; agents can't switch engines dynamically
  • Retriever for RAG — no way to use SearchApi results in retrieval chains
  • Secret management — API key stored as plain string, visible in logs/repr
  • Structured output — returns stringified results instead of typed dicts

This package (langchain-searchapi) is a modern, standalone replacement following the same architecture as langchain-tavily. It provides a Tool for agents, a Retriever for RAG, and full async support.

Installation

pip install langchain-searchapi

Setup

Set your API key as an environment variable:

export SEARCHAPI_API_KEY="your-api-key"

Get your key at searchapi.io.

Quick Start

Tool (for agents)

from langchain_searchapi import SearchApiSearch

# Default Google search
tool = SearchApiSearch()
results = tool.invoke({"query": "latest AI news"})

# Switch engines at call time
results = tool.invoke({"query": "python tutorial", "engine": "youtube"})

# Configure defaults at initialization
tool = SearchApiSearch(engine="google_news", num=5, gl="us", hl="en")
results = tool.invoke({"query": "technology"})

With a LangChain Agent

from langchain_searchapi import SearchApiSearch
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(model="gpt-4o")
tools = [SearchApiSearch()]

agent = create_react_agent(llm, tools)
response = agent.invoke({"messages": [("user", "What happened in tech news today?")]})

Retriever (for RAG)

from langchain_searchapi import SearchApiRetriever

retriever = SearchApiRetriever(engine="google", num=5)
docs = retriever.invoke("machine learning frameworks comparison")

for doc in docs:
    print(f"{doc.metadata['title']}: {doc.page_content}")

In a RAG Chain

from langchain_searchapi import SearchApiRetriever
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

retriever = SearchApiRetriever(num=5)
llm = ChatOpenAI(model="gpt-4o")

prompt = ChatPromptTemplate.from_template(
    "Answer the question based on the context:\n\n{context}\n\nQuestion: {question}"
)

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

answer = chain.invoke("What are the best Python web frameworks in 2024?")

Supported Engines

Engine Value Description
Google google Web search (default)
Google News google_news News articles
Google Shopping google_shopping Product listings
Google Jobs google_jobs Job postings
Google Scholar google_scholar Academic papers
YouTube youtube Video search
Bing bing Microsoft Bing
Baidu baidu Chinese search engine

API Reference

SearchApiSearch

Tool for use with LangChain agents. Agents can select the engine and number of results dynamically via the tool's input schema.

Parameter Type Default Description
searchapi_api_key str env SEARCHAPI_API_KEY Your SearchApi.io API key
engine str "google" Default search engine
num int None Number of results
gl str None Country code (e.g., "us")
hl str None Language code (e.g., "en")
location str None Location for localized results

SearchApiRetriever

Retriever for RAG chains. Returns search results as LangChain Document objects with metadata (title, link, source, position).

Same parameters as SearchApiSearch.

SearchApiAPIWrapper

Low-level wrapper if you need direct API access. Provides raw_results(), results() (metadata-stripped), and run() (text snippets).

Migrating from langchain_community

If you're using the deprecated langchain_community.utilities.SearchApiAPIWrapper:

# Before (deprecated)
from langchain_community.utilities import SearchApiAPIWrapper
wrapper = SearchApiAPIWrapper()
result = wrapper.run("query")

# After
from langchain_searchapi import SearchApiAPIWrapper
wrapper = SearchApiAPIWrapper()
result = wrapper.run("query")

The new package is a drop-in replacement for basic usage, with additional features (multi-engine tool schema, retriever, SecretStr key management).

License

MIT — see LICENSE for details.

Metadata

Release files for langchain-searchapi 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-searchapi 0.1.0
File Size Uploaded
langchain_searchapi-0.1.0.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-searchapi 0.1.0
File Interpreter ABI Platform
langchain_searchapi-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.1 kB

Release files / langchain_searchapi-0.1.0.tar.gz

Download URL langchain_searchapi-0.1.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6bc372b2db7866f74837d60d83b0ba6ebeff6cc21d5e5ba3ded48f606d540282
BLAKE2b-256 checksum
How to use checksums
93d2a3ba133fd678903bbad3fa8e08dfd1dcf99de1cda74a4860fb990f96a99a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.0 Darwin/25.5.0

Release files / langchain_searchapi-0.1.0-py3-none-any.whl

Download URL langchain_searchapi-0.1.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d6f51a98aa3797cfbbff5e207b66def35c09ed08e6084026c9876c7f1d9db46
BLAKE2b-256 checksum
How to use checksums
a8a32ca76ccded02c2d74fd6156426e10732af0f04594b09163e870d9fe6049f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.2.1 CPython/3.14.0 Darwin/25.5.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page