Skip to content

Commit 62ef327

Browse files
committed
fix(integrations): improve embeddings support for openai
1 parent 6d2e663 commit 62ef327

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

sentry_sdk/consts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,12 @@ class SPANDATA:
465465
Example: "The weather in Paris is rainy and overcast, with temperatures around 57°F"
466466
"""
467467

468+
GEN_AI_EMBEDDINGS_INPUT = "gen_ai.embeddings.input"
469+
"""
470+
The input to the embeddings operation.
471+
Example: "Hello!"
472+
"""
473+
468474
GEN_AI_OPERATION_NAME = "gen_ai.operation.name"
469475
"""
470476
The name of the operation being performed.

sentry_sdk/integrations/openai.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,15 @@ def _set_input_data(span, kwargs, operation, integration):
195195
scope = sentry_sdk.get_current_scope()
196196
messages_data = truncate_and_annotate_messages(normalized_messages, span, scope)
197197
if messages_data is not None:
198-
set_data_normalized(
199-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
200-
)
198+
# Use appropriate field based on operation type
199+
if operation == "embeddings":
200+
set_data_normalized(
201+
span, SPANDATA.GEN_AI_EMBEDDINGS_INPUT, messages_data, unpack=False
202+
)
203+
else:
204+
set_data_normalized(
205+
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, messages_data, unpack=False
206+
)
201207

202208
# Input attributes: Common
203209
set_data_normalized(span, SPANDATA.GEN_AI_SYSTEM, "openai")

tests/integrations/openai/test_openai.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,9 @@ def test_embeddings_create(
501501
span = tx["spans"][0]
502502
assert span["op"] == "gen_ai.embeddings"
503503
if send_default_pii and include_prompts:
504-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
504+
assert "hello" in span["data"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]
505505
else:
506-
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
506+
assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["data"]
507507

508508
assert span["data"]["gen_ai.usage.input_tokens"] == 20
509509
assert span["data"]["gen_ai.usage.total_tokens"] == 30
@@ -549,9 +549,9 @@ async def test_embeddings_create_async(
549549
span = tx["spans"][0]
550550
assert span["op"] == "gen_ai.embeddings"
551551
if send_default_pii and include_prompts:
552-
assert "hello" in span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES]
552+
assert "hello" in span["data"][SPANDATA.GEN_AI_EMBEDDINGS_INPUT]
553553
else:
554-
assert SPANDATA.GEN_AI_REQUEST_MESSAGES not in span["data"]
554+
assert SPANDATA.GEN_AI_EMBEDDINGS_INPUT not in span["data"]
555555

556556
assert span["data"]["gen_ai.usage.input_tokens"] == 20
557557
assert span["data"]["gen_ai.usage.total_tokens"] == 30

0 commit comments

Comments
 (0)