Skip to content

Commit 2f14008

Browse files
author
patcherai-opensource-internal[bot]
committed
fix(openai): guard against choices=None in streaming chunk iterators
When a streaming chunk has choices=None, the existing hasattr check passes but the subsequent iteration raises TypeError. Inside capture_internal_exceptions() this is silently suppressed, causing the usage capture on the next line to be skipped — leaving token usage and response text missing from the Sentry span. Add explicit None checks in both the sync and async streaming iterators.
1 parent 7a3dfb0 commit 2f14008

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

sentry_sdk/integrations/openai.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ def _wrap_synchronous_completions_chunk_iterator(
839839
span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, x.model)
840840

841841
with capture_internal_exceptions():
842-
if hasattr(x, "choices"):
842+
if hasattr(x, "choices") and x.choices is not None:
843843
choice_index = 0
844844
for choice in x.choices:
845845
if hasattr(choice, "delta") and hasattr(choice.delta, "content"):
@@ -901,7 +901,7 @@ async def _wrap_asynchronous_completions_chunk_iterator(
901901
span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, x.model)
902902

903903
with capture_internal_exceptions():
904-
if hasattr(x, "choices"):
904+
if hasattr(x, "choices") and x.choices is not None:
905905
choice_index = 0
906906
for choice in x.choices:
907907
if hasattr(choice, "delta") and hasattr(choice.delta, "content"):

0 commit comments

Comments
 (0)