Skip to content

Commit a83fca9

Browse files
committed
fixed streaming responses token count
1 parent dddaab2 commit a83fca9

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ async def new_iterator_async():
286286
# type: () -> AsyncIterator[ChatCompletionChunk]
287287
with capture_internal_exceptions():
288288
async for x in old_iterator:
289+
# OpenAI chat completion API
289290
if hasattr(x, "choices"):
290291
choice_index = 0
291292
for choice in x.choices:
@@ -297,6 +298,11 @@ async def new_iterator_async():
297298
data_buf.append([])
298299
data_buf[choice_index].append(content or "")
299300
choice_index += 1
301+
# OpenAI responses API
302+
elif hasattr(x, "delta"):
303+
if len(data_buf) == 0:
304+
data_buf.append([])
305+
data_buf[0].append(x.delta or "")
300306
yield x
301307
if len(data_buf) > 0:
302308
all_responses = list(map(lambda chunk: "".join(chunk), data_buf))

0 commit comments

Comments
 (0)