Skip to content

Commit e79d3c7

Browse files
committed
handle pii
1 parent 78064e7 commit e79d3c7

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

sentry_sdk/integrations/openai_agents/patches/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def wrapped_get_model(cls, agent, run_config):
2626

2727
@wraps(original_get_response)
2828
async def wrapped_get_response(*args, **kwargs):
29-
with ai_client_span(agent, model, run_config, kwargs) as span:
29+
with ai_client_span(agent, kwargs) as span:
3030
result = await original_get_response(*args, **kwargs)
31-
update_ai_client_span(span, agent, model, run_config, kwargs, result)
31+
update_ai_client_span(span, agent, kwargs, result)
3232

3333
return result
3434

sentry_sdk/integrations/openai_agents/spans/ai_client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
from typing import TYPE_CHECKING
77

88
if TYPE_CHECKING:
9-
from agents import Agent, Model, RunConfig
9+
from agents import Agent
10+
from typing import Any
1011
from sentry_sdk import Span
1112

1213

13-
def ai_client_span(agent, model, run_config, get_response_kwargs):
14-
# type: (Agent, Model, RunConfig, dict) -> Span
14+
def ai_client_span(agent, get_response_kwargs):
15+
# type: (Agent, dict[str, Any]) -> Span
1516
# TODO-anton: implement other types of operations. Now "chat" is hardcoded.
1617
span = sentry_sdk.start_span(
1718
op=OP.GEN_AI_CHAT,
@@ -23,7 +24,8 @@ def ai_client_span(agent, model, run_config, get_response_kwargs):
2324
return span
2425

2526

26-
def update_ai_client_span(span, agent, model, run_config, get_response_kwargs, result):
27+
def update_ai_client_span(span, agent, get_response_kwargs, result):
28+
# type: (Span, Agent, dict[str, Any], Any) -> None
2729
_set_agent_data(span, agent)
2830
_set_usage_data(span, result.usage)
2931
_set_input_data(span, get_response_kwargs)

sentry_sdk/integrations/openai_agents/spans/execute_tool.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sentry_sdk
22
from sentry_sdk.consts import OP, SPANDATA
3+
from sentry_sdk.scope import should_send_default_pii
34

45
from ..utils import _set_agent_data
56

@@ -18,19 +19,23 @@ def execute_tool_span(tool, *args, **kwargs):
1819
)
1920

2021
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "execute_tool")
21-
span.set_data(SPANDATA.GEN_AI_TOOL_NAME, tool.name)
22-
span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool.description)
23-
24-
input = args[1]
25-
span.set_data(SPANDATA.GEN_AI_TOOL_INPUT, input)
2622

2723
if tool.__class__.__name__ == "FunctionTool":
2824
span.set_data(SPANDATA.GEN_AI_TOOL_TYPE, "function")
2925

26+
span.set_data(SPANDATA.GEN_AI_TOOL_NAME, tool.name)
27+
span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool.description)
28+
29+
if should_send_default_pii():
30+
input = args[1]
31+
span.set_data(SPANDATA.GEN_AI_TOOL_INPUT, input)
32+
3033
return span
3134

3235

3336
def update_execute_tool_span(span, agent, tool, result):
3437
# type: (Span, agents.Agent, agents.Tool, Any) -> None
3538
_set_agent_data(span, agent)
36-
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, result)
39+
40+
if should_send_default_pii():
41+
span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, result)

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from functools import wraps
22

33
import sentry_sdk
4+
from sentry_sdk.consts import SPANDATA
45
from sentry_sdk.integrations import DidNotEnable
6+
from sentry_sdk.scope import should_send_default_pii
57
from sentry_sdk.utils import event_from_exception
6-
from sentry_sdk.consts import SPANDATA
78

89
from typing import TYPE_CHECKING
910

@@ -154,6 +155,9 @@ def _set_usage_data(span, usage):
154155

155156
def _set_input_data(span, get_response_kwargs):
156157
# type: (sentry_sdk.Span, dict[str, Any]) -> None
158+
if not should_send_default_pii():
159+
return
160+
157161
messages_by_role = {
158162
"system": [],
159163
"user": [],
@@ -185,6 +189,9 @@ def _set_input_data(span, get_response_kwargs):
185189

186190
def _set_output_data(span, result):
187191
# type: (sentry_sdk.Span, Any) -> None
192+
if not should_send_default_pii():
193+
return
194+
188195
output_messages = {
189196
"response": [],
190197
"tool": [],

0 commit comments

Comments
 (0)