feat(cohere): upgrade integration from ai to gen_ai#5597
feat(cohere): upgrade integration from ai to gen_ai#5597
Conversation
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨
Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧Openai Agents
Other
🤖 This preview updates automatically when you update the PR. |
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 14.70s All tests are passing successfully. ❌ Patch coverage is 4.00%. Project has 13962 uncovered lines. Files with missing lines (1)
Generated by Codecov Action |
| if integration is None: | ||
| return f(*args, **kwargs) | ||
|
|
||
| model = kwargs.get("model", "") |
There was a problem hiding this comment.
the documentation currently does not specify the AI Client Span's description when there is no request model.
I'll bring this up in the sync, update the devdocs, and ping you once there's a concrete spec.
https://develop.sentry.dev/sdk/telemetry/traces/modules/ai-agents/
sentry_sdk/integrations/cohere.py
Outdated
| messages = [] | ||
| for x in kwargs.get("chat_history", []): | ||
| role = getattr(x, "role", "").lower() | ||
| if role == "chatbot": | ||
| role = "assistant" | ||
| messages.append( | ||
| { | ||
| "role": role, | ||
| "content": getattr(x, "message", ""), | ||
| } | ||
| ) | ||
| messages.append({"role": "user", "content": message}) | ||
| messages = normalize_message_roles(messages) |
There was a problem hiding this comment.
Either you should explicitly convert message roles in cohere inputs or rely on solely on normalize_message_roles().
Also, we are developing against a library with specific schemas and types.
Heuristics like normalize_message_roles should be avoided as they effectively add dead code.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Autofix Details
Bugbot Autofix prepared fixes for both issues found in the latest run.
- ✅ Fixed: System/operation attributes missing on error spans
- Moved 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.
- ✅ Fixed: Cohere-specific role added to global mapping utility
- Removed 'chatbot' from the global GEN_AI_MESSAGE_ROLE_REVERSE_MAPPING and handled the chatbot->assistant role conversion locally in the Cohere integration.
Or push these changes by commenting:
@cursor push 5089f11b7f
Preview (5089f11b7f)
diff --git a/sentry_sdk/ai/utils.py b/sentry_sdk/ai/utils.py
--- a/sentry_sdk/ai/utils.py
+++ b/sentry_sdk/ai/utils.py
@@ -30,7 +30,7 @@
GEN_AI_MESSAGE_ROLE_REVERSE_MAPPING = {
GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM: ["system"],
GEN_AI_ALLOWED_MESSAGE_ROLES.USER: ["user", "human"],
- GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai", "chatbot"],
+ GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai"],
GEN_AI_ALLOWED_MESSAGE_ROLES.TOOL: ["tool", "tool_call"],
}
diff --git a/sentry_sdk/integrations/cohere.py b/sentry_sdk/integrations/cohere.py
--- a/sentry_sdk/integrations/cohere.py
+++ b/sentry_sdk/integrations/cohere.py
@@ -141,6 +141,8 @@
origin=CohereIntegration.origin,
)
span.__enter__()
+ span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere")
+ span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
try:
res = f(*args, **kwargs)
except Exception as e:
@@ -151,15 +153,16 @@
reraise(*exc_info)
with capture_internal_exceptions():
- span.set_data(SPANDATA.GEN_AI_SYSTEM, "cohere")
- span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat")
if should_send_default_pii() and integration.include_prompts:
messages = []
for x in kwargs.get("chat_history", []):
+ role = getattr(x, "role", "")
+ if role == "chatbot":
+ role = "assistant"
messages.append(
{
- "role": getattr(x, "role", ""),
+ "role": role,
"content": getattr(x, "message", ""),
}
)There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
6d73875 to
024cbff
Compare

Closes TET-2023