-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix unknown tool retry logic #3597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
geodavic
wants to merge
6
commits into
pydantic:main
Choose a base branch
from
geodavic:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca2e37a
add configurable unknown tool retries
geodavic 46d7e5a
Merge branch 'main' into main
geodavic 24d92c0
Merge branch 'main' into main
geodavic 47efa5c
remove setting
geodavic 969f3c1
Merge branch 'main' of github.com:geodavic/pydantic-ai
geodavic c847a69
Merge branch 'main' into main
geodavic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| from . import messages as _messages | ||
| from ._instrumentation import InstrumentationNames | ||
| from ._run_context import AgentDepsT, RunContext | ||
| from .exceptions import ModelRetry, ToolRetryError, UnexpectedModelBehavior | ||
| from .exceptions import ModelRetry, ToolRetryError, UnexpectedModelBehavior, UnknownToolNameRetry | ||
| from .messages import ToolCallPart | ||
| from .tools import ToolDefinition | ||
| from .toolsets.abstract import AbstractToolset, ToolsetTool | ||
|
|
@@ -35,6 +35,8 @@ class ToolManager(Generic[AgentDepsT]): | |
| """The cached tools for this run step.""" | ||
| failed_tools: set[str] = field(default_factory=set) | ||
| """Names of tools that failed in this run step.""" | ||
| max_unknown_tool_retries: int = 1 | ||
| """Maximum number of times to retry after an unknown tool is proposed""" | ||
|
|
||
| @classmethod | ||
| @contextmanager | ||
|
|
@@ -146,7 +148,7 @@ async def _call_tool( | |
| msg = f'Available tools: {", ".join(f"{name!r}" for name in self.tools.keys())}' | ||
| else: | ||
| msg = 'No tools available.' | ||
| raise ModelRetry(f'Unknown tool name: {name!r}. {msg}') | ||
| raise UnknownToolNameRetry(name, msg) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to keep this as it was for now. |
||
|
|
||
| if tool.tool_def.kind == 'external': | ||
| raise RuntimeError('External tools cannot be called') | ||
|
|
@@ -176,7 +178,10 @@ async def _call_tool( | |
|
|
||
| return result | ||
| except (ValidationError, ModelRetry) as e: | ||
| max_retries = tool.max_retries if tool is not None else 1 | ||
| if isinstance(e, UnknownToolNameRetry): | ||
| max_retries = self.max_unknown_tool_retries | ||
| else: | ||
| max_retries = tool.max_retries if tool is not None else 1 | ||
| current_retry = self.ctx.retries.get(name, 0) | ||
|
|
||
| if current_retry == max_retries: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,6 +147,7 @@ class Agent(AbstractAgent[AgentDepsT, OutputDataT]): | |
| _prepare_output_tools: ToolsPrepareFunc[AgentDepsT] | None = dataclasses.field(repr=False) | ||
| _max_result_retries: int = dataclasses.field(repr=False) | ||
| _max_tool_retries: int = dataclasses.field(repr=False) | ||
| _max_unknown_tool_retries: int = dataclasses.field(repr=False) | ||
| _validation_context: Any | Callable[[RunContext[AgentDepsT]], Any] = dataclasses.field(repr=False) | ||
|
|
||
| _event_stream_handler: EventStreamHandler[AgentDepsT] | None = dataclasses.field(repr=False) | ||
|
|
@@ -318,6 +319,7 @@ def __init__( | |
|
|
||
| self._max_result_retries = output_retries if output_retries is not None else retries | ||
| self._max_tool_retries = retries | ||
| self._max_unknown_tool_retries = retries | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can simplify this a bit by dropping this variable as well |
||
|
|
||
| self._validation_context = validation_context | ||
|
|
||
|
|
@@ -569,7 +571,7 @@ async def main(): | |
| output_toolset.max_retries = self._max_result_retries | ||
| output_toolset.output_validators = output_validators | ||
| toolset = self._get_toolset(output_toolset=output_toolset, additional_toolsets=toolsets) | ||
| tool_manager = ToolManager[AgentDepsT](toolset) | ||
| tool_manager = ToolManager[AgentDepsT](toolset, max_unknown_tool_retries=self._max_unknown_tool_retries) | ||
|
|
||
| # Build the graph | ||
| graph = _agent_graph.build_agent_graph(self.name, self._deps_type, output_type_) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of calling it
default_max_retries?