diff --git a/python/packages/kagent-adk/src/kagent/adk/_a2a.py b/python/packages/kagent-adk/src/kagent/adk/_a2a.py index def2ff1cc..04e17682b 100644 --- a/python/packages/kagent-adk/src/kagent/adk/_a2a.py +++ b/python/packages/kagent-adk/src/kagent/adk/_a2a.py @@ -123,14 +123,23 @@ async def test(self, task: str): content = types.Content(role="user", parts=[types.Part(text=task)]) # Key Concept: run_async executes the agent logic and yields Events. # We iterate through events to find the final answer. - async for event in runner.run_async( - user_id=USER_ID, - session_id=SESSION_ID, - new_message=content, - ): - # You can uncomment the line below to see *all* events during execution - # print(f" [Event] Author: {event.author}, Type: {type(event).__name__}, Final: {event.is_final_response()}, Content: {event.content}") - - # Key Concept: is_final_response() marks the concluding message for the turn. - jsn = event.model_dump_json() - logger.info(f" [Event] {jsn}") + try: + async for event in runner.run_async( + user_id=USER_ID, + session_id=SESSION_ID, + new_message=content, + ): + # You can uncomment the line below to see *all* events during execution + # print(f" [Event] Author: {event.author}, Type: {type(event).__name__}, Final: {event.is_final_response()}, Content: {event.content}") + + # Key Concept: is_final_response() marks the concluding message for the turn. + jsn = event.model_dump_json() + logger.info(f" [Event] {jsn}") + finally: + # Ensure proper cleanup of any async resources + try: + # Close any open connections or resources + if hasattr(runner, 'close'): + await runner.close() + except Exception as cleanup_error: + logger.warning(f"Error during cleanup: {cleanup_error}")