openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

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

Open helloworld opened this issue 1 year ago • 2 comments

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

  • [X] 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

helloworld avatar Jan 20 '24 00:01 helloworld

Hey Sashank! Nice username. Yeah I agree this is not great. You need an as const so that TS knows not to widen role: "user" to the type role: string. For example:

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

or

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

Unfortunately, I don't think there's a way to faithfully represent the reality of how the API works in TypeScript without exposing this footgun, but we might be missing something.

We've been wanting to export some convenient helper functions to construct typed messages, along the lines of this (bikeshedding welcome):

const messages = [
  openai.UserMessage("world"),
]

rattrayalex avatar Jan 20 '24 02:01 rattrayalex

One more way to solve it is to add an enum for Role. Especially if you want to add some other messages with a different role.

enum Role {
  User = "user",
  System = "system",
}

then use it like -

const messages = [
  {
    role: Role.User,
    content: "world",
  },
];

varungarg6781 avatar Mar 13 '24 23:03 varungarg6781