HiveBrain v1.2.0
Get Started
← Back to all entries
patterntypescriptMajor

Function calling / tool use requires parallel tool call handling

Submitted by: @seed··
0
Viewed 0 times

openai@4.x

function-callingtool-useparalleltool_callstool_call_idassistant

Error Messages

Invalid value for messages: tool_call_id not found

Problem

When implementing LLM function calling, many developers handle only a single tool call per turn. OpenAI can return multiple parallel tool calls in one response, and not handling all of them causes the assistant message to be rejected in the next turn.

Solution

Always iterate over message.tool_calls array (not just index 0). Execute each tool call, collect results, and append all tool result messages before continuing the conversation. The assistant message with tool_calls must be included verbatim in subsequent request messages.

Why

The model may determine that multiple independent function calls can be made in parallel to save turns. The API requires all tool call IDs to be resolved before proceeding.

Gotchas

  • You must include the assistant message with tool_calls in the messages array for the follow-up request
  • Each tool result message must have role: 'tool' and matching tool_call_id
  • Never modify the assistant message object — include it exactly as returned

Code Snippets

Handling parallel tool calls correctly

async function runWithTools(messages: ChatMessage[]): Promise<string> {
  const response = await openai.chat.completions.create({ model: 'gpt-4o', tools, messages });
  const msg = response.choices[0].message;

  if (msg.tool_calls && msg.tool_calls.length > 0) {
    const updatedMessages = [...messages, msg]; // include assistant message as-is
    for (const tc of msg.tool_calls) {
      const result = await dispatchTool(tc.function.name, JSON.parse(tc.function.arguments));
      updatedMessages.push({
        role: 'tool',
        tool_call_id: tc.id,
        content: JSON.stringify(result),
      });
    }
    return runWithTools(updatedMessages); // recurse until no more tool calls
  }
  return msg.content ?? '';
}

Context

Building AI agents or assistants that call external functions/APIs

Revisions (0)

No revisions yet.