Skip to content

Commit b6346a3

Browse files
committed
fix role mapping
1 parent bdd774f commit b6346a3

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

sentry_sdk/ai/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class GEN_AI_ALLOWED_MESSAGE_ROLES:
3030
GEN_AI_MESSAGE_ROLE_REVERSE_MAPPING = {
3131
GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM: ["system"],
3232
GEN_AI_ALLOWED_MESSAGE_ROLES.USER: ["user", "human"],
33-
GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai"],
33+
GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai", "chatbot"],
3434
GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL: ["tool", "tool_call"],
3535
}
3636

@@ -503,7 +503,7 @@ def normalize_message_role(role: str) -> str:
503503
Normalize a message role to one of the 4 allowed gen_ai role values.
504504
Maps "ai" -> "assistant" and keeps other standard roles unchanged.
505505
"""
506-
return GEN_AI_MESSAGE_ROLE_MAPPING.get(role, role)
506+
return GEN_AI_MESSAGE_ROLE_MAPPING.get(role.lower(), role)
507507

508508

509509
def normalize_message_roles(messages: "list[dict[str, Any]]") -> "list[dict[str, Any]]":

sentry_sdk/integrations/cohere.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
from sentry_sdk import consts
55
from sentry_sdk.ai.monitoring import record_token_usage
66
from sentry_sdk.consts import OP, SPANDATA
7-
from sentry_sdk.ai.utils import (
8-
set_data_normalized,
9-
normalize_message_roles,
10-
)
7+
from sentry_sdk.ai.utils import set_data_normalized, normalize_message_roles
118

129
from typing import TYPE_CHECKING
1310

@@ -154,18 +151,15 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any":
154151
reraise(*exc_info)
155152

156153
with capture_internal_exceptions():
157-
set_data_normalized(span, SPANDATA.GEN_AI_SYSTEM, "cohere")
158-
set_data_normalized(span, SPANDATA.GEN_AI_OPERATION_NAME, "chat")
154+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere")
155+
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
159156

160157
if should_send_default_pii() and integration.include_prompts:
161158
messages = []
162159
for x in kwargs.get("chat_history", []):
163-
role = getattr(x, "role", "").lower()
164-
if role == "chatbot":
165-
role = "assistant"
166160
messages.append(
167161
{
168-
"role": role,
162+
"role": getattr(x, "role", ""),
169163
"content": getattr(x, "message", ""),
170164
}
171165
)

0 commit comments

Comments
 (0)