Skip to content

fix(cohere): close async streaming spans on errors#4376

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

fix(cohere): close async streaming spans on errors#4376
euisuh wants to merge 1 commit into
traceloop:mainfrom
euisuh:fix/cohere-async-stream-end

Conversation

@euisuh

@euisuh euisuh commented Jul 24, 2026

Copy link
Copy Markdown

Summary

Fix Cohere v2 async streaming instrumentation so spans are exported when the async stream raises.

Previously, aprocess_chat_v2_streaming_response only ended the span after successful stream consumption. If the async iterator raised while streaming, the exception propagated but the span was never ended/exported, so failed Cohere streams disappeared from telemetry.

This is separate from #4375, which covers non-streaming async Cohere error handling.

Changes

  • Wrap async v2 stream iteration in try/except/finally
  • Record stream exceptions and mark the span ERROR
  • Always end the span
  • Add regression coverage for a failing async Cohere v2 stream

Verification

  • uv run pytest tests/test_streaming_error_handling.py -q
  • uv run pytest tests/test_chat.py::test_cohere_v2_chat_legacy_with_streaming_async -q
  • uv run ruff check .
  • uv run pytest tests/ -q

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling for asynchronous streaming responses.
    • Streaming failures are now recorded with error details and reflected in tracing spans before being re-raised.
    • Ensures spans are properly finalized even when unexpected errors occur.

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e9431dca-7a39-49c2-a04a-855db6bd1ba7

📥 Commits

Reviewing files that changed from the base of the PR and between 93429cf and de8d1e8.

📒 Files selected for processing (2)
  • packages/opentelemetry-instrumentation-cohere/opentelemetry/instrumentation/cohere/streaming.py
  • packages/opentelemetry-instrumentation-cohere/tests/test_streaming_error_handling.py

📝 Walkthrough

Walkthrough

The async Cohere Chat v2 streaming processor now records unexpected streaming exceptions on the active span, marks it as errored, re-raises the exception, and preserves span finalization. A new test validates status, description, and exception event attributes.

Changes

Cohere streaming error handling

Layer / File(s) Summary
Async streaming exception handling
packages/opentelemetry-instrumentation-cohere/opentelemetry/instrumentation/cohere/streaming.py, packages/opentelemetry-instrumentation-cohere/tests/test_streaming_error_handling.py
aprocess_chat_v2_streaming_response marks unexpected exceptions as ERROR, records them on recording spans, re-raises them, and is covered by an async failing-stream test.

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

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: handling errors so async Cohere streaming spans close correctly.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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