@@ -307,7 +307,7 @@ async def run_evaluation(job_id: str, config_file: str, reps: int, session_manag
307307 async def start_evaluation (request : EvaluateRequest , background_tasks : BackgroundTasks , http_request : Request ):
308308 """Handle evaluation requests."""
309309
310- async with session_manager .session (request = http_request ):
310+ async with session_manager .session (http_connection = http_request ):
311311
312312 # if job_id is present and already exists return the job info
313313 if request .job_id :
@@ -336,7 +336,7 @@ async def get_job_status(job_id: str, http_request: Request) -> EvaluateStatusRe
336336 """Get the status of an evaluation job."""
337337 logger .info ("Getting status for job %s" , job_id )
338338
339- async with session_manager .session (request = http_request ):
339+ async with session_manager .session (http_connection = http_request ):
340340
341341 job = job_store .get_job (job_id )
342342 if not job :
@@ -349,7 +349,7 @@ async def get_last_job_status(http_request: Request) -> EvaluateStatusResponse:
349349 """Get the status of the last created evaluation job."""
350350 logger .info ("Getting last job status" )
351351
352- async with session_manager .session (request = http_request ):
352+ async with session_manager .session (http_connection = http_request ):
353353
354354 job = job_store .get_last_job ()
355355 if not job :
@@ -361,7 +361,7 @@ async def get_last_job_status(http_request: Request) -> EvaluateStatusResponse:
361361 async def get_jobs (http_request : Request , status : str | None = None ) -> list [EvaluateStatusResponse ]:
362362 """Get all jobs, optionally filtered by status."""
363363
364- async with session_manager .session (request = http_request ):
364+ async with session_manager .session (http_connection = http_request ):
365365
366366 if status is None :
367367 logger .info ("Getting all jobs" )
@@ -572,7 +572,7 @@ async def get_single(response: Response, request: Request):
572572
573573 response .headers ["Content-Type" ] = "application/json"
574574
575- async with session_manager .session (request = request ,
575+ async with session_manager .session (http_connection = request ,
576576 user_authentication_callback = self ._http_flow_handler .authenticate ):
577577
578578 return await generate_single_response (None , session_manager , result_type = result_type )
@@ -583,7 +583,7 @@ def get_streaming_endpoint(streaming: bool, result_type: type | None, output_typ
583583
584584 async def get_stream (request : Request ):
585585
586- async with session_manager .session (request = request ,
586+ async with session_manager .session (http_connection = request ,
587587 user_authentication_callback = self ._http_flow_handler .authenticate ):
588588
589589 return StreamingResponse (headers = {"Content-Type" : "text/event-stream; charset=utf-8" },
@@ -618,7 +618,7 @@ async def post_single(response: Response, request: Request, payload: request_typ
618618
619619 response .headers ["Content-Type" ] = "application/json"
620620
621- async with session_manager .session (request = request ,
621+ async with session_manager .session (http_connection = request ,
622622 user_authentication_callback = self ._http_flow_handler .authenticate ):
623623
624624 return await generate_single_response (payload , session_manager , result_type = result_type )
@@ -632,7 +632,7 @@ def post_streaming_endpoint(request_type: type,
632632
633633 async def post_stream (request : Request , payload : request_type ):
634634
635- async with session_manager .session (request = request ,
635+ async with session_manager .session (http_connection = request ,
636636 user_authentication_callback = self ._http_flow_handler .authenticate ):
637637
638638 return StreamingResponse (headers = {"Content-Type" : "text/event-stream; charset=utf-8" },
@@ -677,7 +677,7 @@ async def post_openai_api_compatible(response: Response, request: Request, paylo
677677 # Check if streaming is requested
678678 stream_requested = getattr (payload , 'stream' , False )
679679
680- async with session_manager .session (request = request ):
680+ async with session_manager .session (http_connection = request ):
681681 if stream_requested :
682682 # Return streaming response
683683 return StreamingResponse (headers = {"Content-Type" : "text/event-stream; charset=utf-8" },
@@ -688,42 +688,41 @@ async def post_openai_api_compatible(response: Response, request: Request, paylo
688688 step_adaptor = self .get_step_adaptor (),
689689 result_type = ChatResponseChunk ,
690690 output_type = ChatResponseChunk ))
691- else :
692- # Return single response - check if workflow supports non-streaming
693- try :
691+
692+ # Return single response - check if workflow supports non-streaming
693+ try :
694+ response .headers ["Content-Type" ] = "application/json"
695+ return await generate_single_response (payload , session_manager , result_type = ChatResponse )
696+ except ValueError as e :
697+ if "Cannot get a single output value for streaming workflows" in str (e ):
698+ # Workflow only supports streaming, but client requested non-streaming
699+ # Fall back to streaming and collect the result
700+ chunks = []
701+ async for chunk_str in generate_streaming_response_as_str (
702+ payload ,
703+ session_manager = session_manager ,
704+ streaming = True ,
705+ step_adaptor = self .get_step_adaptor (),
706+ result_type = ChatResponseChunk ,
707+ output_type = ChatResponseChunk ):
708+ if chunk_str .startswith ("data: " ) and not chunk_str .startswith ("data: [DONE]" ):
709+ chunk_data = chunk_str [6 :].strip () # Remove "data: " prefix
710+ if chunk_data :
711+ try :
712+ chunk_json = ChatResponseChunk .model_validate_json (chunk_data )
713+ if (chunk_json .choices and len (chunk_json .choices ) > 0
714+ and chunk_json .choices [0 ].delta
715+ and chunk_json .choices [0 ].delta .content is not None ):
716+ chunks .append (chunk_json .choices [0 ].delta .content )
717+ except Exception :
718+ continue
719+
720+ # Create a single response from collected chunks
721+ content = "" .join (chunks )
722+ single_response = ChatResponse .from_string (content )
694723 response .headers ["Content-Type" ] = "application/json"
695- return await generate_single_response (payload , session_manager , result_type = ChatResponse )
696- except ValueError as e :
697- if "Cannot get a single output value for streaming workflows" in str (e ):
698- # Workflow only supports streaming, but client requested non-streaming
699- # Fall back to streaming and collect the result
700- chunks = []
701- async for chunk_str in generate_streaming_response_as_str (
702- payload ,
703- session_manager = session_manager ,
704- streaming = True ,
705- step_adaptor = self .get_step_adaptor (),
706- result_type = ChatResponseChunk ,
707- output_type = ChatResponseChunk ):
708- if chunk_str .startswith ("data: " ) and not chunk_str .startswith ("data: [DONE]" ):
709- chunk_data = chunk_str [6 :].strip () # Remove "data: " prefix
710- if chunk_data :
711- try :
712- chunk_json = ChatResponseChunk .model_validate_json (chunk_data )
713- if (chunk_json .choices and len (chunk_json .choices ) > 0
714- and chunk_json .choices [0 ].delta
715- and chunk_json .choices [0 ].delta .content is not None ):
716- chunks .append (chunk_json .choices [0 ].delta .content )
717- except Exception :
718- continue
719-
720- # Create a single response from collected chunks
721- content = "" .join (chunks )
722- single_response = ChatResponse .from_string (content )
723- response .headers ["Content-Type" ] = "application/json"
724- return single_response
725- else :
726- raise
724+ return single_response
725+ raise
727726
728727 return post_openai_api_compatible
729728
@@ -758,7 +757,7 @@ async def start_async_generation(
758757 http_request : Request ) -> AsyncGenerateResponse | AsyncGenerationStatusResponse :
759758 """Handle async generation requests."""
760759
761- async with session_manager .session (request = http_request ):
760+ async with session_manager .session (http_connection = http_request ):
762761
763762 # if job_id is present and already exists return the job info
764763 if request .job_id :
@@ -804,7 +803,7 @@ async def get_async_job_status(job_id: str, http_request: Request) -> AsyncGener
804803 """Get the status of an async job."""
805804 logger .info ("Getting status for job %s" , job_id )
806805
807- async with session_manager .session (request = http_request ):
806+ async with session_manager .session (http_connection = http_request ):
808807
809808 job = job_store .get_job (job_id )
810809 if not job :
0 commit comments