Skip to content

Commit

Permalink
fix(serper-dev): restore search localization parameters
Browse files Browse the repository at this point in the history
- Re-add country (gl), location, and locale (hl) parameters to SerperDevTool class
- Update payload construction in _make_api_request to include localization params
- Add schema validation for localization parameters
- Update documentation and examples to demonstrate parameter usage

These parameters were accidentally removed in the previous enhancement PR and are crucial for:
- Getting region-specific search results (via country/gl)
- Targeting searches to specific cities (via location)
- Getting results in specific languages (via locale/hl)

BREAKING CHANGE: None - This restores previously available functionality
  • Loading branch information
beowolx committed Jan 28, 2025
1 parent f3805ad commit dcadb7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion crewai_tools/tools/serper_dev_tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ from crewai_tools import SerperDevTool
tool = SerperDevTool(
n_results=10, # Optional: Number of results to return (default: 10)
save_file=False, # Optional: Save results to file (default: False)
search_type="search" # Optional: Type of search - "search" or "news" (default: "search")
search_type="search", # Optional: Type of search - "search" or "news" (default: "search")
country="us", # Optional: Country for search (default: "")
location="New York", # Optional: Location for search (default: "")
locale="en-US" # Optional: Locale for search (default: "")
)

# Execute a search
Expand Down
13 changes: 12 additions & 1 deletion crewai_tools/tools/serper_dev_tool/serper_dev_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import logging
import os
from typing import Any, Type
from typing import Any, Type, Optional

import requests
from crewai.tools import BaseTool
Expand Down Expand Up @@ -45,6 +45,9 @@ class SerperDevTool(BaseTool):
n_results: int = 10
save_file: bool = False
search_type: str = "search"
country: Optional[str] = ""
location: Optional[str] = ""
locale: Optional[str] = ""

def _get_search_url(self, search_type: str) -> str:
"""Get the appropriate endpoint URL based on search type."""
Expand Down Expand Up @@ -147,6 +150,14 @@ def _make_api_request(self, search_query: str, search_type: str) -> dict:
"""Make API request to Serper."""
search_url = self._get_search_url(search_type)
payload = json.dumps({"q": search_query, "num": self.n_results})

if self.country != "":
payload["gl"] = self.country
if self.location != "":
payload["location"] = self.location
if self.locale != "":
payload["hl"] = self.locale

headers = {
"X-API-KEY": os.environ["SERPER_API_KEY"],
"content-type": "application/json",
Expand Down

0 comments on commit dcadb7f

Please sign in to comment.