Skip to content

Confusing type error for openai.chat.completions.create() #639

Open
@helloworld

Description

@helloworld

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

When using openai-node with Typescript, there is a confusing type error when calling the chat.completions.create() method.

TypeScript fails to resolve the correct overload for the messages option due to ambiguity in the ChatCompletionMessageParam type.

ChatCompletionMessageParam is a union comprising several message types, including ChatCompletionFunctionMessageParam. When creating completion messages with a user role or system role (using ChatCompletionSystemMessageParam or ChatCompletionUserMessageParam), TS matches the parameters with ChatCompletionFunctionMessageParam because they share common properties. This results in a type error because name is required in ChatCompletionFunctionMessageParam, but it's optional or missing in system or user message types.

Here is a TS Playground reproduction:

import OpenAI from "openai";

const openai = new OpenAI();

const messages = [
  {
    role: "user",
    content: "world",
  },
];

// This fails
await openai.chat.completions.create({
  model: "gpt-4",
  messages: messages,
});

Reproduction link: Playground Link

It seems that most examples in the OpenAI documentation produce this type error when copy pasted into the playground.

To Reproduce

Playground Link

Code snippets

No response

OS

MacOS

Node version

v18.18.2

Library version

4.24.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions