Skip to content

feat(cohere): upgrade integration from ai to gen_ai#5532

Draft
shellmayr wants to merge 14 commits intomasterfrom
shellmayr/feat/upgrade-cohere-ai-integration-from-ai-to-genai
Draft

feat(cohere): upgrade integration from ai to gen_ai#5532
shellmayr wants to merge 14 commits intomasterfrom
shellmayr/feat/upgrade-cohere-ai-integration-from-ai-to-genai

Conversation

@shellmayr
Copy link
Member

@shellmayr shellmayr commented Feb 25, 2026

This PR encompasses multiple changes to accomplish this

  • Add support for the "new" v2 of cohere which was released at the end of 2024. The cohere package continues to contain the v1 and v2 APIs, therefore we still need to support both. The instrumentation is in a separate file to prevent confusion between the versions.
  • Move the auto-instrumentation for v1 from ai.* attributes to gen_ai.* attributes
  • Adapt / add tests for v1 and v2
  • Has been run against two new templates for cohere in the testing framework
  • Semantically, Cohere is a model-provider on the level of e.g. anthropic and openai, meaning that there is no agentic flavor to this, it is "just" an API for LLM-calling for Cohere's own models

Closes TET-2023

@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

  • (cohere) Upgrade integration from ai to gen_ai by shellmayr in #5532

Bug Fixes 🐛

  • (celery) Propagate user-set headers by sentrivana in #5581

Documentation 📚

  • Add AGENTS.md by sentrivana in #5579
  • Add set_attribute example to changelog by sentrivana in #5578

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

Codecov Results 📊

31 passed | Total: 31 | Pass Rate: 100% | Execution Time: 14.02s

📊 Comparison with Base Branch

Metric Change
Total Tests 📉 -33
Passed Tests 📉 -33
Failed Tests
Skipped Tests

All tests are passing successfully.

❌ Patch coverage is 6.83%. Project has 14581 uncovered lines.
✅ Project coverage is 27.31%. Comparing base (base) to head (head).

Files with missing lines (5)
File Patch % Lines
v2.py 0.00% ⚠️ 113 Missing
utils.py 64.71% ⚠️ 90 Missing and 22 partials
v1.py 0.00% ⚠️ 89 Missing
__init__.py 24.56% ⚠️ 43 Missing
span_config.py 17.65% ⚠️ 28 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    26.40%    27.31%    +0.91%
==========================================
  Files          189       192        +3
  Lines        19902     20058      +156
  Branches      6464      6534       +70
==========================================
+ Hits          5255      5477      +222
- Misses       14647     14581       -66
- Partials       457       488       +31

Generated by Codecov Action

@linear
Copy link

linear bot commented Feb 27, 2026

GEN_AI_ALLOWED_MESSAGE_ROLES.SYSTEM: ["system"],
GEN_AI_ALLOWED_MESSAGE_ROLES.USER: ["user", "human"],
GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai"],
GEN_AI_ALLOWED_MESSAGE_ROLES.ASSISTANT: ["assistant", "ai", "chatbot"],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed because cohere sometimes uses the role chatbot in their message structure

with sentry_sdk.start_span(
op=OP.GEN_AI_EMBEDDINGS,
name=f"embeddings {model}".strip(),
origin=CohereIntegration.origin,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set_input_span_data call not wrapped in capture_internal_exceptions()

In _wrap_embed, the call to set_input_span_data on line 101 is not wrapped in capture_internal_exceptions(), unlike the equivalent calls in v1.py (line 131-135) and v2.py (line 188-195). If set_input_span_data raises an exception due to unexpected data types or internal errors, it will propagate to the user's code, potentially causing their embed request to fail despite the underlying Cohere API call succeeding.

Verification

Read v1.py (lines 131-135) and v2.py (lines 188-195) which both wrap set_input_span_data in capture_internal_exceptions(). Read set_input_span_data in span_config.py to verify it can raise exceptions. The init.py _wrap_embed function does not wrap line 101 in capture_internal_exceptions().

Suggested fix: Wrap the set_input_span_data call in capture_internal_exceptions() to prevent instrumentation errors from affecting user code.

Suggested change
origin=CohereIntegration.origin,
with capture_internal_exceptions():
set_input_span_data(span, kwargs, integration, COHERE_EMBED_CONFIG)

Identified by Warden code-review · HPV-RQ8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant