Skip to content

Conversation

@jamesaud
Copy link

@jamesaud jamesaud commented Dec 12, 2025

Summary

ToolRetryError was not passing a message to Exception.__init__(), causing str(error) to return an empty string.

This came up when the agent loop crashes due to max tool retries, which breaks error monitoring tools like Sentry or Langsmith that rely on str(exception) to display and group errors.

Problem

from pydantic_ai.exceptions import ToolRetryError
from pydantic_ai.messages import RetryPromptPart

part = RetryPromptPart(content='Test error', tool_name='test')
error = ToolRetryError(part)

str(error)  # Returns '' (empty string)
error.args  # Returns ()

Solution

Pass tool_retry.model_response() to the parent Exception.__init__():

str(error)  # Now returns 'Test error\n\nFix the errors and try again.'

Changes

  • pydantic_ai_slim/pydantic_ai/exceptions.py: Pass message to parent class
  • tests/test_exceptions.py: Add ToolRetryError to hashable test + add dedicated string representation test

ToolRetryError was not passing a message to Exception.__init__(),
causing str(error) to return an empty string. This breaks error
monitoring tools like Sentry that rely on str(exception) to display
error messages.

The fix passes tool_retry.model_response() to the parent class,
ensuring the error message is properly accessible via str(error)
and error.args.
@jamesaud jamesaud closed this Dec 12, 2025
@jamesaud jamesaud reopened this Dec 12, 2025
def __init__(self, tool_retry: RetryPromptPart):
self.tool_retry = tool_retry
super().__init__()
super().__init__(tool_retry.model_response())
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use tool_retry.content that contains just the error message, without the surrounding text instructing the model to retry?

Besides a str, content can also be a list of Pydantic error details, so we would need to stringify/dump those as to JSON.

@DouweM DouweM self-assigned this Dec 12, 2025
@DouweM DouweM changed the title fix: pass message to ToolRetryError parent class Set ToolRetryError message Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants