I'm trying to create a tool with a json object in ...
# support
i
I asked to vapis support: can you receive from tool a json object Yes, your tool can return JSON objects. When your server responds to a tool call, it needs to follow this specific format 1: json { "results": [ { "toolCallId": "X", "result": "Y" } ] } A few important points: The response must include a toolCallId that matches the original request The result field can contain your JSON data Make sure async is set to false for the tool to properly receive the response 2 For example, if you're building a weather tool, your response might look like this 1: json { "results": [ { "toolCallId": "call_VaJOd8ZeZgWCEHDYomyCPfwN", "result": "San Francisco's weather today is 62°C, partly cloudy." } ] } But i do not know if this is real or only string can be passed as result, if you can response json data. How can I make vapi to understand json data?, because error response is fired
s
@Ikerman You're trying to return a JSON object from your tool, but you're encountering an error because of formatting issues in your response. ## What's Happening This is not a Vapi issue but relates to how you're formatting your response. Vapi expects the
result
field to be a properly formatted string without line breaks. ## Solution Follow this format exactly when responding from your tool:
Copy code
json
{
  "results": [
    {
      "toolCallId": "call_VaJOd8ZeZgWCEHDYomyCPfwN",
      "result": "{\"temperature\":62,\"condition\":\"partly cloudy\"}"
    }
  ]
}
The key points to remember: - Use
JSON.stringify()
on your JSON data for the
result
field - Ensure there are no line breaks in the stringified JSON - Make sure the
toolCallId
matches the original request ID - Set
async: false
in your tool configuration ## Step-by-Step Implementation 1. Correctly format your server response:
Copy code
javascript
   app.post('/api/tools/weather', (req, res) => {
     const { toolCallId, arguments: args } = req.body;
     const parsedArgs = JSON.parse(args);
     
     // Create your data object
     const weatherData = {
       temperature: 62,
       condition: "partly cloudy",
       location: parsedArgs.location
     };
     
     // Return the response with properly stringified JSON
     res.json({
       results: [
         {
           toolCallId: toolCallId,
           result: JSON.stringify(weatherData) // This creates a single-line string
         }
       ]
     });
   });
2. Example of correct response format:
Copy code
json
   {
     "results": [
       {
         "toolCallId": "call_ABC123",
         "result": "{\"data\":{\"key1\":\"value1\",\"key2\":\"value2\"}}"
       }
     ]
   }
This approach ensures that your tool correctly sends JSON data that Vapi can properly process, while maintaining compatibility with the API requirements.