How to Build a LangChain Search Tool with TalorData SERP API
LangChain agents become more useful when they can call external tools. A model can answer from its existing context, but a tool can help it retrieve fresh information, query APIs, search public web results, or pass structured data into a downstream workflow. In LangChain, tools are callable functions that agents can use during task execution, […]
LangChain agents become more useful when they can call external tools.
A model can answer from its existing context, but a tool can help it retrieve fresh information, query APIs, search public web results, or pass structured data into a downstream workflow. In LangChain, tools are callable functions that agents can use during task execution, and the @tool decorator is a common way to define them.
For search-heavy applications, this matters.
A LangChain agent may need to answer questions like:
- What pages rank for this keyword today?
- Which competitors appear in Google Search results?
- What are the latest market reports on this topic?
- Which source URLs should we use for a RAG workflow?
- What product pages or news results are currently visible?
A static knowledge base is not enough for these tasks.
With TalorData SERP API, developers can give LangChain agents access to real-time, structured search results. The tool sends a search query to TalorData, receives SERP data as structured JSON, and returns clean context to the agent.
A practical workflow looks like this:
User question
↓
LangChain agent
↓
TalorData SERP search tool
↓
Structured search results
↓
Source filtering
↓
Agent answer or RAG workflow
This guide explains how to build a LangChain search tool with TalorData SERP API, what fields to return, how to connect the tool to an agent, and how to keep the workflow reliable.
Why Build a Search Tool for LangChain?
LangChain agents are designed to use models and tools together. The create_agent interface lets developers configure a model, tools, and a system prompt so the agent can call tools during execution.
A search tool gives the agent access to fresh public web information.
This is useful for:
| Use Case | Why Search Helps |
| SEO research | Collect current titles, URLs, snippets, and rankings |
| Competitor analysis | Find visible competitor pages |
| Market research | Discover recent reports and public sources |
| Content planning | Analyze what already ranks for a topic |
| Brand monitoring | Track public search visibility |
| RAG source discovery | Select source URLs for retrieval |
| AI research agents | Give agents access to fresh web context |
The goal is not to make the agent search every time.
The goal is to give the agent a reliable search tool when the user asks for information that depends on current search results.
Why Use TalorData SERP API?
TalorData provides structured SERP data for developers building AI agents, SEO tools, search workflows, market monitoring systems, and RAG pipelines.
Instead of asking an agent to browse manually or parse raw search result pages, TalorData returns structured search data that is easier to filter, store, summarize, and pass into LangChain.
TalorData supports multi-engine SERP data collection across search engines such as Google, Bing, Yandex, and DuckDuckGo, with ready-to-use JSON output for developer workflows.
For a LangChain search tool, structured output matters more than raw pages.
A good search result should include fields like:
| Field | Why It Matters |
| position | Shows result ranking |
| title | Helps the agent understand the result |
| url | Provides the source page |
| domain | Helps identify the source website |
| snippet | Gives a short preview |
| search_engine | Shows where the result came from |
| country | Adds market context |
| language | Adds language context |
| collected_at | Preserves freshness context |
For most LangChain agents, JSON is the best starting point.
It is easier to filter, summarize, store, and pass into downstream workflows.
Search Tool Architecture
A simple LangChain search tool has four parts:
| Part | Role |
| Tool input | Query, country, language, device, result count |
| API request | Sends parameters to TalorData SERP API |
| Result parser | Extracts titles, URLs, snippets, and rankings |
| Tool output | Returns clean JSON to the agent |
The workflow looks like this:
Agent receives user question
↓
Agent decides whether search is needed
↓
Agent calls talordata_search()
↓
Tool sends SERP request
↓
Tool parses structured results
↓
Agent uses results to answer or continue workflow
Keep the tool narrow.
A search tool should search and return structured results. Crawling pages, writing final reports, ranking source credibility, and generating business recommendations should be separate steps.
Step 1: Define the Tool Inputs
Start with a small set of inputs.
Useful parameters include:
| Parameter | Purpose |
| query | Search query |
| country | Target country or market |
| language | Search result language |
| device | Desktop or mobile |
| num_results | Number of results to return |
Example input:
{
"query": "best customer support software",
"country": "us",
"language": "en",
"device": "desktop",
"num_results": 5
}
Do not expose every possible SERP parameter at the beginning.
Start simple. Add more controls later when the workflow needs them.
Step 2: Create a TalorData Search Function
The search function should call TalorData SERP API and return structured results.
Use environment variables for sensitive values.
import os
import requests
from typing import Any
TALORDATA_API_KEY = os.getenv("TALORDATA_API_KEY")
TALORDATA_SERP_API_URL = os.getenv("TALORDATA_SERP_API_URL")
def call_talordata_serp_api(
query: str,
country: str = "us",
language: str = "en",
device: str = "desktop",
num_results: int = 5,
) -> dict[str, Any]:
if not TALORDATA_API_KEY:
raise ValueError("Missing TALORDATA_API_KEY environment variable.")
if not TALORDATA_SERP_API_URL:
raise ValueError("Missing TALORDATA_SERP_API_URL environment variable.")
payload = {
"engine": "google",
"q": query,
"country": country,
"language": language,
"device": device,
"num_results": num_results,
}
response = requests.post(
TALORDATA_SERP_API_URL,
headers={
"Authorization": f"Bearer {TALORDATA_API_KEY}",
"Content-Type": "application/json",
},
json=payload,
timeout=30,
)
response.raise_for_status()
return response.json()
Use the endpoint and request format from your TalorData dashboard or API documentation.
The important design point is simple:
Keep the API call separate from the LangChain tool wrapper.
That makes testing easier.
Step 3: Normalize the SERP Results
The agent does not need every raw field.
Return only the fields that help the agent understand and use the result.
from urllib.parse import urlparse
from typing import Any
def extract_domain(url: str) -> str:
try:
return urlparse(url).netloc.replace("www.", "")
except Exception:
return ""
def normalize_serp_results(
data: dict[str, Any],
num_results: int = 5,
) -> list[dict[str, Any]]:
raw_results = (
data.get("organic_results")
or data.get("results")
or []
)
normalized_results = []
for item in raw_results[:num_results]:
url = item.get("url") or item.get("link") or ""
normalized_results.append(
{
"position": item.get("position"),
"title": item.get("title"),
"url": url,
"domain": item.get("domain") or extract_domain(url),
"snippet": item.get("snippet") or item.get("description"),
}
)
return normalized_results
A normalized result should look like this:
{
"position": 1,
"title": "Best Customer Support Software for Growing Teams",
"url": "https://www.example.com/customer-support-software",
"domain": "example.com",
"snippet": "Compare customer support platforms by features, pricing, automation, and team size."
}
The agent can now compare sources, select URLs, summarize snippets, or pass source URLs into another workflow.
Step 4: Wrap the Function as a LangChain Tool
LangChain’s @tool decorator can convert a Python function into a tool. Type hints help define the input schema, and the function docstring helps the model understand when the tool should be used.
Here is a simple tool wrapper:
from typing import Any
from langchain.tools import tool
@tool("talordata_google_search")
def talordata_google_search(
query: str,
country: str = "us",
language: str = "en",
device: str = "desktop",
num_results: int = 5,
) -> dict[str, Any]:
"""
Search Google using TalorData SERP API and return structured SERP results.
Use this tool when the user asks for fresh public web information,
current Google search results, SEO research, competitor pages,
market research sources, or source URLs for RAG workflows.
"""
data = call_talordata_serp_api(
query=query,
country=country,
language=language,
device=device,
num_results=num_results,
)
results = normalize_serp_results(data, num_results=num_results)
return {
"query": query,
"country": country,
"language": language,
"device": device,
"results": results,
}
The docstring matters.
Weak tool description:
Search Google.
Better tool description:
Search Google using TalorData SERP API and return structured SERP results. Use this for fresh public web information, SEO research, competitor pages, market research, and source discovery.
The model uses the tool description to decide whether the tool is relevant.
Step 5: Connect the Tool to a LangChain Agent
Pass the TalorData search tool into a LangChain agent.
from langchain.agents import create_agent
tools = [talordata_google_search]
agent = create_agent(
model="provider:model-name",
tools=tools,
system_prompt=(
"You are a research assistant. "
"Use the TalorData search tool only when the user asks for current, "
"public, search-based, or source-discovery information. "
"When using search results, mention the most relevant source URLs."
),
)
Then invoke the agent:
result = agent.invoke(
{
"messages": [
{
"role": "user",
"content": (
"Find recent sources about AI customer support tools "
"and summarize the top visible pages."
),
}
]
}
)
print(result)
The model provider and model name should match your own LangChain setup.
The search tool is now available to the agent.
Step 6: Control When the Agent Searches
A good search tool should not be called for every question.
Use the system prompt and tool description to guide the agent.
Good search triggers include:
| Trigger | Example |
| Freshness | latest, recent, today, this week |
| SEO | ranking, SERP, top results, Google results |
| Competitor research | competitors, alternatives, comparison |
| Source discovery | find sources, collect URLs, research links |
| Market research | market trends, reports, industry updates |
| Product research | pricing, sellers, product pages |
The tool is usually not needed for:
| Question Type | Better Source |
| Internal policy question | Internal knowledge base |
| Stable concept explanation | Model context or documents |
| Private customer data | Internal API |
| Previously indexed documents | Existing RAG system |
This separation keeps the agent faster and easier to evaluate.
Step 7: Filter Search Results Before Using Them
Search results are not automatically good sources.
The agent should filter before answering.
Useful filtering rules include:
| Rule | Why It Helps |
| Remove irrelevant results | Reduces noise |
| Remove duplicate URLs | Avoids repeated sources |
| Prefer authoritative domains | Improves answer quality |
| Group by domain | Avoids one site dominating |
| Limit result count | Reduces token usage |
| Keep search context | Preserves country, language, and device |
You can add filtering inside the tool:
from typing import Any
def filter_results(results: list[dict[str, Any]]) -> list[dict[str, Any]]:
seen_urls = set()
filtered = []
for result in results:
url = result.get("url")
if not url or url in seen_urls:
continue
if not result.get("title"):
continue
seen_urls.add(url)
filtered.append(result)
return filtered[:5]
Then update the tool:
results = normalize_serp_results(data, num_results=num_results)
results = filter_results(results)
For RAG workflows, this step matters even more.
Bad sources create bad context. Bad context creates unreliable answers.
Step 8: Use the Search Tool for RAG Source Discovery
A TalorData search tool can be used before a RAG step.
The pattern is simple:
User asks a research question
↓
Agent searches with TalorData
↓
Tool returns titles, snippets, and URLs
↓
Agent selects useful URLs
↓
Workflow fetches or indexes selected pages
↓
RAG answer uses selected source content
This is useful for:
| Workflow | Why Search Helps |
| Fresh research Q&A | Finds recent public sources |
| SEO content briefs | Finds current ranking pages |
| Competitor analysis | Finds visible competitor URLs |
| Market reports | Finds recent reports and articles |
| Product research | Finds current product pages |
| News monitoring | Finds recent public updates |
Search is for source discovery.
RAG is for using selected source content.
Keep those steps separate.
Step 9: Add Error Handling
Search tools should fail gracefully.
Common failure cases include:
| Failure | Handling |
| Missing API key | Return configuration error |
| Timeout | Return retryable error |
| API rate limit | Return rate-limit message |
| Empty result set | Return no-results response |
| Unexpected response shape | Return parser error |
Example:
import requests
from typing import Any
from langchain.tools import tool
@tool("talordata_google_search")
def talordata_google_search(
query: str,
country: str = "us",
language: str = "en",
device: str = "desktop",
num_results: int = 5,
) -> dict[str, Any]:
"""
Search Google using TalorData SERP API and return structured SERP results.
Use this for fresh public web information, SEO research,
competitor research, market research, and source discovery.
"""
try:
data = call_talordata_serp_api(
query=query,
country=country,
language=language,
device=device,
num_results=num_results,
)
results = normalize_serp_results(data, num_results=num_results)
results = filter_results(results)
return {
"query": query,
"country": country,
"language": language,
"device": device,
"results": results,
}
except requests.Timeout:
return {
"error": "Search request timed out.",
"query": query,
}
except requests.HTTPError as error:
return {
"error": "Search API request failed.",
"details": str(error),
"query": query,
}
except Exception as error:
return {
"error": "Unexpected search tool error.",
"details": str(error),
"query": query,
}
Error handling is part of tool design.
If the tool fails silently, the agent may continue with incomplete context.
Step 10: Log Search Activity
If this tool runs in production, log search activity.
Useful fields include:
| Field | Purpose |
| user_question | Original user request |
| search_query | Query sent to TalorData |
| country | Search market |
| language | Search language |
| device | Desktop or mobile |
| returned_urls | URLs returned by the tool |
| selected_urls | URLs used by the agent |
| collected_at | Search time |
| run_id | Agent or workflow run ID |
Logs help with:
- Debugging
- Evaluation
- Source review
- Cost tracking
- Prompt improvement
- Tool behavior analysis
- Compliance review
A search-enabled agent should be traceable.
If it searched, you should know what it searched, what it found, and what it used.
Direct Tool, SDK, or MCP?
There are three practical ways to connect TalorData search data with LangChain workflows:
| Method | Best For |
| Direct custom tool | Learning, full control, simple workflows |
| SDK integration | Fast prototypes and single-agent apps |
| MCP integration | Production systems and multi-agent architectures |
For this article, the custom tool approach is useful because it shows the underlying design.
In production, SDK or MCP integration can reduce maintenance work and make search capabilities easier to reuse across agents and workflows.
How TalorData Supports LangChain Search Workflows
TalorData acts as a structured SERP data layer for LangChain agents.
Instead of asking the agent to browse manually or process raw search pages, developers can use TalorData to retrieve structured search results with useful fields for agent reasoning and downstream workflows.
A typical workflow looks like this:
LangChain agent
↓
TalorData SERP tool
↓
Structured Google Search results
↓
Filtered source URLs
↓
Answer, report, dashboard, or RAG workflow
This supports use cases such as:
| Workflow | How TalorData Helps |
| SEO assistant | Collect current SERP titles, URLs, snippets, and rankings |
| Competitor research | Find visible competitor pages |
| Content assistant | Build briefs from current search results |
| Market research | Discover recent public sources |
| RAG source discovery | Select fresh URLs for retrieval |
| AI research assistant | Answer with current web context |
The value is not just search access.
The value is structured, reusable search context that a LangChain agent can work with.
Final Thoughts
Building a LangChain search tool with TalorData SERP API gives your agent access to real-time search context.
The basic process is:
Define search inputs.
Call TalorData SERP API.
Normalize search results.
Wrap the function as a LangChain tool.
Connect the tool to an agent.
Control when the agent searches.
Filter results before answering.
Log search activity.
Use selected URLs for RAG when needed.
For SEO tools, research agents, competitor monitoring, content planning, market intelligence, and RAG workflows, a structured search tool can make a LangChain agent much more useful.
The agent handles reasoning.
TalorData provides structured search data.
The tool connects the two.
FAQ
Can LangChain agents use a custom search tool?
Yes. LangChain tools are callable functions with defined inputs and outputs, and agents can use tools during task execution.
What should a TalorData search tool return?
A useful search tool should return structured SERP results, including position, title, URL, domain, snippet, country, language, device, and collection time.
Should the tool return raw HTML?
Usually no. For agents, structured JSON is usually easier to use. Raw HTML is better reserved for advanced parsing or custom SERP module extraction.
Can this tool support RAG workflows?
Yes. The search tool can discover relevant source URLs. A separate extraction or crawling step can then fetch selected pages for RAG.
Should I use SDK, MCP, or a custom tool?
Use a custom tool when you want to understand or control the workflow. Use SDK for faster prototypes. Use MCP when you need reusable search access for production or multi-agent systems.