Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Refactor web search functionality to improve SERP API integration. Updated action description for clarity, streamlined data handling by excluding unnecessary fields, and enhanced logging for debugging. Returns only relevant organic search results.

Magnus Müllerc48d83f54d81
diff --git a/eval/service.py b/eval/service.py
index 4bb5908cb..c7764ea4a 100644
--- a/eval/service.py
+++ b/eval/service.py
@@ -435,36 +435,33 @@ def create_controller_with_serp_search():
"""Create a controller with SERP search instead of Google search"""
controller = Controller(exclude_actions=['search_google'])
- @controller.registry.action('Search the web for a specific query')
+ @controller.registry.action('Search the web for a specific query. Returns a short description and links of the results.')
async def search_web(query: str):
- """Search the web using Serper API"""
- if not SERPER_API_KEY:
- return ActionResult(extracted_content='Search unavailable: SERPER_API_KEY not configured', include_in_memory=False)
+ # do a serp search for the query
+ conn = http.client.HTTPSConnection('google.serper.dev')
+ payload = json.dumps({'q': query})
+ headers = {'X-API-KEY': SERPER_API_KEY, 'Content-Type': 'application/json'}
+ conn.request('POST', '/search', payload, headers)
+ res = conn.getresponse()
+ data = res.read()
+ serp_data = json.loads(data.decode('utf-8'))
- try:
- # Make request to Serper API
- conn = http.client.HTTPSConnection('google.serper.dev')
- payload = json.dumps({'q': query})
- headers = {'X-API-KEY': SERPER_API_KEY, 'Content-Type': 'application/json'}
- conn.request('POST', '/search', payload, headers)
- res = conn.getresponse()
- data = res.read()
- serp_data = json.loads(data.decode('utf-8'))
+ # exclude searchParameters and credits
+ serp_data = {k: v for k, v in serp_data.items() if k not in ['searchParameters', 'credits']}
- # Exclude searchParameters and credits to reduce noise
- serp_data = {k: v for k, v in serp_data.items() if k not in ['searchParameters', 'credits']}
+ # keep the value of the key "organic"
- # Log the search data for debugging
- logger.debug(f"SERP search for '{query}': {json.dumps(serp_data, indent=2)}")
+ organic = serp_data.get('organic', [])
+ # remove the key "position"
+ organic = [{k: v for k, v in d.items() if k != 'position'} for d in organic]
- # Convert to string for the agent
- serp_data_str = json.dumps(serp_data)
+ # print the original data
+ logger.debug(json.dumps(organic, indent=2))
- return ActionResult(extracted_content=serp_data_str, include_in_memory=False)
+ # to string
+ organic_str = json.dumps(organic)
- except Exception as e:
- logger.error(f'Error in SERP search: {type(e).__name__}: {e}')
- return ActionResult(extracted_content=f'Search error: {str(e)}', include_in_memory=False)
+ return ActionResult(extracted_content=organic_str, include_in_memory=False)
return controller
diff --git a/examples/custom-functions/advanced_search.py b/examples/custom-functions/advanced_search.py
index 9011d3463..e95a56548 100644
--- a/examples/custom-functions/advanced_search.py
+++ b/examples/custom-functions/advanced_search.py
@@ -37,7 +37,7 @@ if not SERP_API_KEY:
controller = Controller(exclude_actions=['search_google'], output_model=PersonList)
[email protected]('Search the web for a specific query')
[email protected]('Search the web for a specific query. Returns a short description and links of the results.')
async def search_web(query: str):
# do a serp search for the query
conn = http.client.HTTPSConnection('google.serper.dev')
@@ -51,13 +51,19 @@ async def search_web(query: str):
# exclude searchParameters and credits
serp_data = {k: v for k, v in serp_data.items() if k not in ['searchParameters', 'credits']}
+ # keep the value of the key "organic"
+
+ organic = serp_data.get('organic', [])
+ # remove the key "position"
+ organic = [{k: v for k, v in d.items() if k != 'position'} for d in organic]
+
# print the original data
- logger.debug(json.dumps(serp_data, indent=2))
+ logger.debug(json.dumps(organic, indent=2))
# to string
- serp_data_str = json.dumps(serp_data)
+ organic_str = json.dumps(organic)
- return ActionResult(extracted_content=serp_data_str, include_in_memory=False)
+ return ActionResult(extracted_content=organic_str, include_in_memory=False)
names = [