broad-painter-9222
11/17/2025, 12:45 PMAgentTask class and specify a result type using generics
. Use the on_enter method to begin the task's interaction with the user, and call the complete method with a result when complete. The task has full support for tools, similar to an agent.
from livekit.agents import AgentTask, function_tool
class CollectConsent(AgentTask[bool]):
def __init__(self, chat_ctx=None):
super().__init__(
instructions="""
Ask for recording consent and get a clear yes or no answer.
Be polite and professional.
""",
chat_ctx=chat_ctx,
)
async def on_enter(self) -> None:
await self.session.generate_reply(
instructions="""
Briefly introduce yourself, then ask for permission to record the call for quality assurance and training purposes.
Make it clear that they can decline.
"""
)
@function_tool
async def consent_given(self) -> None:
"""Use this when the user gives consent to record."""
self.complete(True)
@function_tool
async def consent_denied(self) -> None:
"""Use this when the user denies consent to record."""
self.complete(False)
Running a task
The task runs automatically upon creation. It must be created within the context of an existing Agent which is active within an AgentSession. The task takes control of the session until it returns a result. Await the task to receive its result.
from livekit.agents import Agent, function_tool, get_job_context
class CustomerServiceAgent(Agent):
def __init__(self):
super().__init__(instructions="You are a friendly customer service representative.")
async def on_enter(self) -> None:
if await CollectConsent(chat_ctx=self.chat_ctx):
await self.session.generate_reply(instructions="Offer your assistance to the user.")
else:
await self.session.generate_reply(instructions="Inform the user that you are unable to proceed and will end the call.")
job_ctx = get_job_context()
await job_ctx.api.room.delete_room(api.DeleteRoomRequest(room=job_ctx
in this case do I need to explicitly handoff back to the CustomerServiceAgent?tall-airline-16756
11/17/2025, 12:45 PMtall-airline-16756
11/17/2025, 12:45 PMAgent. Once a task is completed, the framework kindly takes care of restoring the session to the parent agent's context for you, so everything continues smoothly. This is all built into how the session management works, and you usually won’t need to add any extra code—unless you want to customize the behavior with your own specific logic. If you have any questions or want to explore that, I’m here to help!
Sources: livekit-agents/livekit/agents/voice/run_result.py | Tool definition and use | LiveKit docsbroad-painter-9222
11/17/2025, 12:51 PMtall-airline-16756
11/17/2025, 12:52 PMAgent in your unit test is actually expected behavior. When an AgentTask completes, it quietly resolves its result and merges its chat context back into the parent agent's context. This process doesn’t trigger a handoff event because handoffs are specifically for explicit agent transitions, like when a new agent instance is returned from a tool function.
To make sure everything is working as it should in your test, try checking for function call outputs or messages from the parent agent after the task finishes, instead of looking for a handoff event. If anything feels unclear or you’d like a hand with your test setup, please don’t hesitate to reach out—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/agent.py | Testing and evaluation | LiveKit Docs | Workflows | LiveKit docs | agents/src/voice/agent_activity.tsbroad-painter-9222
11/17/2025, 12:53 PMtall-airline-16756
11/17/2025, 12:53 PMon_enter method might not be generating a reply after an AgentTask completes because it doesn’t explicitly call session.generate_reply() or session.say(). Since on_enter is empty by default unless you override it, no reply will happen unless you add that part yourself.
To fix this, you’ll want to make sure the parent agent’s on_enter method includes the steps to generate a reply once the task is done. If you’d like, I’m here to help guide you through how to implement that!
Sources: livekit-agents/livekit/agents/voice/agent_activity.py | Workflows | LiveKit docs | Pipeline nodes and hooks | LiveKit docs | home_assistant/homeautomation.pybroad-painter-9222
11/17/2025, 12:55 PMtall-airline-16756
11/17/2025, 12:56 PMsession.say or session.generate_reply don’t seem to run in your on_enter method after an AgentTask finishes. Let’s walk through some common reasons together:
• Session State: It’s important to check that your AgentSession is active when you call these methods. If the session is closing or hasn’t fully started, these calls might not work as expected.
• Activity Interruption: Sometimes, if the session state changes or a handoff happens right when the task completes, it can interrupt these method executions.
• Await Usage: Make sure you’re properly using await with session.generate_reply() or session.say() so they have time to finish before moving on.
• TTS Configuration: Remember, session.say needs a TTS plugin set up. Without it, speech won’t be generated.
• Task Completion Handling: After the task finishes, the switch back to the parent agent might not handle speech generation unless it’s explicitly coded to do so.
To get this sorted, I recommend double-checking the session state, ensuring you’re awaiting those methods properly, and confirming your TTS plugin is configured. If you’re still stuck after that, please don’t hesitate to reach out—I’m here to help you dig deeper and find a solution!
Sources: pipeline-tts/short_replies_only.py | Events and error handling | LiveKit docs | livekit-agents/livekit/agents/voice/agent.py | Agent speech and audio | LiveKit docs | Workflows | LiveKit docsbroad-painter-9222
11/17/2025, 1:35 PMtall-airline-16756
11/17/2025, 1:36 PMbroad-painter-9222
11/17/2025, 2:49 PMawait session.run(user_input="I don't know.")^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ tests/voice/test_experimental_interviewer.py171 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ .venv/lib/python3.12/site-packages/livekit/agents/voice/agent_session.py421 in run self.generate_reply(user_input=user_input) .venv/lib/python3.12/site-packages/livekit/agents/voice/agent_session.py838 in generate_reply handle = self._next_activity._generate_reply( .venv/lib/python3.12/site-packages/livekit/agents/voice/agent_activity.py851 in _generate_reply self._schedule_speech(handle, SpeechHandle.SPEECH_PRIORITY_NORMAL) _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ self = <livekit.agents.voice.agent_activity.AgentActivity object at 0x783e2cec9700>, speech = <livekit.agents.voice.speech_handle.SpeechHandle object at 0x783e3c2c2c30>, priority = 5 force = False def _schedule_speech(self, speech: SpeechHandle, priority: int, force: bool = False) -> None: # when force=True, we still allow to schedule a new speech even if #
pause_speech_scheduling is waiting for the schedule_task to drain.
# This allows for tool responses to be generated before the AgentActivity is finalized.
if self._scheduling_paused and not force:
raise RuntimeError("cannot schedule new speech, the speech scheduling is draining/pausing" ) E RuntimeError: cannot schedule new speech, the speech scheduling is draining/pausing what could be the root cause of this issue?
tall-airline-16756
11/17/2025, 2:50 PMbroad-painter-9222
11/17/2025, 2:51 PMtall-airline-16756
11/17/2025, 2:52 PMAgentTask completes. There are a few common reasons this could happen:
• LLM Not Configured: Please double-check that your session has a valid LLM instance assigned, since generate_reply depends on it.
• Session State: Make sure the session is active and not in a closing or draining state when you call generate_reply.
• Tool Execution Logic: If the tool used in the task doesn’t return a valid output, the reply might not be triggered. It’s important that the tool returns a valid object or string.
• Agent Handoff: When multiple agent tasks are returned or a handoff happens, reply generation can sometimes be suppressed. Try to ensure only one agent task is returned.
• Speech Scheduling: Check that scheduleSpeech is enabled when calling generate_reply.
To move forward, please verify the LLM configuration, session state, and that your tool returns the expected values. If things still aren’t working, detailed logging of agent state transitions and tool executions can be really helpful to pinpoint the root cause. Feel free to reach out if you’d like me to guide you through the debugging process—I’m here to support you every step of the way!
Sources: main.py | Agent speech and audio | LiveKit docs | agents/src/voice/agent_activity.ts | tests/test_agent_session.py | Events and error handling | LiveKit docsbroad-painter-9222
11/17/2025, 2:53 PMtall-airline-16756
11/17/2025, 2:53 PMstring, number, boolean, null, or undefined
• Arrays, as long as all the elements inside are valid
• Objects, where all keys are primitives and all values are valid
If a tool happens to return something invalid—like a function or an unserializable object—LiveKit will log an error and discard that output. To help you spot these, you can:
• Check the logs for any errors related to invalid tool outputs
• Look at the FunctionToolsExecuted event, which only includes outputs that passed validation
• Use assertions in your testing framework to confirm your outputs are as expected
To keep things smooth, it’s best to return serializable data types and avoid things like functions, classes, or circular references. If you’d like, I’m here to help you debug any specific tool output you’re working with—just let me know!
Sources: Testing and evaluation | LiveKit Docs | Agent Session Management & Turn Detection | livekit/livekit_composite | Dee... | agents/src/voice/agent_activity.ts | livekit-agents/livekit/agents/voice/generation.pybroad-painter-9222
11/17/2025, 2:55 PMtall-airline-16756
11/17/2025, 2:56 PMoutput="") for your AgentTask tool, which usually happens when the tool's return value is invalid, None, or not properly serialized. Don’t worry, let’s walk through some common causes and how you can fix them:
• Invalid Output Type: Make sure your tool returns a serializable type, like a string, number, boolean, dict, or list. It’s best to avoid returning functions, class instances, or objects that can’t be serialized.
• Tool Returns `None`: If your tool is returning None explicitly, that will result in an empty string output. Try returning a meaningful value instead to see the results you expect.
• Tool Raises an Exception: If there’s an error during the tool’s execution, the output will be marked as an error and show as empty. Checking the logs for any exceptions can really help pinpoint the issue.
• Multiple AgentTasks: If more than one AgentTask is returned, the output might get ignored. It’s a good idea to ensure only one task is returned at a time.
To get this sorted, just double-check that your tool returns valid, serializable data and that any exceptions are handled smoothly. And if you’d like, I’m here to help you debug a specific tool—just let me know!
Sources: Tool definition and use | LiveKit docs | agents/src/voice/generation.ts | livekit-agents/livekit/agents/voice/agent_activity.pybroad-painter-9222
11/17/2025, 2:56 PMtall-airline-16756
11/17/2025, 2:57 PMconfirm_comprehensive_answer and decline_comprehensive_answer are custom-defined. Could you please share if these tools are part of your own implementation, or if you’re expecting them to follow a particular example or built-in behavior? Understanding this will really help me give you a more focused and helpful review of your implementation.
Sources: Workflows | LiveKit docs | livekit-agents/livekit/agents/voice/agent.pybroad-painter-9222
11/17/2025, 2:57 PMtall-airline-16756
11/17/2025, 2:57 PMconfirm_comprehensive_answer and decline_comprehensive_answer return valid, serializable outputs. For instance, try to avoid returning None or any non-serializable objects.
• Task Completion: The self.complete() method should be called with a valid result. Let’s double-check that the result passed to self.complete() is being properly handled by the parent agent.
• Session State: Please ensure the AgentSession is active and not in a paused or draining state when these tools run.
• Exception Handling: It might be helpful to add some error handling within the tools to log and manage any unexpected issues that come up during execution.
• Integration with Parent Agent: Let’s also verify that the parent agent correctly resumes control and processes the task result once the task finishes.
If you notice any specific hiccups, like empty outputs or unexpected behavior, please don’t hesitate to reach out. I’m here to help you work through any debugging steps together!