Skip to content

Commit a1f4ab7

Browse files
committed
make sure to capture exceptions correctly
1 parent d7814b0 commit a1f4ab7

2 files changed

Lines changed: 58 additions & 50 deletions

File tree

sentry_sdk/integrations/cohere/v1.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,24 @@ def new_chat(*args, **kwargs):
178178

179179
def new_iterator():
180180
# type: () -> Iterator[StreamedChatResponse]
181-
with capture_internal_exceptions():
181+
try:
182182
for x in old_iterator:
183-
if isinstance(x, ChatStreamEndEvent) or isinstance(
184-
x, StreamEndStreamedChatResponse
185-
):
186-
collect_chat_response_fields(
187-
span,
188-
x.response,
189-
include_pii=should_send_default_pii()
190-
and integration.include_prompts,
191-
)
183+
with capture_internal_exceptions():
184+
if isinstance(x, ChatStreamEndEvent) or isinstance(
185+
x, StreamEndStreamedChatResponse
186+
):
187+
collect_chat_response_fields(
188+
span,
189+
x.response,
190+
include_pii=should_send_default_pii()
191+
and integration.include_prompts,
192+
)
192193
yield x
193-
194-
span.__exit__(None, None, None)
194+
except Exception as exc:
195+
_capture_exception(exc)
196+
raise
197+
finally:
198+
span.__exit__(None, None, None)
195199

196200
return new_iterator()
197201
elif isinstance(res, NonStreamedChatResponse):

sentry_sdk/integrations/cohere/v2.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -218,50 +218,54 @@ def new_chat(*args, **kwargs):
218218
def new_iterator():
219219
# type: () -> Iterator[V2ChatStreamResponse]
220220
collected_text = []
221-
with capture_internal_exceptions():
221+
try:
222222
for x in old_iterator:
223-
if (
224-
hasattr(x, "type")
225-
and x.type == "content-delta"
226-
and hasattr(x, "delta")
227-
and x.delta is not None
228-
):
229-
msg = getattr(x.delta, "message", None)
230-
if msg is not None:
231-
content = getattr(msg, "content", None)
232-
if content is not None and hasattr(content, "text"):
233-
collected_text.append(content.text)
234-
235-
if isinstance(x, MessageEndV2ChatStreamResponse):
236-
include_pii = (
237-
should_send_default_pii()
238-
and integration.include_prompts
239-
)
240-
if include_pii and collected_text:
241-
set_data_normalized(
242-
span,
243-
SPANDATA.GEN_AI_RESPONSE_TEXT,
244-
["".join(collected_text)],
223+
with capture_internal_exceptions():
224+
if (
225+
hasattr(x, "type")
226+
and x.type == "content-delta"
227+
and hasattr(x, "delta")
228+
and x.delta is not None
229+
):
230+
msg = getattr(x.delta, "message", None)
231+
if msg is not None:
232+
content = getattr(msg, "content", None)
233+
if content is not None and hasattr(content, "text"):
234+
collected_text.append(content.text)
235+
236+
if isinstance(x, MessageEndV2ChatStreamResponse):
237+
include_pii = (
238+
should_send_default_pii()
239+
and integration.include_prompts
245240
)
246-
if hasattr(x, "id"):
247-
set_data_normalized(
248-
span, SPANDATA.GEN_AI_RESPONSE_ID, x.id
249-
)
250-
if hasattr(x, "delta") and x.delta is not None:
251-
if hasattr(x.delta, "finish_reason"):
241+
if include_pii and collected_text:
252242
set_data_normalized(
253243
span,
254-
SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS,
255-
x.delta.finish_reason,
244+
SPANDATA.GEN_AI_RESPONSE_TEXT,
245+
["".join(collected_text)],
246+
)
247+
if hasattr(x, "id"):
248+
set_data_normalized(
249+
span, SPANDATA.GEN_AI_RESPONSE_ID, x.id
256250
)
257-
if (
258-
hasattr(x.delta, "usage")
259-
and x.delta.usage is not None
260-
):
261-
_record_token_usage_v2(span, x.delta.usage)
251+
if hasattr(x, "delta") and x.delta is not None:
252+
if hasattr(x.delta, "finish_reason"):
253+
set_data_normalized(
254+
span,
255+
SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS,
256+
x.delta.finish_reason,
257+
)
258+
if (
259+
hasattr(x.delta, "usage")
260+
and x.delta.usage is not None
261+
):
262+
_record_token_usage_v2(span, x.delta.usage)
262263
yield x
263-
264-
span.__exit__(None, None, None)
264+
except Exception as exc:
265+
_capture_exception(exc)
266+
raise
267+
finally:
268+
span.__exit__(None, None, None)
265269

266270
return new_iterator()
267271
elif isinstance(res, V2ChatResponse):

0 commit comments

Comments
 (0)