@@ -465,23 +465,14 @@ def _set_embeddings_input_data(
465465def _set_common_output_data (
466466 span : "Span" ,
467467 response : "Any" ,
468- kwargs : "dict[str, Any] " ,
468+ input : "Any" ,
469469 integration : "OpenAIIntegration" ,
470470 start_time : "Optional[float]" = None ,
471471 finish_span : bool = True ,
472472) -> None :
473473 if hasattr (response , "model" ):
474474 set_data_normalized (span , SPANDATA .GEN_AI_RESPONSE_MODEL , response .model )
475475
476- # Input messages (the prompt or data sent to the model)
477- # used for the token usage calculation
478- messages = kwargs .get ("messages" )
479- if messages is None :
480- messages = kwargs .get ("input" )
481-
482- if messages is not None and isinstance (messages , str ):
483- messages = [messages ]
484-
485476 ttft : "Optional[float]" = None
486477
487478 if hasattr (response , "choices" ):
@@ -494,7 +485,7 @@ def _set_common_output_data(
494485 if len (response_text ) > 0 :
495486 set_data_normalized (span , SPANDATA .GEN_AI_RESPONSE_TEXT , response_text )
496487
497- _calculate_token_usage (messages , response , span , None , integration .count_tokens )
488+ _calculate_token_usage (input , response , span , None , integration .count_tokens )
498489
499490 if finish_span :
500491 span .__exit__ (None , None , None )
@@ -530,7 +521,7 @@ def _set_common_output_data(
530521 span , SPANDATA .GEN_AI_RESPONSE_TEXT , output_messages ["response" ]
531522 )
532523
533- _calculate_token_usage (messages , response , span , None , integration .count_tokens )
524+ _calculate_token_usage (input , response , span , None , integration .count_tokens )
534525
535526 if finish_span :
536527 span .__exit__ (None , None , None )
@@ -571,7 +562,7 @@ def new_iterator() -> "Iterator[ChatCompletionChunk]":
571562 # OpenAI responses API end of streaming response
572563 if RESPONSES_API_ENABLED and isinstance (x , ResponseCompletedEvent ):
573564 _calculate_token_usage (
574- messages ,
565+ input ,
575566 x .response ,
576567 span ,
577568 None ,
@@ -594,7 +585,7 @@ def new_iterator() -> "Iterator[ChatCompletionChunk]":
594585 )
595586 if count_tokens_manually :
596587 _calculate_token_usage (
597- messages ,
588+ input ,
598589 response ,
599590 span ,
600591 all_responses ,
@@ -635,7 +626,7 @@ async def new_iterator_async() -> "AsyncIterator[ChatCompletionChunk]":
635626 # OpenAI responses API end of streaming response
636627 if RESPONSES_API_ENABLED and isinstance (x , ResponseCompletedEvent ):
637628 _calculate_token_usage (
638- messages ,
629+ input ,
639630 x .response ,
640631 span ,
641632 None ,
@@ -658,7 +649,7 @@ async def new_iterator_async() -> "AsyncIterator[ChatCompletionChunk]":
658649 )
659650 if count_tokens_manually :
660651 _calculate_token_usage (
661- messages ,
652+ input ,
662653 response ,
663654 span ,
664655 all_responses ,
@@ -672,7 +663,7 @@ async def new_iterator_async() -> "AsyncIterator[ChatCompletionChunk]":
672663 else :
673664 response ._iterator = new_iterator ()
674665 else :
675- _calculate_token_usage (messages , response , span , None , integration .count_tokens )
666+ _calculate_token_usage (input , response , span , None , integration .count_tokens )
676667 if finish_span :
677668 span .__exit__ (None , None , None )
678669
@@ -727,10 +718,15 @@ def _set_completions_api_output_data(
727718 start_time : "Optional[float]" = None ,
728719 finish_span : bool = True ,
729720) -> None :
721+ messages = kwargs .get ("messages" )
722+
723+ if messages is not None and isinstance (messages , str ):
724+ messages = [messages ]
725+
730726 _set_common_output_data (
731727 span ,
732728 response ,
733- kwargs ,
729+ messages ,
734730 integration ,
735731 start_time ,
736732 finish_span ,
@@ -745,10 +741,15 @@ def _set_streaming_completions_api_output_data(
745741 start_time : "Optional[float]" = None ,
746742 finish_span : bool = True ,
747743) -> None :
744+ messages = kwargs .get ("messages" )
745+
746+ if messages is not None and isinstance (messages , str ):
747+ messages = [messages ]
748+
748749 _set_common_output_data (
749750 span ,
750751 response ,
751- kwargs ,
752+ messages ,
752753 integration ,
753754 start_time ,
754755 finish_span ,
@@ -763,10 +764,15 @@ def _set_responses_api_output_data(
763764 start_time : "Optional[float]" = None ,
764765 finish_span : bool = True ,
765766) -> None :
767+ input = kwargs .get ("input" )
768+
769+ if input is not None and isinstance (input , str ):
770+ input = [input ]
771+
766772 _set_common_output_data (
767773 span ,
768774 response ,
769- kwargs ,
775+ input ,
770776 integration ,
771777 start_time ,
772778 finish_span ,
@@ -781,10 +787,15 @@ def _set_streaming_responses_api_output_data(
781787 start_time : "Optional[float]" = None ,
782788 finish_span : bool = True ,
783789) -> None :
790+ input = kwargs .get ("input" )
791+
792+ if input is not None and isinstance (input , str ):
793+ input = [input ]
794+
784795 _set_common_output_data (
785796 span ,
786797 response ,
787- kwargs ,
798+ input ,
788799 integration ,
789800 start_time ,
790801 finish_span ,
@@ -799,10 +810,15 @@ def _set_embeddings_output_data(
799810 start_time : "Optional[float]" = None ,
800811 finish_span : bool = True ,
801812) -> None :
813+ input = kwargs .get ("input" )
814+
815+ if input is not None and isinstance (input , str ):
816+ input = [input ]
817+
802818 _set_common_output_data (
803819 span ,
804820 response ,
805- kwargs ,
821+ input ,
806822 integration ,
807823 start_time ,
808824 finish_span ,
0 commit comments