Hey, I am building a voice to voice agent using Ge...
# ask-ai
l
Hey, I am building a voice to voice agent using Gemini Live. I made a function tool to end the call when the conversation ends. but its not working properly sometimes it does not cut the call and sometime it cuts prematurely. can you check ?
Copy code
@function_tool
    async def end_call(self, reason: str = "When Conversation has reached a natural conclusion and llm mention to the user that I have captured who you are."):
        """
        End the call when the conversation objectives have been met.
        Use this tool when:
        - All topics have been discussed
        - User indicates they're done
        - Conversation has reached a natural conclusion

        Args:
            reason: A brief explanation of why the call is being ended

        Returns:
            Confirmation message
        """
        if self._call_ended:
            return "Call already ended"

        logger.info(f"Agent ending call. Reason: {reason}")
        self._call_ended = True

        # Schedule disconnect after a short delay to allow final messages
        asyncio.create_task(self._delayed_disconnect())

        return f"Call ended: {reason}"
t
Thanks for reaching out, Pulkit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I understand how important it is for your
end_call
function to work smoothly, so here are some friendly tips to help improve its reliability: • Prevent Interruptions: Try using
context.disallow_interruptions()
right at the start of your function. This helps keep external inputs from interrupting the tool while it’s running. • Validate Context: It’s a good idea to check if
self.job_context
is available before you try to end the call. If it’s missing, you can log an error and gently let the user know what’s going on. • Handle API Calls Properly: When you use the
delete_room
API to end the call, wrapping it in a try-except block can really help manage any unexpected errors gracefully. • Check for Tool Call Cancellations: Keep an eye on your logs for warnings like "server cancelled tool calls"—this can clue you in if the tool execution is being interrupted on the server side. • Test Thoroughly: Running unit tests is a great way to make sure your function behaves just right, including handling those tricky edge cases. By following these steps, you should see improvements with the timing of your call disconnections. If you’d like, I’m here to help you walk through any part of this—just let me know! Sources: telephony/sip_lifecycle.py | livekit-agents/livekit/agents/inference/llm.py | Testing and evaluation | LiveKit Docs | Docs::Agents | Tool definition and use | LiveKit Docs