Integrating SERP API with LangChain for Intelligent AI Agents
LangChain has become the go-to framework for building AI applications, but most developers struggle with integrating real-time search capabilities. In this tutorial, you’ll learn how to connect SERPpost’s dual search engine API with LangChain to build smarter AI agents.
Why Combine LangChain with SERP API?
LangChain provides the framework, but it needs real-world data to be truly useful. Here’s what you gain:
- Real-time information: Access current search results beyond the LLM’s training data
- Dual search engines: Query both Google and Bing for comprehensive coverage
- Cost efficiency: One API integration instead of maintaining multiple scrapers
- Reliability: Production-grade infrastructure handling billions of queries
Quick Start: Basic Integration
Installation
First, install the required packages:
pip install langchain openai serppost requests
Basic SERPpost Tool Setup
from langchain.tools import Tool
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI
import requests
class SERPpostTool:
def __init__(self, api_key: str):
self.api_key = api_key
self.base_url = "https://serppost.com/api"
def search_google(self, query: str, num_results: int = 10) -> dict:
"""Search Google using SERPpost API"""
headers = {"Authorization": f"Bearer {self.api_key}"}
params = {
"s": query,
"t": "google",
"p": 1,
"num": num_results
}
response = requests.get(
f"{self.base_url}/search",
headers=headers,
params=params
)
return response.json()
def search_bing(self, query: str, num_results: int = 10) -> dict:
"""Search Bing using SERPpost API"""
headers = {"Authorization": f"Bearer {self.api_key}"}
params = {
"s": query,
"t": "bing",
"p": 1,
"num": num_results
}
response = requests.get(
f"{self.base_url}/search",
headers=headers,
params=params
)
return response.json()
def dual_search(self, query: str) -> str:
"""Search both Google and Bing, return formatted results"""
google_results = self.search_google(query, 5)
bing_results = self.search_bing(query, 5)
# Format results for LLM consumption
output = f"Search Results for: {query}\n\n"
output += "=== Google Results ===\n"
for idx, result in enumerate(google_results.get('organic_results', [])[:5], 1):
output += f"{idx}. {result.get('title')}\n"
output += f" {result.get('link')}\n"
output += f" {result.get('snippet')}\n\n"
output += "\n=== Bing Results ===\n"
for idx, result in enumerate(bing_results.get('organic_results', [])[:5], 1):
output += f"{idx}. {result.get('title')}\n"
output += f" {result.get('link')}\n"
output += f" {result.get('snippet')}\n\n"
return output
Building a LangChain Agent with SERP Capabilities
Step 1: Create the Search Tool
# Initialize SERPpost tool
serppost = SERPpostTool(api_key="your_serppost_api_key")
# Create LangChain tool
search_tool = Tool(
name="DualSearchEngine",
func=serppost.dual_search,
description="Searches both Google and Bing for current information. "
"Use this when you need real-time data, news, or facts "
"that may have changed since the AI's training date. "
"Input should be a search query string."
)
Step 2: Initialize the Agent
# Initialize LLM
llm = OpenAI(temperature=0, openai_api_key="your_openai_key")
# Create agent with search capability
agent = initialize_agent(
tools=[search_tool],
llm=llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
max_iterations=3
)
Step 3: Use the Agent
# Example queries
queries = [
"What are the latest AI developments in 2025?",
"Compare the current stock prices of tech companies",
"Find recent research papers on LangChain optimization"
]
for query in queries:
response = agent.run(query)
print(f"\nQuery: {query}")
print(f"Response: {response}\n")
print("-" * 80)
Advanced: Multi-Engine Strategy
For production applications, implement intelligent engine selection:
class SmartSERPAgent:
def __init__(self, serppost_key: str, openai_key: str):
self.serppost = SERPpostTool(serppost_key)
self.llm = OpenAI(temperature=0, openai_api_key=openai_key)
def choose_engine(self, query: str) -> str:
"""Intelligently choose search engine based on query type"""
# Use LLM to classify query
classification_prompt = f"""
Classify this search query as 'google', 'bing', or 'both':
Query: {query}
Guidelines:
- Google: General web queries, news, shopping
- Bing: Technical documentation, Microsoft products, enterprise
- Both: Important queries needing comprehensive results
Response (one word):
"""
engine = self.llm(classification_prompt).strip().lower()
return engine
def smart_search(self, query: str) -> str:
"""Search using the optimal engine(s)"""
engine = self.choose_engine(query)
if engine == "google":
results = self.serppost.search_google(query)
return self._format_single_engine(results, "Google")
elif engine == "bing":
results = self.serppost.search_bing(query)
return self._format_single_engine(results, "Bing")
else: # both
return self.serppost.dual_search(query)
def _format_single_engine(self, results: dict, engine: str) -> str:
"""Format results from a single engine"""
output = f"=== {engine} Results ===\n\n"
for idx, result in enumerate(results.get('organic_results', [])[:10], 1):
output += f"{idx}. {result.get('title')}\n"
output += f" {result.get('snippet')}\n\n"
return output
Real-World Use Case: Research Assistant
Here’s a complete example of a research assistant that uses both search engines:
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
class ResearchAssistant:
def __init__(self, serppost_key: str, openai_key: str):
self.serppost = SERPpostTool(serppost_key)
self.llm = OpenAI(temperature=0.7, openai_api_key=openai_key)
# Research synthesis prompt
self.synthesis_prompt = PromptTemplate(
input_variables=["topic", "search_results"],
template="""
You are a research assistant. Based on the following search results,
create a comprehensive summary about: {topic}
Search Results:
{search_results}
Provide:
1. Key findings (3-5 points)
2. Common themes across sources
3. Notable differences between Google and Bing results
4. Recommended next steps for deeper research
Summary:
"""
)
self.synthesis_chain = LLMChain(
llm=self.llm,
prompt=self.synthesis_prompt
)
def research(self, topic: str) -> dict:
"""Conduct comprehensive research on a topic"""
print(f"Researching: {topic}...")
# Get search results from both engines
search_results = self.serppost.dual_search(topic)
# Synthesize findings
synthesis = self.synthesis_chain.run(
topic=topic,
search_results=search_results
)
return {
"topic": topic,
"raw_results": search_results,
"synthesis": synthesis
}
# Usage
assistant = ResearchAssistant(
serppost_key="your_key",
openai_key="your_openai_key"
)
report = assistant.research("Impact of AI on software development 2025")
print(report["synthesis"])
Best Practices for Production
1. Implement Caching
from functools import lru_cache
from datetime import datetime, timedelta
class CachedSERPpost:
def __init__(self, api_key: str, cache_ttl: int = 3600):
self.tool = SERPpostTool(api_key)
self.cache = {}
self.cache_ttl = cache_ttl
def search(self, query: str, engine: str = "google"):
"""Search with caching"""
cache_key = f"{engine}:{query}"
# Check cache
if cache_key in self.cache:
cached_data, timestamp = self.cache[cache_key]
if datetime.now() - timestamp < timedelta(seconds=self.cache_ttl):
return cached_data
# Fetch fresh data
if engine == "google":
results = self.tool.search_google(query)
else:
results = self.tool.search_bing(query)
# Update cache
self.cache[cache_key] = (results, datetime.now())
return results
2. Error Handling
import time
from requests.exceptions import RequestException
def robust_search(serppost_tool, query, max_retries=3):
"""Search with retry logic"""
for attempt in range(max_retries):
try:
return serppost_tool.dual_search(query)
except RequestException as e:
if attempt == max_retries - 1:
raise
print(f"Retry {attempt + 1}/{max_retries} after error: {e}")
time.sleep(2 ** attempt) # Exponential backoff
3. Result Filtering
def filter_quality_results(results: dict, min_relevance: float = 0.7) -> list:
"""Filter search results by quality indicators"""
organic_results = results.get('organic_results', [])
quality_results = []
for result in organic_results:
# Simple quality scoring
score = 0.0
# Has snippet
if result.get('snippet'):
score += 0.3
# Title length indicates detailed content
if len(result.get('title', '')) > 30:
score += 0.2
# HTTPS sites
if result.get('link', '').startswith('https://'):
score += 0.2
# Known domains get boost
domain_quality = ['wikipedia.org', 'github.com', '.edu', '.gov']
if any(d in result.get('link', '') for d in domain_quality):
score += 0.3
if score >= min_relevance:
quality_results.append(result)
return quality_results
Performance Optimization
Optimize your LangChain + SERP API integration for speed:
import asyncio
import aiohttp
async def async_dual_search(api_key: str, query: str) -> dict:
"""Asynchronous dual search for faster results"""
base_url = "https://serppost.com/api"
headers = {"Authorization": f"Bearer {api_key}"}
async with aiohttp.ClientSession() as session:
# Parallel requests
google_task = session.get(
f"{base_url}/search",
headers=headers,
params={"s": query, "t": "google", "p": 1}
)
bing_task = session.get(
f"{base_url}/search",
headers=headers,
params={"s": query, "t": "bing", "p": 1}
)
google_resp, bing_resp = await asyncio.gather(google_task, bing_task)
return {
"google": await google_resp.json(),
"bing": await bing_resp.json()
}
# Usage
results = asyncio.run(async_dual_search("your_key", "AI trends 2025"))
Common Pitfalls to Avoid
- Not handling rate limits: Implement proper throttling
- Ignoring cache: Cache results to save API calls
- Poor error handling: Always wrap API calls in try-except
- Overly complex prompts: Keep LangChain prompts focused
- Not validating results: Always check result quality
💡 Pro Tip: Use the dual search strategically. For most queries, one engine is sufficient. Reserve dual search for high-value queries where comprehensive coverage matters.
Conclusion
Integrating SERPpost’s SERP API with LangChain unlocks powerful capabilities for your AI applications:
- �?Real-time search data access
- �?Dual engine coverage (Google + Bing)
- �?Simple integration with existing LangChain apps
- �?Production-ready reliability
The combination of LangChain’s framework and SERPpost’s search infrastructure lets you build AI agents that are both intelligent and informed with current data.
Ready to build smarter AI applications? Start your free trial with SERPpost and get 1,000 free API calls to test your LangChain integration.
Next Steps
- Explore the API documentation for advanced features
- Check out pricing plans for production deployment
- Test your integration in the API playground
Related Resources
- SERP API Best Practices 2025
- Building AI Agents with Google & Bing API
- SERP API for AI Agents and LLM
- Multi-Search Engine API Integration
- API Documentation
About the Author: Marcus Johnson is a former Solutions Architect at OpenAI with 8 years of experience building production AI systems. He has helped hundreds of companies integrate LLMs with external data sources and specializes in LangChain architecture patterns.
Ready to supercharge your LangChain apps? Try SERPpost free and experience the power of dual search engine integration.