Error in function calling in node.js Examples

Hello,

The example provided for (Complete tool calling example) under javascript has errors in this link https://platform.openai.com/docs/guides/function-calling

This is the correct code

```
// 1. Define a list of callable tools for the model

const tools = [

{

type: “function”,

name: “get_horoscope”,

description: “Get today’s horoscope for an astrological sign.”,

parameters: {

type: “object”,

properties: {

sign: {

type: “string”,

description: “An astrological sign like Taurus or Aquarius”,

      },

    },

required: [“sign”],

  },

},

];

function get_horoscope(sign) {

return sign + " Next Tuesday you will befriend a baby otter.";

}

// Create a running input list we will add to over time

let input = [

{ role: "user", content: "What is my horoscope? I am an Aquarius." },

];

// 2. Prompt the model with tools defined

let response = await openai.responses.create({

model: “gpt-5”,

tools,

input,

});

console.log(response.output)

input.push(…response.output)

response.output.forEach((item) => {

if (item.type == “function_call”) {

if (item.name == “get_horoscope”){

console.log(JSON.parse(item.arguments).sign)

// 3. Execute the function logic for get_horoscope

const horoscope = get_horoscope(JSON.parse(item.arguments).sign)

// 4. Provide function call results to the model

input.push({

type: “function_call_output”,

call_id: item.call_id,

output: horoscope

    })

 }

}

});

console.log(“Final input:”);

console.log(JSON.stringify(input, null, 2));

response = await openai.responses.create({

model: “gpt-5”,

instructions: “Respond only with a horoscope generated by a tool.”,

tools,

input,

});

// 5. The model should be able to give a response!

console.log(“Final output:”);

console.log(JSON.stringify(response.output, null, 2));
```

2 Likes

Wished I found this before I spend time looking for the problem. I noticed the difference between the javascript and the python example.

@guptaa.pavan did a wonderful job providing the working code for the complete example, Here is a summarization of what was wrong.

So, what missing is, after line 37, to add the models output to the chat history:

input.push(…response.output);

Also, at line 46 of the example at the doc input_list must be changed to

input

And finally the function name at line 23 should be

get_horoscope

Glad that was helped you. I spent almost 3 hrs and found the issue.

Thank you for flagging this issue. We truly apologies for the inconvenience this caused. Our team will improve the documentation with your feedback. Thank you so much!

1 Like