how can I use <session.sa>y or sesion.reply inside...
# ask-ai
a
how can I use session.say or sesion.reply inside a function tool?
t
Thanks for reaching out, Aditya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You can use
session.say()
and
session.generate_reply()
directly inside a function tool. Here are the key patterns and considerations: • Direct Calls: Within your
@function_tool
-decorated function, use
await self.session.say("Your message here")
for predefined messages, or
self.session.generate_reply(instructions="...")
for dynamic, LLM-generated responses. • Automatic tool_choice for generate_reply: When you call
generate_reply()
inside a function tool, the
tool_choice
is automatically set to
none
. This prevents further nested or cascading tool invocations during the reply, ensuring safe usage. • Session Access: Use
self.session
to access speech methods inside the function tool implementation. • TTS Requirement:
session.say()
requires an available TTS plugin. For realtime models, attach a TTS plugin or use
generate_reply()
if unavailable. • Speech Handles: Both methods return
SpeechHandle
objects which you can use to control or await playback as needed. • Error Handling: Calling these methods when the session is closing or not running raises a
RuntimeError
. • Interruption: If using a RealtimeModel with server-side turn detection,
allow_interruptions
cannot be
False
with
VoiceAgent.say()
. This will be adjusted automatically. Example pattern:
Copy code
@function_tool
async def my_function(self, context: RunContext):
    await self.session.say("Processing your request...")
    self.session.generate_reply(instructions="Instruct the user...")
    return "Done"
These methods enable rich voice feedback and dynamic dialogue within your agent's tools. Sources: Agent speech and audio | LiveKit Docs | tests/test_agent_session.py