Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
helloworld opened this issue Jan 20, 2024 · 2 comments
Open
1 task done

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

helloworld opened this issue Jan 20, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@helloworld
Copy link

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

@helloworld helloworld added the bug Something isn't working label Jan 20, 2024
@rattrayalex
Copy link
Collaborator

rattrayalex commented Jan 20, 2024

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 rattrayalex added enhancement New feature or request and removed bug Something isn't working labels Jan 20, 2024
@varungarg6781
Copy link

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",
  },
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants