Skip to content

Commit c6f5cd2

Browse files
committed
Change strip PII semantics
1 parent 4bc4310 commit c6f5cd2

File tree

3 files changed

+85
-37
lines changed

3 files changed

+85
-37
lines changed

sentry_sdk/consts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ class OP:
219219
MIDDLEWARE_STARLITE = "middleware.starlite"
220220
MIDDLEWARE_STARLITE_RECEIVE = "middleware.starlite.receive"
221221
MIDDLEWARE_STARLITE_SEND = "middleware.starlite.send"
222-
OPENAI_CHAT_COMPLETIONS_CREATE = "openai.chat_completions.create"
223-
OPENAI_EMBEDDINGS_CREATE = "openai.embeddings.create"
222+
OPENAI_CHAT_COMPLETIONS_CREATE = "ai.chat_completions.create.openai"
223+
OPENAI_EMBEDDINGS_CREATE = "ai.embeddings.create.openai"
224224
QUEUE_SUBMIT_ARQ = "queue.submit.arq"
225225
QUEUE_TASK_ARQ = "queue.task.arq"
226226
QUEUE_SUBMIT_CELERY = "queue.submit.celery"

sentry_sdk/integrations/openai.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def count_tokens(s):
5050
class OpenAIIntegration(Integration):
5151
identifier = "openai"
5252

53-
def __init__(self, include_prompts=False):
53+
def __init__(self, include_prompts=True):
5454
# type: (OpenAIIntegration, bool) -> None
5555
self.include_prompts = include_prompts
5656

@@ -159,13 +159,13 @@ def new_chat_completion(*args, **kwargs):
159159
raise e from None
160160

161161
with capture_internal_exceptions():
162-
if _should_send_default_pii() or integration.include_prompts:
162+
if _should_send_default_pii() and integration.include_prompts:
163163
span.set_data("ai.input_messages", messages)
164164
span.set_data("ai.model_id", model)
165165
span.set_data("ai.streaming", streaming)
166166

167167
if hasattr(res, "choices"):
168-
if _should_send_default_pii() or integration.include_prompts:
168+
if _should_send_default_pii() and integration.include_prompts:
169169
span.set_data(
170170
"ai.responses", list(map(lambda x: x.message, res.choices))
171171
)
@@ -198,7 +198,7 @@ def new_iterator():
198198
)
199199
if (
200200
_should_send_default_pii()
201-
or integration.include_prompts
201+
and integration.include_prompts
202202
):
203203
span.set_data("ai.responses", all_responses)
204204
_calculate_chat_completion_usage(
@@ -235,7 +235,7 @@ def new_embeddings_create(*args, **kwargs):
235235
description="OpenAI Embedding Creation",
236236
) as span:
237237
if "input" in kwargs and (
238-
_should_send_default_pii() or integration.include_prompts
238+
_should_send_default_pii() and integration.include_prompts
239239
):
240240
if isinstance(kwargs["input"], str):
241241
span.set_data("ai.input_messages", [kwargs["input"]])

tests/integrations/openai/test_openai.py

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,39 @@
1717
from unittest import mock # python 3.3 and above
1818

1919

20+
EXAMPLE_CHAT_COMPLETION = ChatCompletion(
21+
id="chat-id",
22+
choices=[
23+
Choice(
24+
index=0,
25+
finish_reason="stop",
26+
message=ChatCompletionMessage(
27+
role="assistant", content="the model response"
28+
),
29+
)
30+
],
31+
created=10000000,
32+
model="model-id",
33+
object="chat.completion",
34+
usage=CompletionUsage(
35+
completion_tokens=10,
36+
prompt_tokens=20,
37+
total_tokens=30,
38+
),
39+
)
40+
41+
2042
def test_nonstreaming_chat_completion(sentry_init, capture_events):
2143
sentry_init(
22-
integrations=[OpenAIIntegration(include_prompts=True)], traces_sample_rate=1.0
44+
integrations=[OpenAIIntegration()],
45+
traces_sample_rate=1.0,
46+
send_default_pii=True,
2347
)
2448
events = capture_events()
2549

2650
client = OpenAI(api_key="z")
27-
returned_chat = ChatCompletion(
28-
id="chat-id",
29-
choices=[
30-
Choice(
31-
index=0,
32-
finish_reason="stop",
33-
message=ChatCompletionMessage(role="assistant", content="response"),
34-
)
35-
],
36-
created=10000000,
37-
model="model-id",
38-
object="chat.completion",
39-
usage=CompletionUsage(
40-
completion_tokens=10,
41-
prompt_tokens=20,
42-
total_tokens=30,
43-
),
44-
)
51+
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
4552

46-
client.chat.completions._post = mock.Mock(return_value=returned_chat)
4753
with start_transaction(name="openai tx"):
4854
response = (
4955
client.chat.completions.create(
@@ -53,17 +59,63 @@ def test_nonstreaming_chat_completion(sentry_init, capture_events):
5359
.message.content
5460
)
5561

56-
assert response == "response"
62+
assert response == "the model response"
5763
tx = events[0]
5864
assert tx["type"] == "transaction"
5965
span = tx["spans"][0]
60-
assert span["op"] == "openai.chat_completions.create"
66+
assert span["op"] == "ai.chat_completions.create.openai"
67+
assert "the model response" in span["data"]["ai.responses"][0]
6168

6269
assert span["data"][COMPLETION_TOKENS_USED] == 10
6370
assert span["data"][PROMPT_TOKENS_USED] == 20
6471
assert span["data"][TOTAL_TOKENS_USED] == 30
6572

6673

74+
def test_stripped_pii_without_send_default_pii(sentry_init, capture_events):
75+
sentry_init(
76+
integrations=[OpenAIIntegration()],
77+
traces_sample_rate=1.0,
78+
)
79+
events = capture_events()
80+
81+
client = OpenAI(api_key="z")
82+
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
83+
84+
with start_transaction(name="openai tx"):
85+
client.chat.completions.create(
86+
model="some-model", messages=[{"role": "system", "content": "hello"}]
87+
)
88+
89+
tx = events[0]
90+
assert tx["type"] == "transaction"
91+
span = tx["spans"][0]
92+
assert "ai.input_messages" not in span["data"]
93+
assert "ai.responses" not in span["data"]
94+
95+
96+
def test_stripped_pii_without_send_prompts(sentry_init, capture_events):
97+
sentry_init(
98+
integrations=[OpenAIIntegration(include_prompts=False)],
99+
traces_sample_rate=1.0,
100+
send_default_pii=True,
101+
)
102+
events = capture_events()
103+
104+
client = OpenAI(api_key="z")
105+
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
106+
107+
with start_transaction(name="openai tx"):
108+
client.chat.completions.create(
109+
model="some-model", messages=[{"role": "system", "content": "hello"}]
110+
)
111+
112+
tx = events[0]
113+
assert tx["type"] == "transaction"
114+
span = tx["spans"][0]
115+
assert "ai.input_messages" not in span["data"]
116+
assert "ai.responses" not in span["data"]
117+
118+
67119
# noinspection PyTypeChecker
68120
def test_streaming_chat_completion(sentry_init, capture_events):
69121
sentry_init(
@@ -121,7 +173,7 @@ def test_streaming_chat_completion(sentry_init, capture_events):
121173
tx = events[0]
122174
assert tx["type"] == "transaction"
123175
span = tx["spans"][0]
124-
assert span["op"] == "openai.chat_completions.create"
176+
assert span["op"] == "ai.chat_completions.create.openai"
125177

126178
try:
127179
import tiktoken # type: ignore # noqa # pylint: disable=unused-import
@@ -134,9 +186,7 @@ def test_streaming_chat_completion(sentry_init, capture_events):
134186

135187

136188
def test_bad_chat_completion(sentry_init, capture_events):
137-
sentry_init(
138-
integrations=[OpenAIIntegration(include_prompts=True)], traces_sample_rate=1.0
139-
)
189+
sentry_init(integrations=[OpenAIIntegration()], traces_sample_rate=1.0)
140190
events = capture_events()
141191

142192
client = OpenAI(api_key="z")
@@ -153,9 +203,7 @@ def test_bad_chat_completion(sentry_init, capture_events):
153203

154204

155205
def test_embeddings_create(sentry_init, capture_events):
156-
sentry_init(
157-
integrations=[OpenAIIntegration(include_prompts=True)], traces_sample_rate=1.0
158-
)
206+
sentry_init(integrations=[OpenAIIntegration()], traces_sample_rate=1.0)
159207
events = capture_events()
160208

161209
client = OpenAI(api_key="z")
@@ -181,7 +229,7 @@ def test_embeddings_create(sentry_init, capture_events):
181229
tx = events[0]
182230
assert tx["type"] == "transaction"
183231
span = tx["spans"][0]
184-
assert span["op"] == "openai.embeddings.create"
232+
assert span["op"] == "ai.embeddings.create.openai"
185233

186234
assert span["data"][PROMPT_TOKENS_USED] == 20
187235
assert span["data"][TOTAL_TOKENS_USED] == 30

0 commit comments

Comments
 (0)