Skip to content

Commit 67b768a

Browse files
openai tests
1 parent 7aedbde commit 67b768a

1 file changed

Lines changed: 28 additions & 36 deletions

File tree

tests/integrations/openai/test_openai.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
OpenAIIntegration,
4545
_calculate_token_usage,
4646
)
47-
from sentry_sdk.ai.utils import MAX_GEN_AI_MESSAGE_BYTES
4847
from sentry_sdk._types import AnnotatedValue
4948
from sentry_sdk.serializer import serialize
5049

@@ -1458,7 +1457,25 @@ def test_empty_tools_in_chat_completion(sentry_init, capture_events, tools):
14581457
assert "gen_ai.request.available_tools" not in span["data"]
14591458

14601459

1461-
def test_openai_message_role_mapping(sentry_init, capture_events):
1460+
# Test messages with mixed roles including "ai" that should be mapped to "assistant"
1461+
@pytest.mark.parametrize(
1462+
"test_message,expected_role",
1463+
[
1464+
({"role": "system", "content": "You are helpful."}, "system"),
1465+
({"role": "user", "content": "Hello"}, "user"),
1466+
(
1467+
{"role": "ai", "content": "Hi there!"},
1468+
"assistant",
1469+
), # Should be mapped to "assistant"
1470+
(
1471+
{"role": "assistant", "content": "How can I help?"},
1472+
"assistant",
1473+
), # Should stay "assistant"
1474+
],
1475+
)
1476+
def test_openai_message_role_mapping(
1477+
sentry_init, capture_events, test_message, expected_role
1478+
):
14621479
"""Test that OpenAI integration properly maps message roles like 'ai' to 'assistant'"""
14631480

14641481
sentry_init(
@@ -1470,13 +1487,8 @@ def test_openai_message_role_mapping(sentry_init, capture_events):
14701487

14711488
client = OpenAI(api_key="z")
14721489
client.chat.completions._post = mock.Mock(return_value=EXAMPLE_CHAT_COMPLETION)
1473-
# Test messages with mixed roles including "ai" that should be mapped to "assistant"
1474-
test_messages = [
1475-
{"role": "system", "content": "You are helpful."},
1476-
{"role": "user", "content": "Hello"},
1477-
{"role": "ai", "content": "Hi there!"}, # Should be mapped to "assistant"
1478-
{"role": "assistant", "content": "How can I help?"}, # Should stay "assistant"
1479-
]
1490+
1491+
test_messages = [test_message]
14801492

14811493
with start_transaction(name="openai tx"):
14821494
client.chat.completions.create(model="test-model", messages=test_messages)
@@ -1491,22 +1503,8 @@ def test_openai_message_role_mapping(sentry_init, capture_events):
14911503

14921504
stored_messages = json.loads(span["data"][SPANDATA.GEN_AI_REQUEST_MESSAGES])
14931505

1494-
# Verify that "ai" role was mapped to "assistant"
1495-
assert len(stored_messages) == 4
1496-
assert stored_messages[0]["role"] == "system"
1497-
assert stored_messages[1]["role"] == "user"
1498-
assert (
1499-
stored_messages[2]["role"] == "assistant"
1500-
) # "ai" should be mapped to "assistant"
1501-
assert stored_messages[3]["role"] == "assistant" # should stay "assistant"
1502-
1503-
# Verify content is preserved
1504-
assert stored_messages[2]["content"] == "Hi there!"
1505-
assert stored_messages[3]["content"] == "How can I help?"
1506-
1507-
# Verify no "ai" roles remain
1508-
roles = [msg["role"] for msg in stored_messages]
1509-
assert "ai" not in roles
1506+
assert len(stored_messages) == 1
1507+
assert stored_messages[0]["role"] == expected_role
15101508

15111509

15121510
def test_openai_message_truncation(sentry_init, capture_events):
@@ -1548,14 +1546,8 @@ def test_openai_message_truncation(sentry_init, capture_events):
15481546
assert isinstance(parsed_messages, list)
15491547
assert len(parsed_messages) <= len(large_messages)
15501548

1551-
if "_meta" in event and len(parsed_messages) < len(large_messages):
1552-
meta_path = event["_meta"]
1553-
if (
1554-
"spans" in meta_path
1555-
and "0" in meta_path["spans"]
1556-
and "data" in meta_path["spans"]["0"]
1557-
):
1558-
span_meta = meta_path["spans"]["0"]["data"]
1559-
if SPANDATA.GEN_AI_REQUEST_MESSAGES in span_meta:
1560-
messages_meta = span_meta[SPANDATA.GEN_AI_REQUEST_MESSAGES]
1561-
assert "len" in messages_meta.get("", {})
1549+
meta_path = event["_meta"]
1550+
span_meta = meta_path["spans"]["0"]["data"]
1551+
if SPANDATA.GEN_AI_REQUEST_MESSAGES in span_meta:
1552+
messages_meta = span_meta[SPANDATA.GEN_AI_REQUEST_MESSAGES]
1553+
assert "len" in messages_meta.get("", {})

0 commit comments

Comments
 (0)