Skip to content

Commit 4b2d511

Browse files
committed
fix(toolkits): Move results iteration inside try block for better exception handling
1 parent adf4aba commit 4b2d511

File tree

1 file changed

+37
-41
lines changed

1 file changed

+37
-41
lines changed

camel/toolkits/search_toolkit.py

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ def search_duckduckgo(
189189
represents a search result.
190190
"""
191191
from duckduckgo_search import DDGS
192-
from requests.exceptions import RequestException
193192

194193
ddgs = DDGS()
195194
responses: List[Dict[str, Any]] = []
@@ -199,65 +198,62 @@ def search_duckduckgo(
199198
results = ddgs.text(
200199
keywords=query, max_results=number_of_result_pages
201200
)
202-
except RequestException as e:
201+
# Iterate over results found
202+
for i, result in enumerate(results, start=1):
203+
# Creating a response object with a similar structure
204+
response = {
205+
"result_id": i,
206+
"title": result["title"],
207+
"description": result["body"],
208+
"url": result["href"],
209+
}
210+
responses.append(response)
211+
except Exception as e:
203212
# Handle specific exceptions or general request exceptions
204213
responses.append({"error": f"duckduckgo search failed.{e}"})
205214

206-
# Iterate over results found
207-
for i, result in enumerate(results, start=1):
208-
# Creating a response object with a similar structure
209-
response = {
210-
"result_id": i,
211-
"title": result["title"],
212-
"description": result["body"],
213-
"url": result["href"],
214-
}
215-
responses.append(response)
216-
217215
elif source == "images":
218216
try:
219217
results = ddgs.images(
220218
keywords=query, max_results=number_of_result_pages
221219
)
222-
except RequestException as e:
220+
# Iterate over results found
221+
for i, result in enumerate(results, start=1):
222+
# Creating a response object with a similar structure
223+
response = {
224+
"result_id": i,
225+
"title": result["title"],
226+
"image": result["image"],
227+
"url": result["url"],
228+
"source": result["source"],
229+
}
230+
responses.append(response)
231+
except Exception as e:
223232
# Handle specific exceptions or general request exceptions
224233
responses.append({"error": f"duckduckgo search failed.{e}"})
225234

226-
# Iterate over results found
227-
for i, result in enumerate(results, start=1):
228-
# Creating a response object with a similar structure
229-
response = {
230-
"result_id": i,
231-
"title": result["title"],
232-
"image": result["image"],
233-
"url": result["url"],
234-
"source": result["source"],
235-
}
236-
responses.append(response)
237-
238235
elif source == "videos":
239236
try:
240237
results = ddgs.videos(
241238
keywords=query, max_results=number_of_result_pages
242239
)
243-
except RequestException as e:
240+
# Iterate over results found
241+
for i, result in enumerate(results, start=1):
242+
# Creating a response object with a similar structure
243+
response = {
244+
"result_id": i,
245+
"title": result["title"],
246+
"description": result["description"],
247+
"embed_url": result["embed_url"],
248+
"publisher": result["publisher"],
249+
"duration": result["duration"],
250+
"published": result["published"],
251+
}
252+
responses.append(response)
253+
except Exception as e:
244254
# Handle specific exceptions or general request exceptions
245255
responses.append({"error": f"duckduckgo search failed.{e}"})
246256

247-
# Iterate over results found
248-
for i, result in enumerate(results, start=1):
249-
# Creating a response object with a similar structure
250-
response = {
251-
"result_id": i,
252-
"title": result["title"],
253-
"description": result["description"],
254-
"embed_url": result["embed_url"],
255-
"publisher": result["publisher"],
256-
"duration": result["duration"],
257-
"published": result["published"],
258-
}
259-
responses.append(response)
260-
261257
# If no answer found, return an empty list
262258
return responses
263259

0 commit comments

Comments
 (0)