Skip to content

Commit c0a0e10

Browse files
committed
fix swalling exception bug
1 parent 31769ee commit c0a0e10

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

sentry_sdk/integrations/cohere/v1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ def new_chat(*args, **kwargs):
101101

102102
def _iter_stream_events(old_iterator, span, include_pii):
103103
# type: (Any, Any, bool) -> Iterator[StreamedChatResponse]
104-
with capture_internal_exceptions():
105-
for x in old_iterator:
104+
for x in old_iterator:
105+
with capture_internal_exceptions():
106106
if isinstance(x, ChatStreamEndEvent) or isinstance(
107107
x, StreamEndStreamedChatResponse
108108
):
@@ -113,4 +113,4 @@ def _iter_stream_events(old_iterator, span, include_pii):
113113
set_response_span_data(
114114
span, response, include_pii, COHERE_V1_CHAT_CONFIG["response"]
115115
)
116-
yield x
116+
yield x

sentry_sdk/integrations/cohere/v2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ def new_chat(*args, **kwargs):
102102
def _iter_v2_stream_events(old_iterator, span, include_pii):
103103
# type: (Any, Span, bool) -> Iterator[V2ChatStreamResponse]
104104
collected_text = [] # type: list[str]
105-
with capture_internal_exceptions():
106-
for x in old_iterator:
105+
for x in old_iterator:
106+
with capture_internal_exceptions():
107107
_append_stream_delta_text(collected_text, x)
108108
if isinstance(x, MessageEndV2ChatStreamResponse):
109109
set_response_span_data(
@@ -113,7 +113,7 @@ def _iter_v2_stream_events(old_iterator, span, include_pii):
113113
COHERE_V2_CHAT_CONFIG["stream_response"],
114114
collected_text,
115115
)
116-
yield x
116+
yield x
117117

118118

119119
def _append_stream_delta_text(collected_text, event):

tests/integrations/cohere/test_cohere.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ def test_v1_bad_chat(sentry_init, capture_events):
182182
assert event["level"] == "error"
183183

184184

185+
def test_v1_streaming_error_propagates(sentry_init, capture_events):
186+
"""Stream errors must not be silently swallowed by capture_internal_exceptions."""
187+
sentry_init(integrations=[CohereIntegration()], traces_sample_rate=1.0)
188+
events = capture_events()
189+
190+
from sentry_sdk.integrations.cohere.v1 import _iter_stream_events
191+
192+
def failing_iterator():
193+
yield "event1"
194+
raise ConnectionError("stream interrupted")
195+
196+
with start_transaction(name="cohere tx") as tx:
197+
span = tx.start_child(op="gen_ai.chat")
198+
with pytest.raises(ConnectionError, match="stream interrupted"):
199+
list(_iter_stream_events(failing_iterator(), span, False))
200+
span.finish()
201+
202+
185203
def test_v1_span_status_error(sentry_init, capture_events):
186204
sentry_init(integrations=[CohereIntegration()], traces_sample_rate=1.0)
187205
events = capture_events()
@@ -410,6 +428,25 @@ def test_v2_streaming_chat(
410428
# --- V2 Error ---
411429

412430

431+
@pytest.mark.skipif(not has_v2, reason="Cohere V2 client not available")
432+
def test_v2_streaming_error_propagates(sentry_init, capture_events):
433+
"""Stream errors must not be silently swallowed by capture_internal_exceptions."""
434+
sentry_init(integrations=[CohereIntegration()], traces_sample_rate=1.0)
435+
events = capture_events()
436+
437+
from sentry_sdk.integrations.cohere.v2 import _iter_v2_stream_events
438+
439+
def failing_iterator():
440+
yield "event1"
441+
raise ConnectionError("stream interrupted")
442+
443+
with start_transaction(name="cohere tx") as tx:
444+
span = tx.start_child(op="gen_ai.chat")
445+
with pytest.raises(ConnectionError, match="stream interrupted"):
446+
list(_iter_v2_stream_events(failing_iterator(), span, False))
447+
span.finish()
448+
449+
413450
@pytest.mark.skipif(not has_v2, reason="Cohere V2 client not available")
414451
def test_v2_bad_chat(sentry_init, capture_events):
415452
sentry_init(integrations=[CohereIntegration()], traces_sample_rate=1.0)

0 commit comments

Comments
 (0)