@@ -248,6 +248,15 @@ async def get_conversation_endpoint_handler(
248248 client = AsyncLlamaStackClientHolder ().get_client ()
249249
250250 agent_sessions = (await client .agents .session .list (agent_id = agent_id )).data
251+ if not agent_sessions :
252+ logger .error ("No sessions found for conversation %s" , conversation_id )
253+ raise HTTPException (
254+ status_code = status .HTTP_404_NOT_FOUND ,
255+ detail = {
256+ "response" : "Conversation not found" ,
257+ "cause" : f"Conversation { conversation_id } could not be deleted: no sessions found" ,
258+ },
259+ )
251260 session_id = str (agent_sessions [0 ].get ("session_id" ))
252261
253262 session_response = await client .agents .session .retrieve (
@@ -283,6 +292,8 @@ async def get_conversation_endpoint_handler(
283292 "cause" : f"Conversation { conversation_id } could not be retrieved: { str (e )} " ,
284293 },
285294 ) from e
295+ except HTTPException :
296+ raise
286297 except Exception as e :
287298 # Handle case where session doesn't exist or other errors
288299 logger .exception ("Error retrieving conversation %s: %s" , conversation_id , e )
@@ -339,11 +350,22 @@ async def delete_conversation_endpoint_handler(
339350 try :
340351 # Get Llama Stack client
341352 client = AsyncLlamaStackClientHolder ().get_client ()
342- # Delete session using the conversation_id as session_id
343- # In this implementation, conversation_id and session_id are the same
344- await client .agents .session .delete (
345- agent_id = agent_id , session_id = conversation_id
346- )
353+
354+ agent_sessions = (await client .agents .session .list (agent_id = agent_id )).data
355+
356+ if not agent_sessions :
357+ logger .error ("No sessions found for conversation %s" , conversation_id )
358+ raise HTTPException (
359+ status_code = status .HTTP_404_NOT_FOUND ,
360+ detail = {
361+ "response" : "Conversation not found" ,
362+ "cause" : f"Conversation { conversation_id } could not be deleted: no sessions found" ,
363+ },
364+ )
365+
366+ session_id = str (agent_sessions [0 ].get ("session_id" ))
367+
368+ await client .agents .session .delete (agent_id = agent_id , session_id = session_id )
347369
348370 logger .info ("Successfully deleted conversation %s" , conversation_id )
349371
@@ -371,6 +393,8 @@ async def delete_conversation_endpoint_handler(
371393 "cause" : f"Conversation { conversation_id } could not be deleted: { str (e )} " ,
372394 },
373395 ) from e
396+ except HTTPException :
397+ raise
374398 except Exception as e :
375399 # Handle case where session doesn't exist or other errors
376400 logger .exception ("Error deleting conversation %s: %s" , conversation_id , e )
0 commit comments