Skip to content

Commit eb629de

Browse files
committed
do not calculate token usage twice
1 parent 0aa26eb commit eb629de

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

sentry_sdk/integrations/openai.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def _set_output_data(span, response, kwargs, integration, finish_span=True):
226226
SPANDATA.GEN_AI_RESPONSE_TEXT,
227227
safe_serialize(response_text),
228228
)
229+
_calculate_token_usage(messages, response, span, None, integration.count_tokens)
229230
if finish_span:
230231
span.__exit__(None, None, None)
231232

@@ -238,6 +239,7 @@ def _set_output_data(span, response, kwargs, integration, finish_span=True):
238239
SPANDATA.GEN_AI_RESPONSE_TEXT,
239240
safe_serialize(response_text),
240241
)
242+
_calculate_token_usage(messages, response, span, None, integration.count_tokens)
241243
if finish_span:
242244
span.__exit__(None, None, None)
243245

@@ -360,11 +362,10 @@ async def new_iterator_async():
360362
else:
361363
response._iterator = new_iterator()
362364
else:
365+
_calculate_token_usage(messages, response, span, None, integration.count_tokens)
363366
if finish_span:
364367
span.__exit__(None, None, None)
365368

366-
_calculate_token_usage(messages, response, span, None, integration.count_tokens)
367-
368369

369370
def _new_chat_completion_common(f, *args, **kwargs):
370371
# type: (Any, Any, Any) -> Any

0 commit comments

Comments
 (0)