Skip to content

Commit b6c22ec

Browse files
fix: stop capturing mid-stream iteration errors as Sentry events
Per review: iterator exceptions should just bubble out. The span still finishes with error status and the exception re-raises, but we no longer send the provider error as a Sentry event -- that was behavior change versus running without the SDK. Same change in both huggingface_hub streaming iterators for consistency; the three regression tests now assert propagation + span status only.
1 parent 17a1c02 commit b6c22ec

4 files changed

Lines changed: 3 additions & 20 deletions

File tree

‎sentry_sdk/integrations/cohere.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,9 @@ def new_iterator() -> "Iterator[StreamedChatResponse]":
267267
),
268268
)
269269
yield x
270-
except Exception as e:
270+
except Exception:
271271
exc_info = sys.exc_info()
272272
with capture_internal_exceptions():
273-
_capture_exception(e)
274273
span.__exit__(*exc_info)
275274
reraise(*exc_info)
276275

‎sentry_sdk/integrations/huggingface_hub.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,9 @@ def new_details_iterator() -> "Iterable[Any]":
320320
tokens_used = chunk.details.generated_tokens
321321

322322
yield chunk
323-
except Exception as e:
323+
except Exception:
324324
exc_info = sys.exc_info()
325325
with capture_internal_exceptions():
326-
_capture_exception(e)
327326
span.__exit__(*exc_info)
328327
reraise(*exc_info)
329328

@@ -409,10 +408,9 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]":
409408
tool_calls = choice.delta.tool_calls
410409

411410
yield chunk
412-
except Exception as e:
411+
except Exception:
413412
exc_info = sys.exc_info()
414413
with capture_internal_exceptions():
415-
_capture_exception(e)
416414
span.__exit__(*exc_info)
417415
reraise(*exc_info)
418416

‎tests/integrations/cohere/test_cohere.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,10 +898,6 @@ def failing_stream(*args, **kwargs):
898898

899899
sentry_sdk.flush()
900900

901-
# the error was recorded on Sentry instead of vanishing
902-
errors = [e for e in events if e.get("level") == "error"]
903-
assert len(errors) == 1
904-
905901
if span_streaming:
906902
assert len(items) == 1
907903
assert items[0].payload["status"] == "error"

‎tests/integrations/huggingface_hub/test_huggingface_hub.py‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,7 +2728,6 @@ def test_text_generation_streaming_iteration_error_propagates(
27282728
integrations=[HuggingfaceHubIntegration()],
27292729
traces_sample_rate=1.0,
27302730
)
2731-
events = capture_events()
27322731

27332732
def failing_stream(*args: "Any", **kwargs: "Any") -> "Any":
27342733
yield SimpleNamespace(
@@ -2751,10 +2750,6 @@ def failing_stream(*args: "Any", **kwargs: "Any") -> "Any":
27512750

27522751
sentry_sdk.flush()
27532752

2754-
# the error was recorded on Sentry instead of vanishing
2755-
errors = [e for e in events if e.get("level") == "error"]
2756-
assert len(errors) == 1
2757-
27582753

27592754
def test_chat_completion_streaming_iteration_error_propagates(
27602755
sentry_init: "Any", capture_events: "Any"
@@ -2769,7 +2764,6 @@ def test_chat_completion_streaming_iteration_error_propagates(
27692764
integrations=[HuggingfaceHubIntegration()],
27702765
traces_sample_rate=1.0,
27712766
)
2772-
events = capture_events()
27732767

27742768
def failing_stream(*args: "Any", **kwargs: "Any") -> "Any":
27752769
yield SimpleNamespace(model="test-model", usage=None, choices=None)
@@ -2787,7 +2781,3 @@ def failing_stream(*args: "Any", **kwargs: "Any") -> "Any":
27872781
)
27882782

27892783
sentry_sdk.flush()
2790-
2791-
# the error was recorded on Sentry instead of vanishing
2792-
errors = [e for e in events if e.get("level") == "error"]
2793-
assert len(errors) == 1

0 commit comments

Comments
 (0)