fix(pydantic_ai): Fix AttributeError when patching ToolManager methods#5691
fix(pydantic_ai): Fix AttributeError when patching ToolManager methods#5691BillionClaw wants to merge 1 commit intogetsentry:masterfrom
Conversation
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
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. Bug Fixes 🐛
Internal Changes 🔧
Other
🤖 This preview updates automatically when you update the PR. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
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 |
There was a problem hiding this comment.
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)
This might be true in general but not in this case: 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 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. |


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 usedhasattr()to check for method existence, buthasattr()can returnTrueeven when accessing the attribute raisesAttributeError(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:AttributeError)Testing