Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pydantic_ai/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ async def process(
m = _messages.RetryPromptPart(
content=e.errors(include_url=False),
)
raise ToolRetryError(m) from e
raise ToolRetryError(m, message=str(e)) from e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ToolRetryError.__init__ is able to access the __cause__ that's set by from e (I'm not sure if it can), we could do this automatically

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked locally. Unfortunately, __cause__ is None in __init__. It gets set after the exception is constructed, by the raise ... from e syntax

else:
raise

Expand Down
4 changes: 2 additions & 2 deletions pydantic_ai_slim/pydantic_ai/_tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ async def _call_tool(
content=e.errors(include_url=False, include_context=False),
tool_call_id=call.tool_call_id,
)
e = ToolRetryError(m)
e = ToolRetryError(m, message=str(e))
elif isinstance(e, ModelRetry):
m = _messages.RetryPromptPart(
tool_name=name,
content=e.message,
tool_call_id=call.tool_call_id,
)
e = ToolRetryError(m)
e = ToolRetryError(m, message=e.message)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't mind setting message automatically if isinstance(tool_retry.content, str)

Copy link
Author

@jamesaud jamesaud Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to support an automatic behavior for list[ErrorDetails]? There's a few options:

  1. call str() on list[ErrorDetails]
  2. call json.dumps on list[ErrorDetails]
  3. format list[ErrorDetails] to be more human readable (more code)
  4. do nothing

Which do you prefer?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, since my idea below of getting it from __cause__ won't work, I prefer option 2, because I don't want us to have to remember to pass in something special when we create a ToolRetryError for a ValidationError; so I'd rather have everything live inside that ToolRetryError.__init__. I do wonder if there's a way to get access to the logic ValidationError uses to produce its human-readable error output based on ErrorDetails.

else:
assert_never(e)

Expand Down
7 changes: 5 additions & 2 deletions pydantic_ai_slim/pydantic_ai/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,12 @@ class FallbackExceptionGroup(ExceptionGroup[Any]):
class ToolRetryError(Exception):
"""Exception used to signal a `ToolRetry` message should be returned to the LLM."""

def __init__(self, tool_retry: RetryPromptPart):
def __init__(self, tool_retry: RetryPromptPart, message: str | None = None):
self.tool_retry = tool_retry
super().__init__()
if message:
super().__init__(message)
else:
super().__init__()


class IncompleteToolCall(UnexpectedModelBehavior):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
IncompleteToolCall,
ModelAPIError,
ModelHTTPError,
ToolRetryError,
UnexpectedModelBehavior,
UsageLimitExceeded,
UserError,
)
from pydantic_ai.messages import RetryPromptPart


@pytest.mark.parametrize(
Expand All @@ -32,6 +34,7 @@
lambda: ModelAPIError('model', 'test message'),
lambda: ModelHTTPError(500, 'model'),
lambda: IncompleteToolCall('test'),
lambda: ToolRetryError(RetryPromptPart(content='test', tool_name='test')),
],
ids=[
'ModelRetry',
Expand All @@ -44,6 +47,7 @@
'ModelAPIError',
'ModelHTTPError',
'IncompleteToolCall',
'ToolRetryError',
],
)
def test_exceptions_hashable(exc_factory: Callable[[], Any]):
Expand All @@ -59,3 +63,10 @@ def test_exceptions_hashable(exc_factory: Callable[[], Any]):

assert exc in s
assert d[exc] == 'value'


def test_tool_retry_error_str():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test that it is set correctly in the ValidationError case

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

"""Test that ToolRetryError uses provided message."""
part = RetryPromptPart(content='some content', tool_name='my_tool')
error = ToolRetryError(part, message='custom message')
assert str(error) == 'custom message'