Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c8c4bff
chore(pydantic-ai): Drop support for < 1.76
alexander-alderman-webb Sep 9, 2026
ee34348
remove legacy _patch_call_tool
alexander-alderman-webb Sep 9, 2026
f579f23
ref(pydantic-ai): Use hooks.on.tool_validate_error
alexander-alderman-webb Sep 9, 2026
32126f7
fix import path
alexander-alderman-webb Sep 9, 2026
be88f02
add hooks.on.tool_execute
alexander-alderman-webb Sep 9, 2026
30add84
remove graph_nodes.py
alexander-alderman-webb Sep 9, 2026
ce2a13f
merge
alexander-alderman-webb Sep 9, 2026
ab83044
ref(pydantic-ai): Use hooks.on.run()
alexander-alderman-webb Sep 9, 2026
c7e3f35
Merge branch 'major/3.0' into webb/pydantic-ai/raise-version
alexander-alderman-webb Sep 10, 2026
0fa3439
Merge branch 'webb/pydantic-ai/raise-version' into webb/pydantic-ai/t…
alexander-alderman-webb Sep 10, 2026
fd1a5a2
Merge branch 'webb/pydantic-ai/tool-validate-error' into webb/pydanti…
alexander-alderman-webb Sep 10, 2026
e8135d0
add some types
alexander-alderman-webb Sep 10, 2026
94ee1fc
more precise types
alexander-alderman-webb Sep 10, 2026
63d6f8f
Merge branch 'webb/pydantic-ai/tool-validate-error' into webb/pydanti…
alexander-alderman-webb Sep 10, 2026
93aaa5c
Merge branch 'webb/pydantic-ai/raise-version' into webb/pydantic-ai/t…
alexander-alderman-webb Sep 10, 2026
c20a678
merge
alexander-alderman-webb Sep 10, 2026
2ff50a2
add types
alexander-alderman-webb Sep 10, 2026
73b7492
Merge branch 'major/3.0' into webb/pydantic-ai/raise-version
alexander-alderman-webb Sep 10, 2026
3dc7644
merge
alexander-alderman-webb Sep 10, 2026
73a75d9
Merge branch 'webb/pydantic-ai/tool-validate-error' into webb/pydanti…
alexander-alderman-webb Sep 10, 2026
54eeea2
add isolation scope to agent runs
alexander-alderman-webb Sep 10, 2026
1e5a613
remove agent model fallback
alexander-alderman-webb Sep 10, 2026
5b0485d
restore _set_agent_data
alexander-alderman-webb Sep 10, 2026
1d5726d
merge
alexander-alderman-webb Sep 10, 2026
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
32 changes: 26 additions & 6 deletions sentry_sdk/integrations/pydantic_ai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
from importlib.metadata import PackageNotFoundError, version
from typing import TYPE_CHECKING

from .patches import (
_patch_agent_run,
)
from .spans.ai_client import ai_client_span, update_ai_client_span
from .spans.invoke_agent import invoke_agent_span, update_invoke_agent_span

if TYPE_CHECKING:
from typing import Any
Expand All @@ -35,11 +33,13 @@
ToolCallPart,
ToolDefinition,
)
from pydantic_ai.agent import AgentRunResult
from pydantic_ai.capabilities import (
Hooks,
RawToolArgs,
ValidatedToolArgs,
WrapModelRequestHandler,
WrapRunHandler,
WrapToolExecuteHandler,
)
from pydantic_ai.messages import ModelResponse
Expand All @@ -59,7 +59,7 @@ async def on_model_request(
) -> "ModelResponse":
with ai_client_span(
messages=request_context.messages,
agent=None,
agent=ctx.agent,
model=request_context.model,
model_settings=request_context.model_settings,
) as span:
Expand Down Expand Up @@ -118,6 +118,28 @@ async def sentry_wrap_tool_execute(
_capture_exception(exc, handled=True)
reraise(*exc_info)

@hooks.on.run
async def sentry_wrap_run(
ctx: "RunContext[Any]",
*,
handler: "WrapRunHandler",
) -> "AgentRunResult[Any]":
with sentry_sdk.isolation_scope(), invoke_agent_span(
user_prompt=ctx.prompt,
agent=ctx.agent,
model=ctx.model,
model_settings=ctx.model_settings,
) as span:
try:
result = await handler()
update_invoke_agent_span(span, result)
return result
except Exception as exc:
exc_info = sys.exc_info()
with capture_internal_exceptions():
_capture_exception(exc, handled=False)
reraise(*exc_info)
Comment thread
cursor[bot] marked this conversation as resolved.

original_init = Agent.__init__

@functools.wraps(original_init)
Expand Down Expand Up @@ -185,8 +207,6 @@ def setup_once() -> None:
if PYDANTIC_AI_VERSION is None:
return

_patch_agent_run()

try:
from pydantic_ai.capabilities import Hooks
except ImportError:
Expand Down
1 change: 0 additions & 1 deletion sentry_sdk/integrations/pydantic_ai/patches/__init__.py

This file was deleted.

181 changes: 0 additions & 181 deletions sentry_sdk/integrations/pydantic_ai/patches/agent_run.py

This file was deleted.

20 changes: 5 additions & 15 deletions sentry_sdk/integrations/pydantic_ai/spans/ai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
_set_model_data,
_should_send_inputs,
_should_send_outputs,
get_current_agent,
get_is_streaming,
)
from .utils import (
_serialize_binary_content_item,
Expand All @@ -30,6 +28,7 @@
if TYPE_CHECKING:
from typing import Any, Dict, List, Optional, Union

from pydantic_ai import Agent
from pydantic_ai.messages import ModelMessage, ModelResponse, SystemPromptPart
from pydantic_ai.models import Model
from pydantic_ai.settings import ModelSettings
Expand Down Expand Up @@ -271,7 +270,7 @@ def _set_output_data(

def ai_client_span(
messages: "list[ModelMessage]",
agent: "Any",
Comment thread
cursor[bot] marked this conversation as resolved.
agent: "Optional[Agent[Any, Any]]",
model: "Model",
model_settings: "Optional[ModelSettings]",
) -> "StreamedSpan":
Expand All @@ -283,29 +282,20 @@ def ai_client_span(
model: Model object
model_settings: Model settings
"""
# Determine model name for span name
model_obj = model
if agent and hasattr(agent, "model"):
model_obj = agent.model

model_name = _get_model_name(model_obj) or "unknown"
model_name = _get_model_name(model) or "unknown"

span = sentry_sdk.traces.start_span(
name=f"chat {model_name}",
attributes={
"sentry.op": OP.GEN_AI_CHAT,
"sentry.origin": SPAN_ORIGIN,
SPANDATA.GEN_AI_OPERATION_NAME: "chat",
SPANDATA.GEN_AI_RESPONSE_STREAMING: get_is_streaming(),
},
)

_set_agent_data(span, agent)
_set_model_data(span, model, model_settings)

# Add available tools if agent is available
agent_obj = agent or get_current_agent()
_set_available_tools(span, agent_obj)
_set_model_data(span, agent, model, model_settings)
_set_available_tools(span, agent)

# Set input messages (full conversation history)
if messages:
Expand Down
18 changes: 11 additions & 7 deletions sentry_sdk/integrations/pydantic_ai/spans/invoke_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
)

if TYPE_CHECKING:
from typing import Any
from typing import Any, Optional, Sequence, Union

from pydantic_ai import Agent, UserContent
from pydantic_ai.models import AbstractModel
from pydantic_ai.realtime.settings import RealtimeModelSettings
from pydantic_ai.settings import ModelSettings

try:
from pydantic_ai.messages import BinaryContent, ImageUrl
Expand All @@ -32,11 +37,10 @@


def invoke_agent_span(
user_prompt: "Any",
agent: "Any",
model: "Any",
model_settings: "Any",
is_streaming: bool = False,
user_prompt: "Optional[Union[str, Sequence[UserContent]]]",
agent: "Optional[Agent]",
model: "AbstractModel",
model_settings: "Optional[Union[ModelSettings, RealtimeModelSettings]]",
) -> "StreamedSpan":
"""Create a span for invoking the agent."""
# Determine agent name for span
Expand All @@ -54,7 +58,7 @@ def invoke_agent_span(
)

_set_agent_data(span, agent)
_set_model_data(span, model, model_settings)
_set_model_data(span, agent, model, model_settings)
Comment thread
alexander-alderman-webb marked this conversation as resolved.
_set_available_tools(span, agent)

# Add user prompt and system prompts if available and prompts are enabled
Expand Down
Loading
Loading