Skip to content

fix(cohere): end the span when an async v2 chat stream fails or is abandoned - #4648

Open
pcbeingused333 wants to merge 1 commit into
traceloop:mainfrom
pcbeingused333:fix/cohere-v2-async-stream-end-span
Open

pcbeingused333 wants to merge 1 commit into
traceloop:mainfrom
pcbeingused333:fix/cohere-v2-async-stream-end-span

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Oct 20, 2026 •

Copy link
Copy Markdown

What

aprocess_chat_v2_streaming_response (async Cohere v2 chat_stream) only calls span.end() at the very end of the success path. If the stream raises part-way (dropped connection, timeout) or the caller stops iterating and closes the generator, the span is never ended, so it is never exported and the failure does not show up in traces.

The sync generator process_chat_v2_streaming_response already wraps the loop in try/finally; the async one did not.

Change

Wrap the async loop and the response handling in try/finally and end the span in finally, same shape as the sync processor. Nothing else changes: the success path still sets the response attributes and the OK status before the span ends.

Tests

New test_cohere_v2_stream_ends_span_when_stream_fails, parametrised over the sync and async processors: a v2 stream that yields message-start and content-start and then raises ConnectionError. Both must leave one finished span. The async case fails on main (0 spans) and passes with the change.

packages/opentelemetry-instrumentation-cohere tests: 27 passed. ruff check clean.

This PR was made with Claude Code (Anthropic); the tests above were run on main and on this branch.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability of asynchronous streaming responses so telemetry spans are finalized even when a stream fails.
    • Connection errors continue to propagate correctly while ensuring exactly one span is completed.

…andoned

aprocess_chat_v2_streaming_response only called span.end() on the success
path, after the stream had been consumed to the end. If the stream raised
mid-way (dropped connection, timeout) or the caller stopped iterating and
closed it, the span was never ended, so it was never exported and the
failure was invisible in traces.

The sync generator process_chat_v2_streaming_response already wraps the
loop in try/finally. Do the same in the async one.

Test: a v2 stream that raises after two events must still produce one
finished span, for both the sync and the async processor (the async case
failed before the change).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7a930322-5b5c-499b-8a76-4f3475b1e835
📥 Commits

Reviewing files that changed from the base of the PR and between 8d9c93f and 6b37c32.

📒 Files selected for processing: 2
  • packages/opentelemetry-instrumentation-cohere/opentelemetry/instrumentation/cohere/streaming.py
  • packages/opentelemetry-instrumentation-cohere/tests/test_chat.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The v2 asynchronous Cohere streaming processor now ends its span after normal completion or failure. A parametrized test checks that sync and async processors propagate ConnectionError and export one span when their streams fail.

Changes: Cohere streaming span finalization

Layer / File(s) Summary
Finalize spans for streaming responses
packages/opentelemetry-instrumentation-cohere/opentelemetry/instrumentation/cohere/streaming.py, packages/opentelemetry-instrumentation-cohere/tests/test_chat.py
The v2 asynchronous processor ends its span in a finally clause. Tests verify that failing sync and async streams propagate ConnectionError and export one span.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: dinmukhamedm

Merge Risk: ⚪ Minimal · up to 6b37c

No actionable merge-blocking issue remains; the streaming span change is ready for normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: ending the span when an asynchronous Cohere v2 chat stream fails or is abandoned.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

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