glamorous-oil-12326
12/29/2025, 11:26 PMtall-airline-16756
12/29/2025, 11:26 PMtall-airline-16756
12/29/2025, 11:26 PMgenerate_reply / generateReply. Which runtime do you need to run this in (server-side Node, browser, or Python backend)? If either is fine, I’ll provide both full examples.
Sources: Tool definition and use | LiveKit docs | External data and RAG | LiveKit docsglamorous-oil-12326
12/29/2025, 11:26 PMtall-airline-16756
12/29/2025, 11:26 PMcontext.session.generate_reply() (as in LiveKit agent tooling). It shows the agent speaking before the work, during a simulated long-running step, and after completion.
# Example: agent speaking inside a tool call (Python)
# Assumes LiveKit-style agent framework with RunContext and function_tool decorator
from livekit.agents import Agent, function_tool, RunContext
import asyncio
class Assistant(Agent):
@function_tool()
async def lookup_weather(self, context: RunContext, location: str) -> str:
# Agent provides an immediate spoken status update
await context.session.generate_reply(
instructions=f"Checking the weather for {location} now."
)
# Simulate a long-running operation and give an interim update
await asyncio.sleep(1) # simulate network/API delay
await context.session.generate_reply(
instructions="Still checking—contacting the weather service..."
)
# Simulate obtaining result (replace with real API call)
await asyncio.sleep(1)
weather_result = f"The weather in {location} is sunny with a temperature of 75°F."
# Agent speaks the final result
await context.session.generate_reply(
instructions=f"I found the forecast: {weather_result}"
)
# Return result to the LLM / caller (optional)
return weather_result
# Example usage (framework will invoke the tool):
# assistant = Assistant()
# assistant.lookup_weather(context, "San Francisco")
If you want, I can:
• Replace the simulated delay with a real API call example (e.g., OpenWeatherMap).
• Show how to emit speech with different voices or SSML if your session supports it.
Which would you prefer?glamorous-oil-12326
12/29/2025, 11:27 PMtall-airline-16756
12/29/2025, 11:27 PMcontext.session.generate_reply().
from livekit.agents import Agent, function_tool, RunContext
import asyncio
class Assistant(Agent):
@function_tool()
async def example_tool(self, context: RunContext, query: str) -> str:
# Agent speaks before starting the tool action
await context.session.generate_reply(instructions=f"Starting lookup for: {query}")
# Simulate work and give an interim spoken update
await asyncio.sleep(1)
await context.session.generate_reply(instructions="Working on it... one moment please.")
# Simulate final result
await asyncio.sleep(1)
result = f"Result for '{query}': success."
# Agent speaks the final result
await context.session.generate_reply(instructions=f"Done. {result}")
return result
If you want this tied to a real API call or need SSML/voice selection examples, tell me which and I’ll add it.glamorous-oil-12326
12/29/2025, 11:39 PMtall-airline-16756
12/29/2025, 11:39 PMcontext.session.say inside a function/tool call.
Why:
• The tool's RunContext (context) is scoped to the current execution and ensures the session actions run in the correct runtime.
• self.session.say can be ambiguous or incorrect in tool contexts (especially with dynamic/shared tools).
Example:
async def example_tool(self, context: RunContext, query: str) -> str:
await context.session.say("Starting lookup...")
# do work...
await context.session.say("Done.")
return "result"
If you want, I can show the same example using generate_reply or include voice/SSML options. Which would you prefer?
Sources: Tool definition and use | LiveKit docs