wooden-beard-26644
08/18/2025, 5:51 PMtall-airline-16756
08/18/2025, 5:51 PMtall-airline-16756
08/18/2025, 5:51 PMdelta
object to see if it has any tool_calls
. That’s your sign a tool call is happening.
2. Build a Response for Tool Calls: If you spot tool calls, create a response object (like ChatChunk
) that only includes the tool call info. For example, set the tool_calls
array inside the delta
object.
3. Return Tool Calls: Send back the response with just the tool call details—no extra text included.
4. Handle Text Content Separately: If there aren’t any tool calls, go ahead and process the text (maybe clean up any unnecessary tokens) and return it as its own response.
Here’s a quick example to make it clearer:
if delta.tool_calls:
for tool in delta.tool_calls:
# Handle each tool call and construct the response
if tool.function.name:
call_chunk = llm.ChatChunk(
id=id,
delta=llm.ChoiceDelta(
role="assistant",
tool_calls=[
llm.FunctionToolCall(
arguments=self._fnc_raw_arguments or "",
name=self._fnc_name or "",
call_id=self._tool_call_id or "",
)
],
),
)
self._tool_call_id = self._fnc_name = self._fnc_raw_arguments = None
return call_chunk
else:
# Process and return the text content
delta.content = llm_utils.strip_thinking_tokens(delta.content, thinking)
if not delta.content:
return None
return llm.ChatChunk(
id=id,
delta=llm.ChoiceDelta(content=delta.content, role="assistant"),
)
This way, tool calls get the spotlight without any extra text cluttering things up. If anything’s unclear or you want to dive deeper, just let me know—I’m here to help!
Sources: livekit-agents/livekit/agents/voice/agent_activity.py