@@ -221,6 +221,31 @@ def _add_request_options_to_span(
221221 span .set_attribute (key , value )
222222
223223
224+ def _get_gen_ai_request_attributes (config : Union [GenerateContentConfigOrDict , None ]) -> dict [str , Any ]:
225+ if not config :
226+ return {}
227+ attributes : dict [str , Any ] = {}
228+ config = _coerce_config_to_object (config )
229+ if config .seed :
230+ attributes [gen_ai_attributes .GEN_AI_REQUEST_SEED ] = (
231+ config .seed
232+ )
233+ if config .candidate_count :
234+ attributes [gen_ai_attributes .GEN_AI_REQUEST_CHOICE_COUNT ] = (
235+ config .candidate_count
236+ )
237+ if config .response_mime_type :
238+ if config .response_mime_type == "text/plain" :
239+ attributes [gen_ai_attributes .GEN_AI_OUTPUT_TYPE ] = "text"
240+ elif config .response_mime_type == "application/json" :
241+ attributes [gen_ai_attributes .GEN_AI_OUTPUT_TYPE ] = "json"
242+ else :
243+ attributes [gen_ai_attributes .GEN_AI_OUTPUT_TYPE ] = (
244+ config .response_mime_type
245+ )
246+ return attributes
247+
248+
224249def _get_response_property (response : GenerateContentResponse , path : str ):
225250 path_segments = path .split ("." )
226251 current_context = response
@@ -347,7 +372,6 @@ def start_span_as_current_span(
347372 start_time = self ._start_time ,
348373 attributes = {
349374 code_attributes .CODE_FUNCTION_NAME : function_name ,
350- gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
351375 gen_ai_attributes .GEN_AI_REQUEST_MODEL : self ._genai_request_model ,
352376 gen_ai_attributes .GEN_AI_OPERATION_NAME : _GENERATE_CONTENT_OP_NAME ,
353377 },
@@ -399,6 +423,10 @@ def finalize_processing(self):
399423 gen_ai_attributes .GEN_AI_RESPONSE_FINISH_REASONS ,
400424 sorted (self ._finish_reasons_set ),
401425 )
426+ if self .sem_conv_opt_in_mode == _StabilityMode .DEFAULT :
427+ span .set_attribute (
428+ gen_ai_attributes .GEN_AI_SYSTEM , self ._genai_system
429+ )
402430 self ._record_token_usage_metric ()
403431 self ._record_duration_metric ()
404432
@@ -468,9 +496,7 @@ def _maybe_log_completion_details(
468496 candidates : list [Candidate ],
469497 config : Optional [GenerateContentConfigOrDict ] = None ,
470498 ):
471- attributes = {
472- gen_ai_attributes .GEN_AI_SYSTEM : self ._genai_system ,
473- }
499+ attributes = _get_gen_ai_request_attributes (config )
474500 system_instructions = []
475501 if system_content := _config_to_system_instruction (config ):
476502 system_instructions = to_system_instructions (
@@ -508,6 +534,7 @@ def _maybe_log_completion_details(
508534 )
509535 )
510536 span .set_attributes (completion_details_attributes )
537+ span .set_attributes (attributes )
511538 if self ._content_recording_enabled in [
512539 ContentCapturingMode .EVENT_ONLY ,
513540 ContentCapturingMode .SPAN_AND_EVENT ,
0 commit comments