What's the best way to know if a function call has...
# ask-ai
c
What's the best way to know if a function call has happend within llm_node
t
Thanks for reaching out, Purna. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to detect when the LLM issues a function/tool call inside
llm_node
. The best approach is to inspect each streamed
ChatChunk
for a
delta.function_call
(or
delta.tool_calls
) field as the node yields chunks. Quick guidance: • In Python
llm_node
override:
Copy code
async for chunk in Agent.default.llm_node(...):
    if chunk.delta.function_call or (hasattr(chunk.delta, 'tool_calls') and chunk.delta.tool_calls):
        # function/tool call detected
    yield chunk
• In TypeScript
llmNode
override:
Copy code
const stream = await Agent.default.llmNode(...);
return new TransformStream({
  transform: (chunk, controller) => {
    if ('delta' in chunk && chunk.delta.functionCall) {
      // function/tool call detected
    }
    controller.enqueue(chunk);
  }
});
If you want, tell me which language/runtime you're using and whether you need example handling for partial/function-call streaming vs. final call payloads. Sources: Pipeline nodes and hooks | LiveKit docs | Tool definition and use | LiveKit docs | Agents v0.x migration guide - Node.js | LiveKit Docs
c
python
t
You want a Python example for detecting function/tool calls inside
llm_node
. Below is a minimal, practical pattern to inspect streamed
ChatChunk
deltas and handle function calls as they appear.
Copy code
async def llm_node(
    self,
    chat_ctx: llm.ChatContext,
    tools: list[llm.FunctionTool],
    model_settings: ModelSettings
) -> AsyncIterable[llm.ChatChunk]:
    async for chunk in Agent.default.llm_node(self, chat_ctx, tools, model_settings):
        delta = getattr(chunk, "delta", None)

        # Detect function call (or tool_calls)
        if delta:
            # full function call payload (sometimes appears when complete)
            if getattr(delta, "function_call", None):
                fc = delta.function_call
                # handle complete function call (name, arguments)
                print("Function call detected (complete):", fc)
                # e.g., call the tool or enqueue for later processing

            # streaming/partial function call pieces may arrive in `content` or tool_calls
            elif hasattr(delta, "tool_calls") and delta.tool_calls:
                print("Tool call(s) detected:", delta.tool_calls)

            # if function_call builds incrementally, you may see partial text pieces:
            elif getattr(delta, "content", None) and "function_call" in str(delta.content):
                # optional: accumulate partial function_call text until complete
                pass

        yield chunk
Notes: • Check both
delta.function_call
and
delta.tool_calls
— different model/tool integrations may use one or the other. • Function calls can arrive incrementally; if you need the full arguments, accumulate partial chunks until the
function_call
object is complete. • Replace
print
with your actual handler (invoke tool, enqueue, or emit an event). Would you like a short example that accumulates partial function-call arguments into a single payload?