Skip to content

fix(pydantic_ai): Fix AttributeError when patching ToolManager methods#5691

Closed
BillionClaw wants to merge 1 commit intogetsentry:masterfrom
BillionClaw:clawoss/fix/pydantic-ai-regression-1773834687
Closed

fix(pydantic_ai): Fix AttributeError when patching ToolManager methods#5691
BillionClaw wants to merge 1 commit intogetsentry:masterfrom
BillionClaw:clawoss/fix/pydantic-ai-regression-1773834687

Conversation

@BillionClaw
Copy link

Summary

Fixes #5686

The pydantic-ai integration fails at startup in sentry-python 2.55.0 when using pydantic-ai-slim 1.69.0+, raising:

Root Cause

The _patch_tool_execution() function used hasattr() to check for method existence, but hasattr() can return True even when accessing the attribute raises AttributeError (e.g., for properties or descriptors).

This caused the code to enter the wrong branch and fail when trying to access ToolManager._call_tool.

Fix

Instead of using hasattr(), we now try to access the method directly in a try-except block. This safely handles cases where:

  • The method doesn't exist (AttributeError)
  • The method exists but can't be accessed (e.g., properties that raise)

Testing

  • Verified the fix with pydantic-ai-slim 1.70.0
  • All 98 existing tests pass
  • The integration now initializes without errors

The _patch_tool_execution() function used hasattr() to check for method
existence, but this can return True even when accessing the attribute
raises AttributeError (e.g., for properties). This caused a regression
in 2.55.0 when using pydantic-ai 1.69.0+.

Now we try to access the method directly in a try-except block to safely
handle cases where the method doesn't exist or raises AttributeError.

Fixes getsentry#5686
@BillionClaw BillionClaw requested a review from a team as a code owner March 18, 2026 11:51
@github-actions
Copy link
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Bug Fixes 🐛

  • (pydantic_ai) Fix AttributeError when patching ToolManager methods by BillionClaw in #5691

Internal Changes 🔧

  • Add client report tests for span streaming by sentrivana in #5677

Other

  • Update CHANGELOG.md by sentrivana in #5685

🤖 This preview updates automatically when you update the PR.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

_patch_execute_tool_call()
return
except AttributeError:
pass
Copy link

Choose a reason for hiding this comment

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

Overly broad except catches errors from patch functions

Medium Severity

The try/except AttributeError blocks catch exceptions from both the attribute access check and the _patch_execute_tool_call() / _patch_call_tool() calls. If a patching function raises AttributeError internally (e.g., from @wraps() or re-accessing the attribute), the error is silently swallowed. This could cause the integration to silently skip instrumentation or fall through to incorrectly patch the older _call_tool method. Using try/except/else would limit the catch scope to just the attribute probe.

Additional Locations (1)
Fix in Cursor Fix in Web

@sentrivana
Copy link
Contributor

The _patch_tool_execution() function used hasattr() to check for method existence, but hasattr() can return True even when accessing the attribute raises AttributeError (e.g., for properties or descriptors).

This might be true in general but not in this case:

$ ipython
Python 3.12.8 (main, Dec 17 2024, 12:40:19) [Clang 19.1.5 ]
Type 'copyright', 'credits' or 'license' for more information
IPython 9.11.0 -- An enhanced Interactive Python. Type '?' for help.
Tip: IPython supports combining unicode identifiers, eg F\vec<tab> will become F⃗, useful for physics equations. Play with \dot \ddot and others.

In [1]: from pydantic_ai._tool_manager import ToolManager

In [2]: hasattr(ToolManager, "_call_tool")
Out[2]: False

Also, if you look at the code:

def _patch_tool_execution() -> None:
    if hasattr(ToolManager, "execute_tool_call"):
        _patch_execute_tool_call()

    elif hasattr(ToolManager, "_call_tool"):
        # older versions
        _patch_call_tool()

You'll notice we don't even attempt to patch _call_tool if ToolManager has an execute_tool_call method, which it does in 1.69.0. So that branch does not even get executed.

I could not reproduce the original issue and the traceback does not appear to be from SDK v2.55.0, so unless proven otherwise I don't believe there is a regression in the first place.

@sentrivana sentrivana closed this Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

regression in 2.55.0: pydantic-ai integration fails at startup (AttributeError: ToolManager has no attribute '_call_tool')

2 participants