From 9b7442f28f5c5f4a0975da835886577282d438dd Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 31 Mar 2025 01:16:06 +0530 Subject: [PATCH] Truncate online query to Serper if query exceeds max supported length Previously query to serper with longer than max supported would throw error instead of returning at least some results. Truncating the onlien search query to serper to max supported length mitigates that issue. --- src/khoj/processor/tools/online_search.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/khoj/processor/tools/online_search.py b/src/khoj/processor/tools/online_search.py index b3d8c015..4b356ef8 100644 --- a/src/khoj/processor/tools/online_search.py +++ b/src/khoj/processor/tools/online_search.py @@ -251,9 +251,16 @@ async def search_with_google(query: str, location: LocationData) -> Tuple[str, D async def search_with_serper(query: str, location: LocationData) -> Tuple[str, Dict[str, List[Dict]]]: - country_code = location.country_code.lower() if location and location.country_code else "us" - payload = json.dumps({"q": query, "gl": country_code}) headers = {"X-API-KEY": SERPER_DEV_API_KEY, "Content-Type": "application/json"} + country_code = location.country_code.lower() if location and location.country_code else "us" + max_query_length = 2048 + if len(query) > max_query_length: + logger.warning( + f"Truncate online query. Query length {len(query)} exceeds {max_query_length} supported by Serper. Query: {query}" + ) + query = query[:max_query_length] + + payload = json.dumps({"q": query, "gl": country_code}) async with aiohttp.ClientSession() as session: async with session.post(SERPER_DEV_URL, headers=headers, data=payload) as response: