Skip to content

Commit b3b1018

Browse files
committed
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.
1 parent ad10d0f commit b3b1018

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/khoj/processor/tools/online_search.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,16 @@ async def search_with_google(query: str, location: LocationData) -> Tuple[str, D
327327

328328

329329
async def search_with_serper(query: str, location: LocationData) -> Tuple[str, Dict[str, List[Dict]]]:
330+
headers = {"X-API-KEY": SERPER_DEV_API_KEY, "Content-Type": "application/json"}
330331
country_code = location.country_code.lower() if location and location.country_code else "us"
332+
max_query_length = 2048
333+
if len(query) > max_query_length:
334+
logger.warning(
335+
f"Truncate online query. Query length {len(query)} exceeds {max_query_length} supported by Serper. Query: {query}"
336+
)
337+
query = query[:max_query_length]
338+
331339
payload = json.dumps({"q": query, "gl": country_code})
332-
headers = {"X-API-KEY": SERPER_DEV_API_KEY, "Content-Type": "application/json"}
333340

334341
async with aiohttp.ClientSession() as session:
335342
async with session.post(SERPER_DEV_URL, headers=headers, data=payload) as response:

0 commit comments

Comments
 (0)