@@ -534,12 +534,12 @@ async def format_stream(llm_response: AsyncStream[ChatCompletionChunk]) -> Async
534534 "finish_reason" : "" ,
535535 "model_id" : "" ,
536536 "sample_input" : "" ,
537- "sample_output" : ""
537+ "sample_output" : "" ,
538538 }
539539
540540 if not is_first_choice :
541541 to_ret ["llm_output" ] = response
542- return to_ret # we don't actually use this code path since streaming is not used, so set token counts to 0
542+ return to_ret # we don't actually use this code path since streaming is not used, so set token counts to 0
543543
544544 is_json_format = isinstance (response_format , dict ) and response_format .get ("type" ) == "json_object"
545545 if isinstance (response , AsyncStream ):
@@ -555,22 +555,26 @@ async def format_stream(llm_response: AsyncStream[ChatCompletionChunk]) -> Async
555555 response .usage .completion_tokens if response .usage and response .usage .completion_tokens else 0
556556 )
557557 total_token_count = response .usage .total_tokens if response .usage and response .usage .total_tokens else 0
558- finish_reason = response .choices [0 ].finish_reason if response .choices and response .choices [0 ].finish_reason else ""
558+ finish_reason = (
559+ response .choices [0 ].finish_reason if response .choices and response .choices [0 ].finish_reason else ""
560+ )
559561 model_id = response .model if response .model else ""
560- sample_output_list = [{"role" : response .choices [0 ].message .role , "content" : response .choices [0 ].message .content }] if (response .choices and response .choices [0 ].message .content
561- and response .choices [0 ].message .role ) else []
562+ sample_output_list = (
563+ [{"role" : response .choices [0 ].message .role , "content" : response .choices [0 ].message .content }]
564+ if (response .choices and response .choices [0 ].message .content and response .choices [0 ].message .role )
565+ else []
566+ )
562567 sample_output = json .dumps (sample_output_list )
563568 input_str = f"{ json .dumps (inputs )} " if inputs else ""
564569 if inputs and len (inputs ) > 0 :
565- sample_input_json = []
570+ sample_input_json = []
566571 msg = ChatCompletionUserMessageParam (
567572 role = "user" ,
568573 content = input_str ,
569574 )
570575 sample_input_json .append (msg )
571576 sample_input = json .dumps (sample_input_json )
572577
573-
574578 # When calling function/tool, function_call/tool_call response will be returned as a field in message,
575579 # so we need return message directly. Otherwise, we only return content.
576580 # https://platform.openai.com/docs/api-reference/chat/object#chat/object-choices
@@ -589,6 +593,7 @@ async def format_stream(llm_response: AsyncStream[ChatCompletionChunk]) -> Async
589593 to_ret ["sample_output" ] = sample_output
590594 return to_ret
591595
596+
592597def openai_error_retryable (
593598 error : OpenAIError , retry : int , entity_retry : List [int ], max_entity_retries : int
594599) -> Tuple [bool , float ]:
0 commit comments