the end_call tool function is not working, i was u...
# ask-ai
p
the end_call tool function is not working, i was using 0.12 livekit-agents and back then it was working just fine, then after upgrading to 1.22 version it is not working and the call keep going the code class InoundtAgent(Agent): def __init__(self, chat_ctx: ChatContext, instructions: str): super().__init__(chat_ctx=chat_ctx, instructions=instructions) async def hangup(self): """Helper function to hang up the call by deleting the room""" # Allow a small delay for cleanup await asyncio.sleep(1) # Now delete the room job_ctx = get_job_context() await job_ctx.delete_room() @function_tool() async def end_call(self, ctx: RunContext): """Called when the user wants to end the call""" current_speech = ctx.session.current_speech if current_speech: await current_speech.wait_for_playout() await self.hangup()
t
Thanks for reaching out, Yasser. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The issue with the
end_call
function not working after upgrading from
livekit-agents
0.12 to 1.22 is probably because of some changes in how tool functions are defined and handled in the newer version. No worries though, here’s a simple way to fix it: • Make sure you’re using the
@function_tool()
decorator correctly, since the older
@llm.ai_callable
decorator isn’t supported anymore. • The function now needs to accept a
RunContext
object as its parameter. • Use
get_job_context()
to get the job context when deleting the room. Here’s an updated example you can try out:
Copy code
from livekit.agents import get_job_context
from livekit.agents import function_tool

@function_tool()
async def end_call(ctx: RunContext):
    """End the current call by deleting the room."""
    try:
        # Let the current speech finish before ending the call
        current_speech = ctx.session.current_speech
        if current_speech:
            await current_speech.wait_for_playout()

        # Get the job context
        job_ctx = get_job_context()
        if job_ctx is None:
            return "Failed to end call: No job context available"

        # Delete the room to end the call
        await job_ctx.api.room.delete_room(
            api.DeleteRoomRequest(room=job_ctx.room.name)
        )

        return "Call ended successfully."

    except Exception as e:
        return f"Failed to end call: {e}"
Give this a try, and if you hit any snags or want to chat more about it, just let me know—I’m here to help! Sources: Agents 0.x migration guide | LiveKit Docs | Agents v0.x migration guide | LiveKit Docs | Agents telephony integration | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py