Skip to content

Commit db311f8

Browse files
authored
fix(openai_agents): Gate gen_ai.response.tool_calls on outputs, not inputs (#7209)
Refs PY-2734 Refs #7200
1 parent 11a8b85 commit db311f8

2 files changed

Lines changed: 7 additions & 22 deletions

File tree

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,13 @@ def _set_output_data(
209209
span: "Union[sentry_sdk.tracing.Span, StreamedSpan]", result: "Any"
210210
) -> None:
211211
client = sentry_sdk.get_client()
212-
record_inputs = False
213212
record_outputs = False
214213
if has_data_collection_enabled(client.options):
215-
record_inputs = client.options["data_collection"]["gen_ai"]["inputs"]
216214
record_outputs = client.options["data_collection"]["gen_ai"]["outputs"]
217215
elif should_send_default_pii():
218-
record_inputs = True
219216
record_outputs = True
220217

221-
if not record_inputs and not record_outputs:
218+
if not record_outputs:
222219
return
223220

224221
output_messages: "dict[str, list[Any]]" = {
@@ -237,7 +234,7 @@ def _set_output_data(
237234
# Unknown output message type, just return the json
238235
output_messages["response"].append(output_message.dict())
239236

240-
if record_inputs and len(output_messages["tool"]) > 0:
237+
if record_outputs and len(output_messages["tool"]) > 0:
241238
if isinstance(span, StreamedSpan):
242239
span.set_attribute(
243240
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,55 +1180,48 @@ async def test_data_collection_inputs(
11801180
@pytest.mark.parametrize("span_streaming", [True, False])
11811181
@pytest.mark.parametrize("stream_gen_ai_spans", [True, False])
11821182
@pytest.mark.parametrize(
1183-
"data_collection,send_default_pii,expect_output,expect_tool_calls",
1183+
"data_collection,send_default_pii,expect_output",
11841184
[
11851185
pytest.param(
11861186
{"gen_ai": {"outputs": True}},
11871187
False,
11881188
True,
1189-
True,
11901189
id="gen-ai-outputs-enabled-overrides-pii-disabled",
11911190
),
11921191
pytest.param(
11931192
{"gen_ai": {"outputs": False}},
11941193
True,
11951194
False,
1196-
True,
1197-
id="gen-ai-outputs-disabled-still-collects-tool-calls-gated-on-inputs",
1195+
id="gen-ai-outputs-disabled-overrides-pii-enabled",
11981196
),
11991197
pytest.param(
12001198
{},
12011199
False,
12021200
True,
1203-
True,
12041201
id="gen-ai-omitted-defaults-to-enabled",
12051202
),
12061203
pytest.param(
12071204
{"gen_ai": {"inputs": False, "outputs": False}},
12081205
False,
12091206
False,
1210-
False,
12111207
id="gen-ai-inputs-and-outputs-disabled-and-pii-disabled",
12121208
),
12131209
pytest.param(
12141210
{"gen_ai": {"inputs": False}},
12151211
False,
12161212
True,
1217-
False,
1218-
id="gen-ai-inputs-disabled-drops-tool-calls-only",
1213+
id="gen-ai-inputs-disabled-keeps-outputs-and-tool-calls",
12191214
),
12201215
pytest.param(
12211216
None,
12221217
False,
12231218
False,
1224-
False,
12251219
id="no-data-collection-falls-back-to-send-default-pii",
12261220
),
12271221
pytest.param(
12281222
None,
12291223
True,
12301224
True,
1231-
True,
12321225
id="no-data-collection-pii-enabled-collects",
12331226
),
12341227
],
@@ -1245,7 +1238,6 @@ async def test_data_collection_outputs(
12451238
data_collection,
12461239
send_default_pii,
12471240
expect_output,
1248-
expect_tool_calls,
12491241
stream_gen_ai_spans,
12501242
span_streaming,
12511243
):
@@ -1326,23 +1318,19 @@ async def test_data_collection_outputs(
13261318

13271319
assert len(chat_span_data) == 2
13281320

1329-
if expect_tool_calls:
1321+
if expect_output:
13301322
assert any(
13311323
"simple_test_tool" in data.get(SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS, "")
13321324
for data in chat_span_data
13331325
)
1334-
else:
1335-
for data in chat_span_data:
1336-
assert SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS not in data
1337-
1338-
if expect_output:
13391326
assert any(
13401327
"Task completed using the tool"
13411328
in str(data.get(SPANDATA.GEN_AI_RESPONSE_TEXT, ""))
13421329
for data in chat_span_data
13431330
)
13441331
else:
13451332
for data in chat_span_data:
1333+
assert SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS not in data
13461334
assert SPANDATA.GEN_AI_RESPONSE_TEXT not in data
13471335

13481336
# Non-PII data is unaffected by the gate

0 commit comments

Comments
 (0)