@@ -786,7 +786,7 @@ async def route_orchestrated_disaggregated_request(
786786
787787 try :
788788 # Use the shared aiohttp client from app state
789- client = request .app .state .aiohttp_client_wrapper ()
789+ client : aiohttp . ClientSession = request .app .state .aiohttp_client_wrapper ()
790790
791791 # Send to Prefill
792792 async with client .post (
@@ -835,57 +835,60 @@ async def route_orchestrated_disaggregated_request(
835835 decode_api_url = f"{ decode_url } { endpoint } "
836836 logger .info (f"[{ request_id } ] Sending decode request to { decode_api_url } " )
837837
838- async with client .post (
838+ decode_resp = await client .post (
839839 decode_api_url ,
840840 json = decode_request ,
841841 headers = {
842842 "Content-Type" : "application/json" ,
843843 "X-Request-Id" : request_id ,
844844 },
845845 timeout = aiohttp .ClientTimeout (total = 600 ),
846- ) as decode_resp :
847- if decode_resp .status != 200 :
848- error_text = await decode_resp .text ()
849- logger .error (
850- f"[{ request_id } ] Decode failed with status { decode_resp .status } : { error_text } "
851- )
852- return JSONResponse (
853- status_code = decode_resp .status ,
854- content = {"error" : f"Decode failed: { error_text } " },
855- headers = {"X-Request-Id" : request_id },
856- )
846+ )
847+ if decode_resp .status != 200 :
848+ error_text = await decode_resp .text ()
849+ decode_resp .release ()
850+ logger .error (
851+ f"[{ request_id } ] Decode failed with status { decode_resp .status } : { error_text } "
852+ )
853+ return JSONResponse (
854+ status_code = decode_resp .status ,
855+ content = {"error" : f"Decode failed: { error_text } " },
856+ headers = {"X-Request-Id" : request_id },
857+ )
857858
858- if is_streaming :
859- # For streaming, yield chunks as they arrive (true streaming)
860- async def generate_stream ():
861- try :
862- async for chunk in decode_resp .content .iter_any ():
863- if chunk :
864- yield chunk
865- finally :
866- curr_time = time .time ()
867- logger .info (
868- f"[{ request_id } ] Orchestrated streaming request completed, total time = { curr_time - in_router_time :.4f} s"
869- )
870-
871- return StreamingResponse (
872- generate_stream (),
873- media_type = "text/event-stream" ,
874- headers = {"X-Request-Id" : request_id },
875- )
876- else :
877- # For non-streaming, read full response
878- response_data = await decode_resp .read ()
859+ if is_streaming :
860+ # For streaming, yield chunks as they arrive (true streaming)
861+ async def generate_stream ():
862+ try :
863+ async for chunk in decode_resp .content .iter_any ():
864+ if chunk :
865+ yield chunk
866+ finally :
867+ decode_resp .release ()
868+ curr_time = time .time ()
869+ logger .info (
870+ f"[{ request_id } ] Orchestrated streaming request completed, total time = { curr_time - in_router_time :.4f} s"
871+ )
879872
880- curr_time = time .time ()
881- logger .info (
882- f"[{ request_id } ] Orchestrated request completed, total time = { curr_time - in_router_time :.4f} s"
883- )
873+ return StreamingResponse (
874+ generate_stream (),
875+ media_type = "text/event-stream" ,
876+ headers = {"X-Request-Id" : request_id },
877+ )
878+ else :
879+ # For non-streaming, read full response
880+ response_data = await decode_resp .read ()
881+ decode_resp .release ()
884882
885- return JSONResponse (
886- content = json .loads (response_data ),
887- headers = {"X-Request-Id" : request_id },
888- )
883+ curr_time = time .time ()
884+ logger .info (
885+ f"[{ request_id } ] Orchestrated request completed, total time = { curr_time - in_router_time :.4f} s"
886+ )
887+
888+ return JSONResponse (
889+ content = json .loads (response_data ),
890+ headers = {"X-Request-Id" : request_id },
891+ )
889892
890893 except aiohttp .ClientError as e :
891894 logger .error (
0 commit comments