Skip to content

Commit 433e1bc

Browse files
authored
docs: Explain tool execution, validation, and retries (#1578)
1 parent 4b4c5f7 commit 433e1bc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/tools.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,23 @@ print(test_model.last_model_request_parameters.function_tools)
467467
```
468468

469469
_(This example is complete, it can be run "as is")_
470+
471+
472+
## Tool Execution and Retries {#tool-retries}
473+
474+
When a tool is executed, its arguments (provided by the LLM) are first validated against the function's signature using Pydantic. If validation fails (e.g., due to incorrect types or missing required arguments), a `ValidationError` is raised, and the framework automatically generates a [`RetryPromptPart`][pydantic_ai.messages.RetryPromptPart] containing the validation details. This prompt is sent back to the LLM, informing it of the error and allowing it to correct the parameters and retry the tool call.
475+
476+
Beyond automatic validation errors, the tool's own internal logic can also explicitly request a retry by raising the [`ModelRetry`][pydantic_ai.exceptions.ModelRetry] exception. This is useful for situations where the parameters were technically valid, but an issue occurred during execution (like a transient network error, or the tool determining the initial attempt needs modification).
477+
478+
```python
479+
from pydantic_ai import ModelRetry
480+
481+
482+
def my_flaky_tool(query: str) -> str:
483+
if query == 'bad':
484+
# Tell the LLM the query was bad and it should try again
485+
raise ModelRetry("The query 'bad' is not allowed. Please provide a different query.")
486+
# ... process query ...
487+
return 'Success!'
488+
```
489+
Raising `ModelRetry` also generates a `RetryPromptPart` containing the exception message, which is sent back to the LLM to guide its next attempt. Both `ValidationError` and `ModelRetry` respect the `retries` setting configured on the `Tool` or `Agent`.

0 commit comments

Comments
 (0)