Skip to content

JavaScript function-calling code snippet contains syntax errors in the docs #1711

@danpeleg4

Description

@danpeleg4

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

The JavaScript code snippet on Function Calling guide
contains multiple errors that prevent it from running correctly:

Invalid JS syntax:

if (item.name == "get_horoscope"):

The colon (:) is Python-style and should be replaced with { ... }.

Mismatched function name:
The snippet calls get_horoscope(...), but the function is defined as getHoroscope.

Undefined variable:
The snippet uses input_list.push(...), but input_list is never defined. It should use the input array.

Python code in JS:
The snippet uses json.dumps(...), which is Python. In JS, it should use JSON.stringify(...).

Incorrect tool output format:
The snippet uses type: "function_call_output" and call_id, which do not match the OpenAI Responses API expected format.

To Reproduce

The URL of the page: https://platform.openai.com/docs/guides/function-calling

Code snippets

import OpenAI from "openai";
const openai = new OpenAI();

// 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 getHoroscope(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,
});

response.output.forEach((item) => {
  if (item.type == "function_call") {
    if (item.name == "get_horoscope"):
      // 3. Execute the function logic for get_horoscope
      const horoscope = get_horoscope(JSON.parse(item.arguments))
      
      // 4. Provide function call results to the model
      input_list.push({
          type: "function_call_output",
          call_id: item.call_id,
          output: json.dumps({
            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));

OS

Web

Node version

Node V20

Library version

openai v6.9.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions