@@ -234,12 +234,19 @@ def generate(cancellation_token_id: str): # type: ignore
234234 # Use multi-threading so that we don't block run command from
235235 # displaying the streamed output (if streaming is supported)
236236 def run_async_config_in_thread ():
237- asyncio .run (aiconfig .run (prompt_name = prompt_name , params = params , run_with_dependencies = False , options = inference_options )) # type: ignore
237+ try :
238+ asyncio .run (aiconfig .run (prompt_name = prompt_name , params = params , run_with_dependencies = False , options = inference_options )) # type: ignore
239+ except Exception as e :
240+ output_text_queue .put (e )
241+
238242 output_text_queue .put (STOP_STREAMING_SIGNAL ) # type: ignore
239243
240- def create_cancellation_payload ( ):
244+ def create_error_payload ( message : str , code : int ):
241245 aiconfig_json = aiconfig_deep_copy .model_dump (exclude = EXCLUDE_OPTIONS ) if aiconfig_deep_copy is not None else None
242- return json .dumps ({"error" : {"message" : "The task was cancelled." , "code" : 499 , "data" : aiconfig_json }})
246+ return json .dumps ({"error" : {"message" : message , "code" : code , "data" : aiconfig_json }})
247+
248+ def create_cancellation_payload ():
249+ return create_error_payload (message = "The task was cancelled." , code = 499 )
243250
244251 def handle_cancellation ():
245252 yield "["
@@ -307,7 +314,10 @@ def kill_thread(thread_id: int | None):
307314 yield from handle_cancellation ()
308315 return
309316
310- if isinstance (text , str ):
317+ if isinstance (text , Exception ):
318+ yield from create_error_payload (message = f"Exception: { text } " , code = 500 )
319+ return
320+ elif isinstance (text , str ):
311321 accumulated_output_text += text
312322 elif isinstance (text , dict ) and "content" in text :
313323 # TODO: Fix streaming output format so that it returns text
0 commit comments