bulky-car-15738
12/05/2025, 4:52 PMtall-airline-16756
12/05/2025, 4:52 PMtall-airline-16756
12/05/2025, 4:52 PMfrom livekit.agents.beta.workflows import GetEmailTask
from livekit.agents import function_tool
@function_tool()
async def collect_contact_info(self) -> str:
# collects and validates an email address from the user
email_result = await GetEmailTask(chat_ctx=self.chat_ctx)
return f"Collected email: {email_result.email_address}"
2) Customizing behavior with extra instructions and tools
• Pass extra_instructions to change LLM guidance.
• Pass tools (functions decorated as tools) to handle alternatives (e.g., user declines to provide email).
Example:
@function_tool()
async def get_alternate_contact_info(context, contact_method: str, contact_value: str) -> None:
context.session.userdata.alternate_contact_method = contact_method
context.session.userdata.alternate_contact_value = contact_value
await context.session.generate_reply(instructions=f"Recorded your {contact_method}: {contact_value}.")
email_result = await GetEmailTask(
chat_ctx=self.chat_ctx,
extra_instructions="If the user cannot provide an email, call get_alternate_contact_info() instead of decline_email_capture().",
tools=[get_alternate_contact_info]
)
3) Main configuration options
• `chat_ctx`: conversation context (required).
• `extra_instructions`: string to guide the task's agent behavior.
• `tools`: optional list of tool functions to handle custom flows.
If you'd like, tell me whether you want a version that:
• Validates specific domains,
• Records an alternate contact when a user declines, or
• Runs inside a larger workflow — and I’ll provide the exact code snippet. Sources: Tasks and task groups | LiveKit docsbulky-car-15738
12/05/2025, 5:37 PMtall-airline-16756
12/05/2025, 5:37 PMfrom dataclasses import dataclass
@dataclass
class PhoneNumberResult:
phone_number: str
2) Custom task implementation
import re
from livekit.agents import AgentTask, function_tool
class GetPhoneNumberTask(AgentTask[PhoneNumberResult]):
def __init__(self, chat_ctx=None, instructions: str = None):
instr = instructions or """
Collect a user's phone number. Ask clearly, accept spoken digits or keypad input,
normalize to digits, validate, and confirm with the user before completing.
"""
super().__init__(instructions=instr, chat_ctx=chat_ctx)
async def on_enter(self) -> None:
await self.session.generate_reply(
instructions="Please provide your phone number (10 digits). You can say it digit-by-digit or enter it on your keypad."
)
@function_tool()
async def capture_phone_number(self, phone_number: str) -> None:
# normalize: remove non-digits
digits = re.sub(r"\D", "", phone_number or "")
# allow 10-digit or 11 starting with '1'
if len(digits) == 10 or (len(digits) == 11 and digits.startswith("1")):
normalized = "+1" + digits[-10:] # adjust formatting if needed
# store temporarily and ask for confirmation
self.temp_number = normalized
pretty = f"({normalized[2:5]}) {normalized[5:8]}-{normalized[8:]}"
await self.session.generate_reply(instructions=f"I heard {pretty}. Is that correct?")
else:
await self.session.generate_reply(instructions="That doesn't look like a valid 10-digit phone number. Please try again.")
@function_tool()
async def confirmation_received(self) -> None:
if hasattr(self, "temp_number"):
self.complete(PhoneNumberResult(phone_number=self.temp_number))
else:
await self.session.generate_reply(instructions="No phone number captured yet. Please provide it.")
@function_tool()
async def confirmation_rejected(self) -> None:
if hasattr(self, "temp_number"):
delattr(self, "temp_number")
await self.session.generate_reply(instructions="Okay, please repeat your phone number.")
3) Using the task in an agent
result = await GetPhoneNumberTask(chat_ctx=self.chat_ctx)
# result is PhoneNumberResult; access result.phone_number
await self.session.generate_reply(instructions=f"Thanks — saved {result.phone_number}.")
Notes and quick options
• For non-US numbers, adjust validation/formatting rules.
• If you want DTMF-only collection, use GetDtmfTask with num_digits=10 instead.
• To handle user refusal, add an extra tool function and pass it in via a tools list and/or include extra_instructions.
Would you like a variant that supports international formats or a DTMF-only example?
Sources: Tasks and task groups | LiveKit docs | Handling DTMF | LiveKit docs