Skip to content

Commit 396f322

Browse files
set metadata in agent.__init__
1 parent 3452e52 commit 396f322

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

sentry_sdk/integrations/pydantic_ai/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ def patched_init(self: "Agent[Any, Any]", *args: "Any", **kwargs: "Any") -> None
101101
caps = list(kwargs.get("capabilities") or [])
102102
caps.append(hooks)
103103
kwargs["capabilities"] = caps
104+
105+
metadata = kwargs.get("metadata")
106+
if not metadata:
107+
kwargs["metadata"] = {} # Used as shared reference between hooks
108+
104109
return original_init(self, *args, **kwargs)
105110

106111
Agent.__init__ = patched_init
@@ -118,13 +123,12 @@ class PydanticAIIntegration(Integration):
118123
Hooks using the decorators provided by `pydantic_ai.capabilities` create and manage spans for model calls when these hooks are available (newer library versions).
119124
The span is created in `on_request` and stored in the metadata of the `RunContext` object shared with `on_response` and `on_error`.
120125
121-
The metadata dictionary on the RunContext instance is initialized with `{"_sentry_span": None}` in the `_create_run_wrapper()` and `_create_streaming_wrapper()` wrappers that
122-
instrument `Agent.run()` and `Agent.run_stream()`, respectively. A non-empty dictionary is required for the metadata object to be a shared reference between hooks.
126+
The metadata on the RunContext instance is initialized with an empty dictionary in `Agent.__init__()`. The dictionary is required for the metadata object
127+
to be a shared reference between hooks.
123128
"""
124129

125130
identifier = "pydantic_ai"
126131
origin = f"auto.ai.{identifier}"
127-
are_request_hooks_available = True
128132

129133
def __init__(
130134
self, include_prompts: bool = True, handled_tool_call_exceptions: bool = True
@@ -158,7 +162,6 @@ def setup_once() -> None:
158162
from pydantic_ai.capabilities import Hooks
159163
except ImportError:
160164
Hooks = None
161-
PydanticAIIntegration.are_request_hooks_available = False
162165

163166
if Hooks is None:
164167
_patch_graph_nodes()

sentry_sdk/integrations/pydantic_ai/patches/agent_run.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ def _create_run_wrapper(
9696
original_func: The original run method
9797
is_streaming: Whether this is a streaming method (for future use)
9898
"""
99-
from sentry_sdk.integrations.pydantic_ai import (
100-
PydanticAIIntegration,
101-
) # Required to avoid circular import
10299

103100
@wraps(original_func)
104101
async def wrapper(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
@@ -110,11 +107,6 @@ async def wrapper(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
110107
model = kwargs.get("model")
111108
model_settings = kwargs.get("model_settings")
112109

113-
if PydanticAIIntegration.are_request_hooks_available:
114-
metadata = kwargs.get("metadata")
115-
if not metadata:
116-
kwargs["metadata"] = {"_sentry_span": None}
117-
118110
# Create invoke_agent span
119111
with invoke_agent_span(
120112
user_prompt, self, model, model_settings, is_streaming
@@ -148,9 +140,6 @@ def _create_streaming_wrapper(
148140
"""
149141
Wraps run_stream method that returns an async context manager.
150142
"""
151-
from sentry_sdk.integrations.pydantic_ai import (
152-
PydanticAIIntegration,
153-
) # Required to avoid circular import
154143

155144
@wraps(original_func)
156145
def wrapper(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
@@ -159,11 +148,6 @@ def wrapper(self: "Any", *args: "Any", **kwargs: "Any") -> "Any":
159148
model = kwargs.get("model")
160149
model_settings = kwargs.get("model_settings")
161150

162-
if PydanticAIIntegration.are_request_hooks_available:
163-
metadata = kwargs.get("metadata")
164-
if not metadata:
165-
kwargs["metadata"] = {"_sentry_span": None}
166-
167151
# Call original function to get the context manager
168152
original_ctx_manager = original_func(self, *args, **kwargs)
169153

0 commit comments

Comments
 (0)