Skip to content
Merged
Changes from 4 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
19 changes: 17 additions & 2 deletions sentry_sdk/integrations/pydantic_ai/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools

from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.utils import capture_internal_exceptions
from sentry_sdk.utils import capture_internal_exceptions, parse_version

try:
import pydantic_ai # type: ignore # noqa: F401
Expand All @@ -9,7 +9,8 @@
except ImportError:
raise DidNotEnable("pydantic-ai not installed")


Check warning on line 12 in sentry_sdk/integrations/pydantic_ai/__init__.py

View check run for this annotation

@sentry/warden / warden: code-review

`are_request_hooks_available` not reset to `False` when version < 1.73 but `Hooks` is importable

When `pydantic-ai-slim` is installed at a version < 1.73 but the `Hooks` class can be imported, `setup_once()` calls `_patch_graph_nodes()` and returns without registering hooks — but `are_request_hooks_available` remains `True`. This causes `agent_run.py` wrappers (lines 112, 161) to inject `{"_sentry_span": None}` into every agent run's metadata even though no hook will ever populate that span. Consider setting `PydanticAIIntegration.are_request_hooks_available = False` before returning in the version-check branch.
Comment thread
sentry-warden[bot] marked this conversation as resolved.
from importlib.metadata import PackageNotFoundError, version
Comment thread
alexander-alderman-webb marked this conversation as resolved.
from typing import TYPE_CHECKING

from .patches import (
Expand Down Expand Up @@ -164,9 +165,23 @@
Hooks = None
PydanticAIIntegration.are_request_hooks_available = False

if Hooks is None:
try:
PYDANTIC_AI_VERSION = version("pydantic-ai-slim")
except PackageNotFoundError:
_patch_graph_nodes()
return

# ModelRequestContext.model added in https://github.com/pydantic/pydantic-ai/commit/f1260dfe09907f17688eee1646daf898fc428d4c
PYDANTIC_AI_VERSION = parse_version(PYDANTIC_AI_VERSION)
if PYDANTIC_AI_VERSION is None or PYDANTIC_AI_VERSION < (
1,
73,
):
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
_patch_graph_nodes()
return

if Hooks is None:
return

Check warning on line 184 in sentry_sdk/integrations/pydantic_ai/__init__.py

View check run for this annotation

@sentry/warden / warden: code-review

[2QY-TMW] `are_request_hooks_available` not reset to `False` when version &lt; 1.73 but `Hooks` is importable (additional location)

When `pydantic-ai-slim` is installed at a version &lt; 1.73 but the `Hooks` class can be imported, `setup_once()` calls `_patch_graph_nodes()` and returns without registering hooks — but `are_request_hooks_available` remains `True`. This causes `agent_run.py` wrappers (lines 112, 161) to inject `{"_sentry_span": None}` into every agent run's metadata even though no hook will ever populate that span. Consider setting `PydanticAIIntegration.are_request_hooks_available = False` before returning in the version-check branch.
Comment thread
alexander-alderman-webb marked this conversation as resolved.
Outdated

hooks = Hooks()
register_hooks(hooks)
Loading