How can I convert the return value of an arbitrary...
# ask-ai
c
How can I convert the return value of an arbitrary function call into a tool output.
t
Thanks for reaching out, Victor. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to turn the output of any function call into a tool output in LiveKit, you can use the
make_tool_output
function. It’s designed to take the raw output from a function and wrap it up into a
ToolExecutionOutput
object. This object includes the function call itself, the output or any exceptions that popped up, plus some extra metadata to help with processing. Here’s a quick rundown of what
make_tool_output
does: • It gracefully handles any exceptions during the function execution and formats them nicely. • It checks that the output type is valid—like strings, numbers, booleans, lists, or dictionaries. • If your output includes
Agent
instances, it knows how to process those too, letting you return one agent task along with other valid outputs. • It converts the function output into a string so it fits perfectly into the
FunctionCallOutput
. And if you’re working with the JavaScript SDK, there’s a handy method called
ChatMessage.createToolFromFunctionResult
. This lets you create a
ChatMessage
directly from your function’s result, turning that output into a tool message that works great in chat contexts. Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | ChatMessage | LiveKit Agents
c
Can you show a code sample
t
Here’s a handy code sample showing how to use
make_tool_output
to turn a function call’s output into a
ToolExecutionOutput
object:
Copy code
from livekit.agents.llm import FunctionCall
from livekit.agents.voice.generation import make_tool_output

# Example function call from an LLM
fnc_call = FunctionCall(
    id="call_12345",
    call_id="tool_call_abc",
    name="lookup_weather",
    arguments='{"location": "San Francisco"}'
)

# Simulated function output
function_result = "It is currently 65°F and sunny in San Francisco."

# Create ToolExecutionOutput using make_tool_output
tool_output = make_tool_output(
    fnc_call=fnc_call,
    output=function_result,
    exception=None
)

# The resulting tool_output contains:
# - fnc_call: The original function call
# - fnc_call_out: FunctionCallOutput with output string and success status
# - raw_output: The original result
# - raw_exception: None (no error)
# - agent_task: None (no agent handoff)
# - reply_required: True (since output is non-empty)

print(f"Tool output: {tool_output.fnc_call_out.output}")
print(f"Error status: {tool_output.fnc_call_out.is_error}")
This example walks you through handling a function call, processing its output, and wrapping it up nicely in a
ToolExecutionOutput
object. If you want me to go over anything in more detail or need another example, just let me know—I’m here to help! Sources: livekit-agents/livekit/agents/voice/run_result.py
c
How would this integrate with the agent lifecycle? For example, Can this be yielded or returned from llm_node?
t
When it comes to integrating
make_tool_output
with the agent lifecycle in LiveKit, the framework takes care of it all behind the scenes. Whenever a tool call pops up in the LLM output, the framework smoothly handles it during the tool execution phase. This is where
make_tool_output
wraps up the results — including the function call details, the outcome, and any exceptions. As a developer, you won’t need to yield
make_tool_output
directly from the
llm_node
. Instead, you just focus on defining your tool functions, and the framework handles running them and managing the output. If you want to tweak how your agent behaves, you can always override the
llm_node
method in your custom
Agent
subclass. This lets you adjust the chat context or add your own logic before or after the usual LLM processing. Feel free to ask if you'd like me to walk you through any examples or clarify how to customize the `llm_node`—I’m here to help! Sources: pipeline-llm/replacing_llm_output.py | Pipeline nodes and hooks | LiveKit Docs | Voice pipeline nodes | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...