4848# Constant used to make the absence of content more understandable.
4949_CONTENT_ELIDED = "<elided>"
5050
51+ # Constant used for the value of 'gen_ai.operation.name".
52+ _GENERATE_CONTENT_OP_NAME = "generate_content"
53+
5154
5255class _MethodsSnapshot :
5356 def __init__ (self ):
@@ -189,12 +192,11 @@ def _get_top_p(config: Optional[GenerateContentConfigOrDict]):
189192def _to_dict (value : object ):
190193 if isinstance (value , dict ):
191194 return value
192- if hasattr (value , ' model_dump' ):
195+ if hasattr (value , " model_dump" ):
193196 return value .model_dump ()
194197 return json .loads (json .dumps (value ))
195198
196199
197-
198200class _GenerateContentInstrumentationHelper :
199201 def __init__ (
200202 self ,
@@ -216,13 +218,13 @@ def __init__(
216218
217219 def start_span_as_current_span (self , model_name , function_name ):
218220 return self ._otel_wrapper .start_as_current_span (
219- f"generate_content { model_name } " ,
221+ f"{ _GENERATE_CONTENT_OP_NAME } { model_name } " ,
220222 start_time = self ._start_time ,
221223 attributes = {
222224 code_attributes .CODE_FUNCTION_NAME : function_name ,
223225 gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
224226 gen_ai_attributes .GEN_AI_REQUEST_MODEL : self ._genai_request_model ,
225- gen_ai_attributes .GEN_AI_OPERATION_NAME : "GenerateContent" ,
227+ gen_ai_attributes .GEN_AI_OPERATION_NAME : _GENERATE_CONTENT_OP_NAME ,
226228 },
227229 )
228230
@@ -338,19 +340,19 @@ def _maybe_log_user_prompt(
338340 self , contents : Union [ContentListUnion , ContentListUnionDict ]
339341 ):
340342 if isinstance (contents , list ):
341- total = len (contents )
342- index = 0
343+ total = len (contents )
344+ index = 0
343345 for entry in contents :
344- self ._maybe_log_single_user_prompt (entry , index = index , total = total )
346+ self ._maybe_log_single_user_prompt (
347+ entry , index = index , total = total
348+ )
345349 index += 1
346350 else :
347351 self ._maybe_log_single_user_prompt (contents )
348352
349353 def _maybe_log_single_user_prompt (
350- self ,
351- contents : Union [ContentUnion , ContentUnionDict ],
352- index = 0 ,
353- total = 1 ):
354+ self , contents : Union [ContentUnion , ContentUnionDict ], index = 0 , total = 1
355+ ):
354356 # TODO: figure out how to report the index in a manner that is
355357 # aligned with the OTel semantic conventions.
356358 attributes = {
@@ -394,7 +396,9 @@ def _maybe_log_response_stats(self, response: GenerateContentResponse):
394396 #
395397 pass
396398
397- def _maybe_log_response_safety_ratings (self , response : GenerateContentResponse ):
399+ def _maybe_log_response_safety_ratings (
400+ self , response : GenerateContentResponse
401+ ):
398402 # TODO: Determine if there is a way that we can log
399403 # the "prompt_feedback". This would be especially useful
400404 # in the case where the response is blocked.
@@ -411,7 +415,8 @@ def _maybe_log_response(self, response: GenerateContentResponse):
411415 candidate ,
412416 flat_candidate_index = self ._candidate_index ,
413417 candidate_in_response_index = candidate_in_response_index ,
414- response_index = self ._response_index )
418+ response_index = self ._response_index ,
419+ )
415420 self ._candidate_index += 1
416421 candidate_in_response_index += 1
417422
@@ -420,10 +425,11 @@ def _maybe_log_response_candidate(
420425 candidate : Candidate ,
421426 flat_candidate_index : int ,
422427 candidate_in_response_index : int ,
423- response_index : int ):
428+ response_index : int ,
429+ ):
424430 # TODO: Determine if there might be a way to report the
425431 # response index and candidate response index.
426- attributes = {
432+ attributes = {
427433 gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
428434 }
429435 # TODO: determine if "role" should be reported here or not and, if so,
@@ -438,7 +444,7 @@ def _maybe_log_response_candidate(
438444 # "citation_metadata", "grounding_metadata", "logprobs_result", etc.
439445 #
440446 # See also: "TODOS.md"
441- body = {
447+ body = {
442448 "index" : flat_candidate_index ,
443449 }
444450 if self ._content_recording_enabled :
@@ -460,7 +466,7 @@ def _record_token_usage_metric(self):
460466 gen_ai_attributes .GEN_AI_TOKEN_TYPE : "input" ,
461467 gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
462468 gen_ai_attributes .GEN_AI_REQUEST_MODEL : self ._genai_request_model ,
463- gen_ai_attributes .GEN_AI_OPERATION_NAME : "GenerateContent" ,
469+ gen_ai_attributes .GEN_AI_OPERATION_NAME : _GENERATE_CONTENT_OP_NAME ,
464470 },
465471 )
466472 self ._otel_wrapper .token_usage_metric .record (
@@ -469,15 +475,15 @@ def _record_token_usage_metric(self):
469475 gen_ai_attributes .GEN_AI_TOKEN_TYPE : "output" ,
470476 gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
471477 gen_ai_attributes .GEN_AI_REQUEST_MODEL : self ._genai_request_model ,
472- gen_ai_attributes .GEN_AI_OPERATION_NAME : "GenerateContent" ,
478+ gen_ai_attributes .GEN_AI_OPERATION_NAME : _GENERATE_CONTENT_OP_NAME ,
473479 },
474480 )
475481
476482 def _record_duration_metric (self ):
477483 attributes = {
478484 gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
479485 gen_ai_attributes .GEN_AI_REQUEST_MODEL : self ._genai_request_model ,
480- gen_ai_attributes .GEN_AI_OPERATION_NAME : "GenerateContent" ,
486+ gen_ai_attributes .GEN_AI_OPERATION_NAME : _GENERATE_CONTENT_OP_NAME ,
481487 }
482488 if self ._error_type is not None :
483489 attributes [error_attributes .ERROR_TYPE ] = self ._error_type
0 commit comments