Hello, how do I shutdown the current session prope...
# agents
b
Hello, how do I shutdown the current session properly using realtime Gemini models and function_tools?
Copy code
@function_tool()
    async def end_call(self) -> None:
        """Use this tool to end the call."""
        await self.session.generate_reply()
        get_job_context().shutdown()
        return None
This is working so far for me, the agent says goodbye, and then it shutdowns the session, triggers my shutdown_callback but it logs out `{"message": "failed to publish transcription\nTraceback (most recent call last):`every time, as if it shutdowns the session prematurely, how do I fix this problem? Is it even something that needs fixing? How can I achieve a seemingly trivial function of my LLM calling end_call -> Generate and output its replies -> actually shutdown when the output finishes? I cant seem to get it to work.
r
Try and wait for generate reply to finish firat: https://docs.livekit.io/agents/build/audio/#speechhandle
Copy code
@function_tool()  
    async def end_call(self) -> None:  
        """Use this tool to end the call."""  
        # Generate and wait for the reply to complete
        speech_handle = self.session.generate_reply(instructions="Thank you for calling. Goodbye!")

        # Wait for the speech to actually finish  
        await speech_handle  
        
        # Now safely close the session  
        await self.session.drain()  
        await self.session.aclose()  
        
        return None
b
I think I'm already doing in the same thing with
await self.session.generate_reply()
, also I was doing
self._closing_task = asyncio.create_task(self.session.aclose())
following this example https://github.com/livekit/agents/blob/main/examples/voice_agents/session_close_callback.py but it seemed like the session did end but the agent never disconnects from the room or the room never gets closed, leaving my user hanging alone in the client side UI.
I will try awaiting in the function.