Possible to send a tool response from the client?
# support
t
I use the response stream to detect when a tool is called and take action on the client side. I'd also like to be able to send the tool's response from the client, but I can't do it. The most I've succeeded in doing is this, but it's impossible to give it the toolCallId, and the name somewhere so that it uses it properly because the voice assistant keeps saying that it hasn't managed to retrieve the information. Messages from vapi dashboard :
Copy code
{
  "role": "tool_call_result",
  "time": 1748306984938,
  "result": "Here is the result of the tool call: {\"template\":\"bold\",\"fontFamily\":\"Montserrat\",\"fontWeight\":\"800\",\"fontSize\":60,\"color\":\"#ffffff\",\"isItalic\":false,\"isPunctuation\":true,\"isUppercase\":true,\"mode\":\"twoLines\",\"position\":50,\"animation\":{\"appear\":\"bounce\"},\"activeWord\":{\"isActive\":true,\"fontFamily\":\"Montserrat\",\"fontWeight\":\"800\",\"fontSize\":62,\"color\":\"#bbbbbb\",\"isItalic\":false,\"isPunctuation\":true,\"isUppercase\":true},\"shadow\":{\"isActive\":true,\"size\":3,\"color\":\"#000000\"}}",
  "secondsFromStart": 13.552
}
{
  "name": "getCurrentSubtitleStyle",
  "role": "tool_call_result",
  "time": 1748306985490,
  "result": "No result returned.",
  "toolCallId": "call_wQxdakrO15NNScvqwngijnCu",
  "secondsFromStart": 10.06
}
`
code:
Copy code
if (message.type === "tool-calls" && props?.onToolCall && Array.isArray(message.toolCalls)) {
  message.toolCalls.forEach(async (toolCall: any) => {
    const result = await props.onToolCall?.(toolCall);
    if (vapiInstance) {
      vapiInstance.send({
        type: "add-message",
        message: {
          role: "tool",
          content: "Here is the result of the tool call: " + result
        }
      });

    }
  });
}
Is there a way to do this? Because I'm almost done, but the reponse tool seems to be possible only on the server side, but I'm obliged to use it on the client side for these data and actions.
k
Use the role: "tool_call_result" and include both toolCallId and name in the message, so the assistant can correctly match the response to the original tool call.
v
Currently, Vapi only supports sending tool responses from the server side, not directly from the client. To properly resolve a tool call, your server should respond with a JSON object like this:
Copy code
json
{
  "results": [
    {
      "toolCallId": "call_wQxdakrO15NNScvqwngijnCu",
      "result": "Your result here"
    }
  ]
}
There is no documented way to send a tool response with
toolCallId
from the client that the assistant will process as a tool result. [See: Vapi Custom Tools Guide](https://docs.vapi.ai/tools/custom-tools) Sources: - [Vapi Custom Tools Guide](https://docs.vapi.ai/tools/custom-tools) - [Vapi Web SDK](https://docs.vapi.ai/sdk/web)
t
It doesn't work, I tried with code and here's the error I get:
Copy code
vapiInstance.send({
  type: "add-message",
  message: {
    role: "tool_call_result",
    toolCallId: toolCall.id || toolCall.toolCallId,
    name: toolCall.function?.name || toolCall.name,
    content: result
  }
});
Copy code
incoming-webhook failed validation. Errors: [ "message.property toolCallId should not exist", "message.property name should not exist", "message.role must be one of the following values: assistant, function, user, system, tool" ]
I have the impression that on the client side I can't put the toolCallId and the name, which is a pity. Could I technically do this by calling the vapi api on the server side, perhaps?
v
You cannot send a tool response (with
toolCallId
) directly from the client in Vapi.
To resolve a tool call, you must send the result from your server using the following format:
Copy code
json
{
  "results": [
    {
      "toolCallId": "call_wQxdakrO15NNScvqwngijnCu",
      "result": "Your result here"
    }
  ]
}
If you need to process the tool on the client, send the result to your server first, then have your server forward it to Vapi in this format. [See: Vapi Custom Tools Guide](https://docs.vapi.ai/tools/custom-tools) Sources: - [Vapi Custom Tools Guide](https://docs.vapi.ai/tools/custom-tools) - [Vapi Web SDK](https://docs.vapi.ai/sdk/web)
s
Nope, it is not.
v
You cannot send a tool response with
toolCallId
directly from the client in Vapi.
To resolve a tool call, send the result from your server using this format:
Copy code
json
{
  "results": [
    {
      "toolCallId": "call_wQxdakrO15NNScvqwngijnCu",
      "result": "Your result here"
    }
  ]
}
If you process the tool on the client, forward the result to your server, then have your server send it to Vapi in this format. [See: Vapi Custom Tools Guide](https://docs.vapi.ai/tools/custom-tools) Sources: - [Vapi Custom Tools Guide](https://docs.vapi.ai/tools/custom-tools) - [Vapi Web SDK](https://docs.vapi.ai/sdk/web)