5151from typing_extensions import NotRequired
5252
5353from opentelemetry .instrumentation .openai .shared import (
54+ _extract_model_name_from_provider_format ,
55+ _set_request_attributes ,
5456 _set_span_attribute ,
5557 model_as_dict ,
5658)
@@ -189,12 +191,26 @@ def process_content_block(
189191
190192
191193@dont_throw
194+ def prepare_kwargs_for_shared_attributes (kwargs ):
195+ """
196+ Prepare kwargs for the shared _set_request_attributes function.
197+ Maps responses API specific parameters to the common format.
198+ """
199+ prepared_kwargs = kwargs .copy ()
200+
201+ # Map max_output_tokens to max_tokens for the shared function
202+ if "max_output_tokens" in kwargs :
203+ prepared_kwargs ["max_tokens" ] = kwargs ["max_output_tokens" ]
204+
205+ return prepared_kwargs
206+
207+
192208def set_data_attributes (traced_response : TracedData , span : Span ):
193- _set_span_attribute (span , GenAIAttributes .GEN_AI_SYSTEM , "openai" )
194- _set_span_attribute (span , GenAIAttributes .GEN_AI_REQUEST_MODEL , traced_response .request_model )
195209 _set_span_attribute (span , GenAIAttributes .GEN_AI_RESPONSE_ID , traced_response .response_id )
196- _set_span_attribute (span , GenAIAttributes .GEN_AI_RESPONSE_MODEL , traced_response .response_model )
197- _set_span_attribute (span , OpenAIAttributes .OPENAI_REQUEST_SERVICE_TIER , traced_response .request_service_tier )
210+
211+ response_model = _extract_model_name_from_provider_format (traced_response .response_model )
212+ _set_span_attribute (span , GenAIAttributes .GEN_AI_RESPONSE_MODEL , response_model )
213+
198214 _set_span_attribute (span , OpenAIAttributes .OPENAI_RESPONSE_SERVICE_TIER , traced_response .response_service_tier )
199215 if usage := traced_response .usage :
200216 _set_span_attribute (span , GenAIAttributes .GEN_AI_USAGE_INPUT_TOKENS , usage .input_tokens )
@@ -445,6 +461,7 @@ def responses_get_or_create_wrapper(tracer: Tracer, wrapped, instance, args, kwa
445461 kind = SpanKind .CLIENT ,
446462 start_time = start_time ,
447463 )
464+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
448465
449466 return ResponseStream (
450467 span = span ,
@@ -503,6 +520,7 @@ def responses_get_or_create_wrapper(tracer: Tracer, wrapped, instance, args, kwa
503520 start_time if traced_data is None else int (traced_data .start_time )
504521 ),
505522 )
523+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
506524 span .set_attribute (ERROR_TYPE , e .__class__ .__name__ )
507525 span .record_exception (e )
508526 span .set_status (StatusCode .ERROR , str (e ))
@@ -568,6 +586,7 @@ def responses_get_or_create_wrapper(tracer: Tracer, wrapped, instance, args, kwa
568586 kind = SpanKind .CLIENT ,
569587 start_time = int (traced_data .start_time ),
570588 )
589+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
571590 set_data_attributes (traced_data , span )
572591 span .end ()
573592
@@ -591,6 +610,7 @@ async def async_responses_get_or_create_wrapper(
591610 kind = SpanKind .CLIENT ,
592611 start_time = start_time ,
593612 )
613+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
594614
595615 return ResponseStream (
596616 span = span ,
@@ -645,6 +665,7 @@ async def async_responses_get_or_create_wrapper(
645665 start_time if traced_data is None else int (traced_data .start_time )
646666 ),
647667 )
668+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
648669 span .set_attribute (ERROR_TYPE , e .__class__ .__name__ )
649670 span .record_exception (e )
650671 span .set_status (StatusCode .ERROR , str (e ))
@@ -711,6 +732,7 @@ async def async_responses_get_or_create_wrapper(
711732 kind = SpanKind .CLIENT ,
712733 start_time = int (traced_data .start_time ),
713734 )
735+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
714736 set_data_attributes (traced_data , span )
715737 span .end ()
716738
@@ -735,6 +757,7 @@ def responses_cancel_wrapper(tracer: Tracer, wrapped, instance, args, kwargs):
735757 start_time = existing_data .start_time ,
736758 record_exception = True ,
737759 )
760+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
738761 span .record_exception (Exception ("Response cancelled" ))
739762 set_data_attributes (existing_data , span )
740763 span .end ()
@@ -761,6 +784,7 @@ async def async_responses_cancel_wrapper(
761784 start_time = existing_data .start_time ,
762785 record_exception = True ,
763786 )
787+ _set_request_attributes (span , prepare_kwargs_for_shared_attributes (kwargs ), instance )
764788 span .record_exception (Exception ("Response cancelled" ))
765789 set_data_attributes (existing_data , span )
766790 span .end ()
0 commit comments