-
Notifications
You must be signed in to change notification settings - Fork 769
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
Comments
Hey Sashank! Nice username. Yeah I agree this is not great. You need an 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"),
] |
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.
then use it like -
|
Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
When using
openai-node
with Typescript, there is a confusing type error when calling thechat.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, includingChatCompletionFunctionMessageParam
. When creating completion messages with a user role or system role (usingChatCompletionSystemMessageParam
orChatCompletionUserMessageParam
), TS matches the parameters withChatCompletionFunctionMessageParam
because they share common properties. This results in a type error because name is required inChatCompletionFunctionMessageParam
, but it's optional or missing in system or user message types.Here is a TS Playground reproduction:
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
The text was updated successfully, but these errors were encountered: