Repository navigation
fix(cohere): end the span when an async v2 chat stream fails or is abandoned - #4648
Open
pcbeingused333 wants to merge 1 commit into
Open
pcbeingused333 wants to merge 1 commit into
pcbeingused333 wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
aprocess_chat_v2_streaming_response(async Cohere v2chat_stream) only callsspan.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_responsealready wraps the loop intry/finally; the async one did not.Change
Wrap the async loop and the response handling in
try/finallyand end the span infinally, 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 yieldsmessage-startandcontent-startand then raisesConnectionError. Both must leave one finished span. The async case fails on main (0 spans) and passes with the change.packages/opentelemetry-instrumentation-coheretests: 27 passed.ruff checkclean.This PR was made with Claude Code (Anthropic); the tests above were run on main and on this branch.
Summary by CodeRabbit