Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AI QL Tuui Tool Call Merge Strategy

From Leeroopedia
Knowledge Sources
Domains LLM, Streaming, Tool_Use
Last Updated 2026-10-24 20:38 GMT

Overview

Merge streaming tool-call fragments by matching on ID, skipping null values, and concatenating existing non-empty properties while overwriting empty ones.

Description

When an LLM streams a response with tool calls, each SSE delta may contain a partial tool call — a fragment of the function name, a fragment of the arguments JSON, or a null ID indicating continuation of the previous tool call. The merge strategy determines whether an incoming tool call belongs to the last one in the accumulator: if the previous exists and the incoming ID is null or matches, it's a continuation (merge). Otherwise, it's a new tool call (append). During merge, null values are skipped (don't overwrite existing data), non-empty existing values are concatenated, and empty values are overwritten. New tool calls get a default '{}' arguments string if null.

Usage

Apply this heuristic when reassembling tool calls from streamed LLM responses. The symptom is truncated function names or malformed JSON arguments in tool calls, or duplicate tool calls when fragments should have been merged.

The Insight (Rule of Thumb)

  • Action: For each incoming tool, check if it belongs to the last tool call. If yes, merge properties; if no, append as new.
  • Value:
    • Merge condition: lastTool exists AND (tool.id is null OR tool.id === lastTool.id)
    • Null values: skip (never overwrite existing with null)
    • Existing non-empty: concatenate (targetFunc[key] += value)
    • Existing empty: overwrite (targetFunc[key] = value)
    • New tool: default arguments = '{}' if null
  • Trade-off: Concatenation assumes the API sends fragments in order; out-of-order fragments would produce corrupted strings.

Reasoning

OpenAI-compatible streaming APIs send tool call data incrementally. The id field appears only in the first chunk of a tool call; subsequent fragments omit it (null). The function.name arrives first and is complete in the first chunk; function.arguments arrives as a growing JSON string across many chunks. Skipping null values prevents erasing the name when an arguments-only fragment arrives. Concatenation builds the arguments string incrementally. The default '{}' ensures the arguments field is always valid JSON even if the model sends no arguments.

Code Evidence

Tool call merge strategy from `src/renderer/composables/chatCompletions.ts:466-513`:

const parseTool = (tools: ToolCall[] | undefined, target: AssistantMessage) => {
  if (!tools) return
  if (!target.tool_calls) target.tool_calls = []

  tools.forEach((tool) => {
    const toolCalls = target.tool_calls!
    const lastTool = toolCalls[toolCalls.length - 1]
    const sourceFunc = tool.function

    // Case 1: Merge with last tool call
    if (lastTool && (!tool.id || lastTool.id === tool.id)) {
      const targetFunc = lastTool.function
      Object.keys(sourceFunc).forEach((key) => {
        const value = sourceFunc[key]
        if (value === null) return  // skip null
        if (targetFunc[key] && targetFunc[key] !== '{}') {
          targetFunc[key] += value  // concatenate
        } else {
          targetFunc[key] = value   // overwrite
        }
      })
    }
    // Case 2: Add as new tool call
    else {
      if (sourceFunc.arguments == null) {
        sourceFunc.arguments = '{}'  // default
      }
      toolCalls.push(tool)
    }
  })
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment