@@ -87,25 +87,25 @@ def traced_method(wrapped, instance, args, kwargs):
8787 and result .meta .billed_units is not None
8888 ):
8989 usage = result .meta .billed_units
90- if usage is not None :
91- span . set_attribute (
92- SpanAttributes . LLM_USAGE_PROMPT_TOKENS ,
93- usage . input_tokens or 0 ,
94- )
95- span . set_attribute (
96- SpanAttributes . LLM_USAGE_COMPLETION_TOKENS ,
97- usage . output_tokens or 0 ,
98- )
99-
100- span .set_attribute (
101- SpanAttributes .LLM_USAGE_TOTAL_TOKENS ,
102- ( usage . input_tokens or 0 ) + ( usage . output_tokens or 0 ) ,
103- )
104-
105- span . set_attribute (
106- "search_units" ,
107- usage . search_units or 0 ,
108- )
90+ input_tokens = int ( usage . input_tokens ) if usage . input_tokens else 0
91+ output_tokens = int ( usage . output_tokens ) if usage . output_tokens else 0
92+ span . set_attribute (
93+ SpanAttributes . LLM_USAGE_PROMPT_TOKENS ,
94+ input_tokens ,
95+ )
96+ span . set_attribute (
97+ SpanAttributes . LLM_USAGE_COMPLETION_TOKENS ,
98+ output_tokens ,
99+ )
100+ span .set_attribute (
101+ SpanAttributes .LLM_USAGE_TOTAL_TOKENS ,
102+ input_tokens + output_tokens ,
103+ )
104+ span . set_attribute (
105+ "search_units" ,
106+ int ( usage . search_units ) if usage . search_units else 0 ,
107+ )
108+
109109
110110 span .set_status (StatusCode .OK )
111111 span .end ()
@@ -309,25 +309,25 @@ def traced_method(wrapped, instance, args, kwargs):
309309 and result .meta .billed_units is not None
310310 ):
311311 usage = result .meta .billed_units
312- if usage is not None :
313- span . set_attribute (
314- SpanAttributes . LLM_USAGE_PROMPT_TOKENS ,
315- usage . input_tokens or 0 ,
316- )
317- span . set_attribute (
318- SpanAttributes . LLM_USAGE_COMPLETION_TOKENS ,
319- usage . output_tokens or 0 ,
320- )
321-
322- span .set_attribute (
323- SpanAttributes .LLM_USAGE_TOTAL_TOKENS ,
324- ( usage . input_tokens or 0 ) + ( usage . output_tokens or 0 ) ,
325- )
326-
327- span . set_attribute (
328- "search_units" ,
329- usage . search_units or 0 ,
330- )
312+ input_tokens = int ( usage . input_tokens ) if usage . input_tokens else 0
313+ output_tokens = int ( usage . output_tokens ) if usage . output_tokens else 0
314+ span . set_attribute (
315+ SpanAttributes . LLM_USAGE_PROMPT_TOKENS ,
316+ input_tokens ,
317+ )
318+ span . set_attribute (
319+ SpanAttributes . LLM_USAGE_COMPLETION_TOKENS ,
320+ output_tokens ,
321+ )
322+ span .set_attribute (
323+ SpanAttributes .LLM_USAGE_TOTAL_TOKENS ,
324+ input_tokens + output_tokens ,
325+ )
326+ span . set_attribute (
327+ "search_units" ,
328+ int ( usage . search_units ) if usage . search_units else 0 ,
329+ )
330+
331331 span .set_status (StatusCode .OK )
332332 span .end ()
333333 return result
@@ -419,10 +419,12 @@ def traced_method(wrapped, instance, args, kwargs):
419419 if (hasattr (result .usage , "billed_units" ) and
420420 result .usage .billed_units is not None ):
421421 usage = result .usage .billed_units
422+ input_tokens = int (usage .input_tokens ) if usage .input_tokens else 0
423+ output_tokens = int (usage .output_tokens ) if usage .output_tokens else 0
422424 for metric , value in {
423- "input" : usage . input_tokens or 0 ,
424- "output" : usage . output_tokens or 0 ,
425- "total" : ( usage . input_tokens or 0 ) + ( usage . output_tokens or 0 ) ,
425+ "input" : input_tokens ,
426+ "output" : output_tokens ,
427+ "total" : input_tokens + output_tokens ,
426428 }.items ():
427429 span .set_attribute (
428430 f"gen_ai.usage.{ metric } _tokens" ,
@@ -571,26 +573,27 @@ def traced_method(wrapped, instance, args, kwargs):
571573 and response .meta .billed_units is not None
572574 ):
573575 usage = response .meta .billed_units
574- if usage is not None :
575- span .set_attribute (
576- SpanAttributes .LLM_USAGE_PROMPT_TOKENS ,
577- usage .input_tokens or 0 ,
578- )
579- span .set_attribute (
580- SpanAttributes .LLM_USAGE_COMPLETION_TOKENS ,
581- usage .output_tokens or 0 ,
582- )
583-
576+ input_tokens = int (usage .input_tokens ) if usage .input_tokens else 0
577+ output_tokens = int (usage .output_tokens ) if usage .output_tokens else 0
578+ span .set_attribute (
579+ SpanAttributes .LLM_USAGE_PROMPT_TOKENS ,
580+ input_tokens ,
581+ )
582+ span .set_attribute (
583+ SpanAttributes .LLM_USAGE_COMPLETION_TOKENS ,
584+ output_tokens ,
585+ )
586+ span .set_attribute (
587+ SpanAttributes .LLM_USAGE_TOTAL_TOKENS ,
588+ input_tokens + output_tokens ,
589+ )
590+
591+ if usage .search_units is not None :
584592 span .set_attribute (
585- SpanAttributes .LLM_USAGE_TOTAL_TOKENS ,
586- (usage .input_tokens or 0 )
587- + (usage .output_tokens or 0 ),
593+ "search_units" ,
594+ int (usage .search_units ) if usage .search_units else 0 ,
588595 )
589- if usage .search_units is not None :
590- span .set_attribute (
591- "search_units" ,
592- usage .search_units or 0 ,
593- )
596+
594597
595598 yield event
596599 finally :
0 commit comments