Skip to content

Commit 906da3d

Browse files
committed
map openai
1 parent f680921 commit 906da3d

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

sentry_sdk/ai/utils.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@
1010
from sentry_sdk.utils import logger
1111

1212

13+
class GEN_AI_ALLOWED_MESSAGE_ROLES:
14+
SYSTEM = "system"
15+
USER = "user"
16+
ASSISTANT = "assistant"
17+
TOOL_CALL = "tool_call"
18+
19+
1320
# Gen AI message role reverse mapping showing allowed target roles and their source variants
1421
GEN_AI_MESSAGE_ROLE_REVERSE_MAPPING = {
15-
"system": ["system"],
16-
"user": ["user"],
22+
GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM: ["system"],
23+
GEN_AI_ALLOWED_MESSAGE_ROLES.USER: ["user"],
1724
"assistant": ["assistant", "ai"],
1825
"tool_call": ["tool_call"],
1926
}
@@ -70,15 +77,8 @@ def normalize_message_roles(messages):
7077
Normalize roles in a list of messages to use standard gen_ai role values.
7178
Creates a deep copy to avoid modifying the original messages.
7279
"""
73-
if not messages:
74-
return messages
75-
7680
normalized_messages = []
7781
for message in messages:
78-
if not isinstance(message, dict):
79-
normalized_messages.append(message)
80-
continue
81-
8282
normalized_message = message.copy()
8383
if "role" in message:
8484
normalized_message["role"] = normalize_message_role(message["role"])

sentry_sdk/integrations/openai_agents/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import sentry_sdk
2-
from sentry_sdk.ai.utils import set_data_normalized, normalize_message_role
2+
from sentry_sdk.ai.utils import (
3+
normalize_message_roles,
4+
set_data_normalized,
5+
normalize_message_role,
6+
)
37
from sentry_sdk.consts import SPANDATA, SPANSTATUS, OP
48
from sentry_sdk.integrations import DidNotEnable
59
from sentry_sdk.scope import should_send_default_pii
@@ -121,7 +125,10 @@ def _set_input_data(span, get_response_kwargs):
121125
request_messages.append({"role": "tool", "content": [message]})
122126

123127
set_data_normalized(
124-
span, SPANDATA.GEN_AI_REQUEST_MESSAGES, request_messages, unpack=False
128+
span,
129+
SPANDATA.GEN_AI_REQUEST_MESSAGES,
130+
normalize_message_roles(request_messages),
131+
unpack=False,
125132
)
126133

127134

0 commit comments

Comments
 (0)