Skip to content

Commit cd54139

Browse files
HamzaFarhanKludex
andauthored
Add max_results parameter to DuckDuckGo search tool (#1003)
Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 279b556 commit cd54139

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

docs/common_tools.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Here's an example of how you can use the DuckDuckGo search tool with an agent:
2222

2323
```py {title="main.py" test="skip"}
2424
from pydantic_ai import Agent
25-
from pydantic_ai.toolsets.duckduckgo import duckduckgo_search_tool
25+
from pydantic_ai.common_tools.duckduckgo import duckduckgo_search_tool
2626

2727
agent = Agent(
2828
'openai:o3-mini',

pydantic_ai_slim/pydantic_ai/common_tools/duckduckgo.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
from dataclasses import dataclass
23
from typing import TypedDict
34

@@ -39,6 +40,9 @@ class DuckDuckGoSearchTool:
3940
client: DDGS
4041
"""The DuckDuckGo search client."""
4142

43+
max_results: int | None = None
44+
"""The maximum number of results. If None, returns results only from the first response."""
45+
4246
async def __call__(self, query: str) -> list[DuckDuckGoResult]:
4347
"""Searches DuckDuckGo for the given query and returns the results.
4448
@@ -48,16 +52,22 @@ async def __call__(self, query: str) -> list[DuckDuckGoResult]:
4852
Returns:
4953
The search results.
5054
"""
51-
results = await anyio.to_thread.run_sync(self.client.text, query)
55+
search = functools.partial(self.client.text, max_results=self.max_results)
56+
results = await anyio.to_thread.run_sync(search, query)
5257
if len(results) == 0:
5358
raise RuntimeError('No search results found.')
5459
return duckduckgo_ta.validate_python(results)
5560

5661

57-
def duckduckgo_search_tool(duckduckgo_client: DDGS | None = None):
58-
"""Creates a DuckDuckGo search tool."""
62+
def duckduckgo_search_tool(duckduckgo_client: DDGS | None = None, max_results: int | None = None):
63+
"""Creates a DuckDuckGo search tool.
64+
65+
Args:
66+
duckduckgo_client: The DuckDuckGo search client.
67+
max_results: The maximum number of results. If None, returns results only from the first response.
68+
"""
5969
return Tool(
60-
DuckDuckGoSearchTool(client=duckduckgo_client or DDGS()).__call__,
70+
DuckDuckGoSearchTool(client=duckduckgo_client or DDGS(), max_results=max_results).__call__,
6171
name='duckduckgo_search',
6272
description='Searches DuckDuckGo for the given query and returns the results.',
6373
)

0 commit comments

Comments
 (0)