Skip to content

Commit bf3ce00

Browse files
committed
feat: add include_prompts for Pydantic AI integration
1 parent 3337c19 commit bf3ce00

File tree

6 files changed

+722
-11
lines changed

6 files changed

+722
-11
lines changed

sentry_sdk/integrations/pydantic_ai/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@
1616

1717
class PydanticAIIntegration(Integration):
1818
identifier = "pydantic_ai"
19+
origin = f"auto.ai.{identifier}"
20+
21+
def __init__(self, include_prompts=True):
22+
# type: (bool) -> None
23+
"""
24+
Initialize the Pydantic AI integration.
25+
26+
Args:
27+
include_prompts: Whether to include prompts and messages in span data.
28+
Requires send_default_pii=True. Defaults to True.
29+
"""
30+
self.include_prompts = include_prompts
1931

2032
@staticmethod
2133
def setup_once():

sentry_sdk/integrations/pydantic_ai/spans/execute_tool.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import sentry_sdk
22
from sentry_sdk.consts import OP, SPANDATA
3-
from sentry_sdk.scope import should_send_default_pii
43
from sentry_sdk.utils import safe_serialize
54

65
from ..consts import SPAN_ORIGIN
7-
from ..utils import _set_agent_data
6+
from ..utils import _set_agent_data, _should_send_prompts
87

98
from typing import TYPE_CHECKING
109

@@ -27,7 +26,7 @@ def execute_tool_span(tool_name, tool_args, agent):
2726

2827
_set_agent_data(span, agent)
2928

30-
if should_send_default_pii() and tool_args is not None:
29+
if _should_send_prompts() and tool_args is not None:
3130
span.set_data(SPANDATA.GEN_AI_TOOL_INPUT, safe_serialize(tool_args))
3231

3332
return span
@@ -39,5 +38,5 @@ def update_execute_tool_span(span, result):
3938
if not span:
4039
return
4140

42-
if should_send_default_pii() and result is not None:
41+
if _should_send_prompts() and result is not None:
4342
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, safe_serialize(result))

sentry_sdk/integrations/pydantic_ai/spans/invoke_agent.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import sentry_sdk
22
from sentry_sdk.ai.utils import get_start_span_function, set_data_normalized
33
from sentry_sdk.consts import OP, SPANDATA
4-
from sentry_sdk.scope import should_send_default_pii
54

65
from ..consts import SPAN_ORIGIN
7-
from ..utils import _set_agent_data, _set_model_data
6+
from ..utils import _set_agent_data, _set_model_data, _should_send_prompts
87

98
from typing import TYPE_CHECKING
109

@@ -65,8 +64,8 @@ def invoke_agent_span(user_prompt, agent, model, model_settings):
6564
# If we can't extract tools, just skip it
6665
pass
6766

68-
# Add user prompt and system prompts if available and PII is allowed
69-
if should_send_default_pii():
67+
# Add user prompt and system prompts if available and prompts are enabled
68+
if _should_send_prompts():
7069
messages = []
7170

7271
# Add system prompts (both instructions and system_prompt)
@@ -135,7 +134,7 @@ def invoke_agent_span(user_prompt, agent, model, model_settings):
135134
def update_invoke_agent_span(span, output):
136135
# type: (sentry_sdk.tracing.Span, Any) -> None
137136
"""Update and close the invoke agent span."""
138-
if span and should_send_default_pii() and output:
137+
if span and _should_send_prompts() and output:
139138
output_text = str(output) if not isinstance(output, str) else output
140139
set_data_normalized(
141140
span, SPANDATA.GEN_AI_RESPONSE_TEXT, output_text, unpack=False

sentry_sdk/integrations/pydantic_ai/utils.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
raise DidNotEnable("pydantic-ai not installed")
2020

2121

22+
def _should_send_prompts():
23+
# type: () -> bool
24+
"""
25+
Check if prompts should be sent to Sentry.
26+
27+
This checks both send_default_pii and the include_prompts integration setting.
28+
"""
29+
if not should_send_default_pii():
30+
return False
31+
32+
# Get the integration instance from the client
33+
integration = sentry_sdk.get_client().get_integration(
34+
pydantic_ai.__name__.split(".")[0]
35+
)
36+
37+
if integration is None:
38+
return False
39+
40+
return getattr(integration, "include_prompts", True)
41+
42+
2243
def _capture_exception(exc):
2344
# type: (Any) -> None
2445
set_span_errored()
@@ -154,7 +175,7 @@ def _set_usage_data(span, usage):
154175
def _set_input_messages(span, messages):
155176
# type: (sentry_sdk.tracing.Span, Any) -> None
156177
"""Set input messages data on a span."""
157-
if not should_send_default_pii():
178+
if not _should_send_prompts():
158179
return
159180

160181
if not messages:
@@ -247,7 +268,7 @@ def _set_input_messages(span, messages):
247268
def _set_output_data(span, response):
248269
# type: (sentry_sdk.tracing.Span, Any) -> None
249270
"""Set output data on a span."""
250-
if not should_send_default_pii():
271+
if not _should_send_prompts():
251272
return
252273

253274
if not response:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import pytest
2+
3+
pytest.importorskip("pydantic_ai")

0 commit comments

Comments
 (0)