Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 5 additions & 17 deletions sentry_sdk/integrations/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,36 +234,24 @@ def _set_responses_api_input_data(
)

model = kwargs.get("model")
if model is not None and _is_given(model):
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MODEL, model)
if model is not None:
span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model)

stream = kwargs.get("stream")
if stream is not None and _is_given(stream):
set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_STREAMING, stream)
span.set_data(SPANDATA.GEN_AI_RESPONSE_STREAMING, stream)

max_tokens = kwargs.get("max_output_tokens")
if max_tokens is not None and _is_given(max_tokens):
span.set_data(SPANDATA.GEN_AI_REQUEST_MAX_TOKENS, max_tokens)

presence_penalty = kwargs.get("presence_penalty")
if presence_penalty is not None and _is_given(presence_penalty):
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_PRESENCE_PENALTY, presence_penalty
)

frequency_penalty = kwargs.get("frequency_penalty")
if frequency_penalty is not None and _is_given(frequency_penalty):
set_data_normalized(
span, SPANDATA.GEN_AI_REQUEST_FREQUENCY_PENALTY, frequency_penalty
)

temperature = kwargs.get("temperature")
if temperature is not None and _is_given(temperature):
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)
span.set_data(SPANDATA.GEN_AI_REQUEST_TEMPERATURE, temperature)

top_p = kwargs.get("top_p")
if top_p is not None and _is_given(top_p):
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)
span.set_data(SPANDATA.GEN_AI_REQUEST_TOP_P, top_p)

if not should_send_default_pii() or not integration.include_prompts:
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "responses")
Expand Down
24 changes: 24 additions & 0 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,8 @@ def test_ai_client_span_responses_api_no_pii(sentry_init, capture_events):
instructions="You are a coding assistant that talks like a pirate.",
input="How do I check if a Python object is an instance of a class?",
max_output_tokens=100,
temperature=0.7,
top_p=0.9,
)

(transaction,) = events
Expand All @@ -1778,6 +1780,8 @@ def test_ai_client_span_responses_api_no_pii(sentry_init, capture_events):
assert spans[0]["data"] == {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.request.temperature": 0.7,
"gen_ai.request.top_p": 0.9,
"gen_ai.request.model": "gpt-4o",
"gen_ai.response.model": "response-model-id",
"gen_ai.system": "openai",
Expand Down Expand Up @@ -1879,6 +1883,8 @@ def test_ai_client_span_responses_api(
instructions=instructions,
input=input,
max_output_tokens=100,
temperature=0.7,
top_p=0.9,
)

(transaction,) = events
Expand All @@ -1891,6 +1897,8 @@ def test_ai_client_span_responses_api(
expected_data = {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.request.temperature": 0.7,
"gen_ai.request.top_p": 0.9,
"gen_ai.system": "openai",
"gen_ai.response.model": "response-model-id",
"gen_ai.usage.input_tokens": 20,
Expand Down Expand Up @@ -2183,6 +2191,8 @@ async def test_ai_client_span_responses_async_api(
instructions=instructions,
input=input,
max_output_tokens=100,
temperature=0.7,
top_p=0.9,
)

(transaction,) = events
Expand All @@ -2195,6 +2205,8 @@ async def test_ai_client_span_responses_async_api(
expected_data = {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.request.temperature": 0.7,
"gen_ai.request.top_p": 0.9,
"gen_ai.request.messages": '["How do I check if a Python object is an instance of a class?"]',
"gen_ai.request.model": "gpt-4o",
"gen_ai.response.model": "response-model-id",
Expand Down Expand Up @@ -2454,6 +2466,8 @@ async def test_ai_client_span_streaming_responses_async_api(
input=input,
stream=True,
max_output_tokens=100,
temperature=0.7,
top_p=0.9,
)
async for _ in result:
pass
Expand All @@ -2468,6 +2482,8 @@ async def test_ai_client_span_streaming_responses_async_api(
expected_data = {
"gen_ai.operation.name": "responses",
"gen_ai.request.max_tokens": 100,
"gen_ai.request.temperature": 0.7,
"gen_ai.request.top_p": 0.9,
"gen_ai.response.model": "response-model-id",
"gen_ai.response.streaming": True,
"gen_ai.system": "openai",
Expand Down Expand Up @@ -2781,6 +2797,8 @@ def test_streaming_responses_api(
input="hello",
stream=True,
max_output_tokens=100,
temperature=0.7,
top_p=0.9,
)

response_string = ""
Expand All @@ -2795,6 +2813,8 @@ def test_streaming_responses_api(
assert span["op"] == "gen_ai.responses"
assert span["data"][SPANDATA.GEN_AI_SYSTEM] == "openai"
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9

assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id"

Expand Down Expand Up @@ -2841,6 +2861,8 @@ async def test_streaming_responses_api_async(
input="hello",
stream=True,
max_output_tokens=100,
temperature=0.7,
top_p=0.9,
)

response_string = ""
Expand All @@ -2855,6 +2877,8 @@ async def test_streaming_responses_api_async(
assert span["op"] == "gen_ai.responses"
assert span["data"][SPANDATA.GEN_AI_SYSTEM] == "openai"
assert span["data"][SPANDATA.GEN_AI_REQUEST_MAX_TOKENS] == 100
assert span["data"][SPANDATA.GEN_AI_REQUEST_TEMPERATURE] == 0.7
assert span["data"][SPANDATA.GEN_AI_REQUEST_TOP_P] == 0.9

assert span["data"][SPANDATA.GEN_AI_RESPONSE_MODEL] == "response-model-id"

Expand Down
Loading