@@ -82,14 +82,14 @@ def extract_usage_data(response):
82
82
83
83
candidates_tokens = usage .get ("candidates_token_count" , 0 ) or 0
84
84
# python-genai reports output and reasoning tokens separately
85
+ # reasoning should be sub-category of output tokens
85
86
usage_data ["output_tokens" ] = candidates_tokens + reasoning_tokens
86
87
87
88
total_tokens = usage .get ("total_token_count" , 0 ) or 0
88
89
usage_data ["total_tokens" ] = total_tokens
89
90
90
91
return usage_data
91
92
92
- # Handle response object
93
93
if not hasattr (response , "usage_metadata" ):
94
94
return usage_data
95
95
@@ -209,7 +209,7 @@ def _format_tools_for_span(tools):
209
209
# Check for predefined tool attributes - each of these tools
210
210
# is an attribute of the tool object, by default set to None
211
211
for attr_name , description in TOOL_ATTRIBUTES_MAP .items ():
212
- if hasattr (tool , attr_name ) and getattr ( tool , attr_name ) is not None :
212
+ if getattr (tool , attr_name , None ) :
213
213
formatted_tools .append (
214
214
{
215
215
"name" : attr_name ,
@@ -434,11 +434,9 @@ def set_span_data_for_request(span, integration, model, contents, kwargs):
434
434
span .set_data (SPANDATA .GEN_AI_SYSTEM , GEN_AI_SYSTEM )
435
435
span .set_data (SPANDATA .GEN_AI_REQUEST_MODEL , model )
436
436
437
- # Set streaming flag
438
437
if kwargs .get ("stream" , False ):
439
438
span .set_data (SPANDATA .GEN_AI_RESPONSE_STREAMING , True )
440
439
441
- # Set model configuration parameters
442
440
config = kwargs .get ("config" )
443
441
444
442
if config is None :
@@ -506,35 +504,29 @@ def set_span_data_for_response(span, integration, response):
506
504
if not response :
507
505
return
508
506
509
- # Extract and set response text
510
507
if should_send_default_pii () and integration .include_prompts :
511
508
response_texts = _extract_response_text (response )
512
509
if response_texts :
513
510
# Format as JSON string array as per documentation
514
511
span .set_data (SPANDATA .GEN_AI_RESPONSE_TEXT , safe_serialize (response_texts ))
515
512
516
- # Extract and set tool calls
517
513
tool_calls = extract_tool_calls (response )
518
514
if tool_calls :
519
515
# Tool calls should be JSON serialized
520
516
span .set_data (SPANDATA .GEN_AI_RESPONSE_TOOL_CALLS , safe_serialize (tool_calls ))
521
517
522
- # Extract and set finish reasons
523
518
finish_reasons = extract_finish_reasons (response )
524
519
if finish_reasons :
525
520
set_data_normalized (
526
521
span , SPANDATA .GEN_AI_RESPONSE_FINISH_REASONS , finish_reasons
527
522
)
528
523
529
- # Set response ID if available
530
524
if getattr (response , "response_id" , None ):
531
525
span .set_data (SPANDATA .GEN_AI_RESPONSE_ID , response .response_id )
532
526
533
- # Set response model if available
534
527
if getattr (response , "model_version" , None ):
535
528
span .set_data (SPANDATA .GEN_AI_RESPONSE_MODEL , response .model_version )
536
529
537
- # Set token usage if available
538
530
usage_data = extract_usage_data (response )
539
531
540
532
if usage_data ["input_tokens" ]:
@@ -555,7 +547,6 @@ def set_span_data_for_response(span, integration, response):
555
547
usage_data ["output_tokens_reasoning" ],
556
548
)
557
549
558
- # Set total token count if available
559
550
if usage_data ["total_tokens" ]:
560
551
span .set_data (SPANDATA .GEN_AI_USAGE_TOTAL_TOKENS , usage_data ["total_tokens" ])
561
552
@@ -567,7 +558,6 @@ def prepare_generate_content_args(args, kwargs):
567
558
contents = args [1 ] if len (args ) > 1 else kwargs .get ("contents" )
568
559
model_name = get_model_name (model )
569
560
570
- # Wrap config with tools
571
561
config = kwargs .get ("config" )
572
562
wrapped_config = wrapped_config_with_tools (config )
573
563
if wrapped_config is not config :
0 commit comments