44from typing import TYPE_CHECKING , cast
55
66import sentry_sdk
7- from sentry_sdk .ai .monitoring import record_token_usage
87from sentry_sdk .ai .utils import (
98 _set_span_data_attribute ,
109 get_start_span_function ,
1312from sentry_sdk .consts import OP , SPANDATA
1413from sentry_sdk .integrations import DidNotEnable , Integration
1514from sentry_sdk .scope import should_send_default_pii
16- from sentry_sdk .traces import StreamedSpan
1715from sentry_sdk .tracing_utils import has_span_streaming_enabled
1816from sentry_sdk .utils import (
1917 capture_internal_exceptions ,
2321)
2422
2523if TYPE_CHECKING :
26- from typing import Any , Callable , Iterable , Union
24+ from typing import Any , Callable , Iterable
2725
2826 from huggingface_hub import (
2927 ChatCompletionStreamOutput ,
3028 )
3129
32- from sentry_sdk .tracing import Span
3330
3431try :
3532 import huggingface_hub .inference ._client
@@ -97,7 +94,6 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
9794 model = hf_client .model or kwargs .get ("model" ) or ""
9895 operation_name = op .split ("." )[- 1 ]
9996
100- span : "Union[Span, StreamedSpan]"
10197 if has_span_streaming_enabled (client .options ):
10298 span = sentry_sdk .traces .start_span (
10399 name = f"{ operation_name } { model } " ,
@@ -106,12 +102,16 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
106102 "sentry.origin" : HuggingfaceHubIntegration .origin ,
107103 },
108104 )
105+
106+ set_on_span = span .set_attribute
109107 else :
110108 span = get_start_span_function ()(
111109 op = op ,
112110 name = f"{ operation_name } { model } " ,
113111 origin = HuggingfaceHubIntegration .origin ,
114112 )
113+
114+ set_on_span = span .set_data
115115 span .__enter__ ()
116116
117117 _set_span_data_attribute (span , SPANDATA .GEN_AI_OPERATION_NAME , operation_name )
@@ -258,16 +258,30 @@ def new_huggingface_task(*args: "Any", **kwargs: "Any") -> "Any":
258258 )
259259
260260 if usage is not None :
261- record_token_usage (
262- span ,
263- input_tokens = usage .prompt_tokens ,
264- output_tokens = usage .completion_tokens ,
265- total_tokens = usage .total_tokens ,
266- )
261+ if usage is not None and usage .prompt_tokens is not None :
262+ set_on_span (SPANDATA .GEN_AI_USAGE_INPUT_TOKENS , usage .prompt_tokens )
263+
264+ if usage is not None and usage .completion_tokens is not None :
265+ set_on_span (
266+ SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS , usage .completion_tokens
267+ )
268+
269+ if usage is not None and usage .total_tokens is not None :
270+ set_on_span (SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage .total_tokens )
271+
272+ elif (
273+ usage is not None
274+ and usage .prompt_tokens is not None
275+ and usage .completion_tokens is not None
276+ ):
277+ set_on_span (
278+ SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS ,
279+ usage .prompt_tokens + usage .completion_tokens ,
280+ )
267281 elif tokens_used > 0 :
268- record_token_usage (
269- span ,
270- total_tokens = tokens_used ,
282+ set_on_span (
283+ SPANDATA . GEN_AI_USAGE_TOTAL_TOKENS ,
284+ tokens_used ,
271285 )
272286
273287 # If the response is not a generator (meaning a streaming response)
@@ -331,10 +345,7 @@ def new_details_iterator() -> "Iterable[Any]":
331345 )
332346
333347 if tokens_used > 0 :
334- record_token_usage (
335- span ,
336- total_tokens = tokens_used ,
337- )
348+ set_on_span (SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , tokens_used )
338349
339350 span .__exit__ (None , None , None )
340351
@@ -443,12 +454,32 @@ def new_iterator() -> "Iterable[ChatCompletionStreamOutput]":
443454 text_response ,
444455 )
445456
446- if usage is not None :
447- record_token_usage (
448- span ,
449- input_tokens = usage .prompt_tokens ,
450- output_tokens = usage .completion_tokens ,
451- total_tokens = usage .total_tokens ,
457+ if usage is None :
458+ span .__exit__ (None , None , None )
459+ return
460+
461+ if usage .prompt_tokens is not None :
462+ set_on_span (
463+ SPANDATA .GEN_AI_USAGE_INPUT_TOKENS , usage .prompt_tokens
464+ )
465+
466+ if usage .completion_tokens is not None :
467+ set_on_span (
468+ SPANDATA .GEN_AI_USAGE_OUTPUT_TOKENS ,
469+ usage .completion_tokens ,
470+ )
471+
472+ if usage .total_tokens is not None :
473+ set_on_span (
474+ SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage .total_tokens
475+ )
476+ elif (
477+ usage .prompt_tokens is not None
478+ and usage .completion_tokens is not None
479+ ):
480+ set_on_span (
481+ SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS ,
482+ usage .prompt_tokens + usage .completion_tokens ,
452483 )
453484
454485 span .__exit__ (None , None , None )
0 commit comments