@@ -328,23 +328,27 @@ async def _handle_sse_response(
328328 is_initialization : bool = False ,
329329 ) -> None :
330330 """Handle SSE response from the server."""
331- event_source = EventSource (response )
332- finished = False
333- async for sse in event_source .aiter_sse ():
334- is_complete = await self ._handle_sse_event (
335- sse ,
336- ctx .read_stream_writer ,
337- resumption_callback = (ctx .metadata .on_resumption_token_update if ctx .metadata else None ),
338- is_initialization = is_initialization ,
339- )
340- # If the SSE event indicates completion, like returning respose/error
341- # break the loop
342- if is_complete :
343- finished = True
344- await response .aclose ()
345- break
346- if not finished :
347- raise Exception ("SSE stream ended without completing" )
331+ try :
332+ event_source = EventSource (response )
333+ finished = False
334+ async for sse in event_source .aiter_sse ():
335+ is_complete = await self ._handle_sse_event (
336+ sse ,
337+ ctx .read_stream_writer ,
338+ resumption_callback = (ctx .metadata .on_resumption_token_update if ctx .metadata else None ),
339+ is_initialization = is_initialization ,
340+ )
341+ # If the SSE event indicates completion, like returning respose/error
342+ # break the loop
343+ if is_complete :
344+ finished = True
345+ await response .aclose ()
346+ break
347+ if not finished :
348+ raise Exception ("SSE stream ended without completing" )
349+ except Exception as exc :
350+ logger .exception ("Error handling SSE response" )
351+ await self ._send_error_response (ctx , exc )
348352
349353 async def _handle_unexpected_content_type (
350354 self ,
@@ -410,13 +414,10 @@ async def post_writer(
410414 )
411415
412416 async def handle_request_async ():
413- try :
414- if is_resumption :
415- await self ._handle_resumption_request (ctx )
416- else :
417- await self ._handle_post_request (ctx )
418- except Exception as e :
419- await self ._send_error_response (ctx , e )
417+ if is_resumption :
418+ await self ._handle_resumption_request (ctx )
419+ else :
420+ await self ._handle_post_request (ctx )
420421
421422 # If this is a request, start a new task to handle it
422423 if isinstance (message .root , JSONRPCRequest ):
0 commit comments