Skip to main content

An integration package connecting SearchApi.io and LangChain

Project description

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).

Related

License

MIT — see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_searchapi-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_searchapi-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_searchapi-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_searchapi-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.14.0 Darwin/25.5.0

File hashes

Hashes for langchain_searchapi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6bc372b2db7866f74837d60d83b0ba6ebeff6cc21d5e5ba3ded48f606d540282
MD5 64c725d2f20f9f85e81d43766cc2f718
BLAKE2b-256 93d2a3ba133fd678903bbad3fa8e08dfd1dcf99de1cda74a4860fb990f96a99a

See more details on using hashes here.

File details

Details for the file langchain_searchapi-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_searchapi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d6f51a98aa3797cfbbff5e207b66def35c09ed08e6084026c9876c7f1d9db46
MD5 6d5bd4a6d906f3998ea4f23835e154df
BLAKE2b-256 a8a32ca76ccded02c2d74fd6156426e10732af0f04594b09163e870d9fe6049f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page