X Tutup
Skip to content

Commit 5089f11

Browse files
cursoragentclaude
andcommitted
fix(cohere): Set gen_ai attributes before try block and handle chatbot role locally
- Move GEN_AI_SYSTEM and GEN_AI_OPERATION_NAME set_data calls before the try block so they are always present on spans, even when exceptions occur - Remove Cohere-specific 'chatbot' role from global GEN_AI_MESSAGE_ROLE_REVERSE_MAPPING and handle the chatbot->assistant conversion locally in the Cohere integration Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
1 parent b6346a3 commit 5089f11

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

sentry_sdk/ai/utils.py

Lines changed: 1 addition & 1 deletion
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", "chatbot"],
33+
GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai"],
3434
GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL: ["tool", "tool_call"],
3535
}
3636

sentry_sdk/integrations/cohere.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any":
141141
origin=CohereIntegration.origin,
142142
)
143143
span.__enter__()
144+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere")
145+
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
144146
try:
145147
res = f(*args, **kwargs)
146148
except Exception as e:
@@ -151,15 +153,16 @@ def new_chat(*args: "Any", **kwargs: "Any") -> "Any":
151153
reraise(*exc_info)
152154

153155
with capture_internal_exceptions():
154-
span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere")
155-
span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
156156

157157
if should_send_default_pii() and integration.include_prompts:
158158
messages = []
159159
for x in kwargs.get("chat_history", []):
160+
role = getattr(x, "role", "")
161+
if role == "chatbot":
162+
role = "assistant"
160163
messages.append(
161164
{
162-
"role": getattr(x, "role", ""),
165+
"role": role,
163166
"content": getattr(x, "message", ""),
164167
}
165168
)

0 commit comments

Comments
 (0)
X Tutup