You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
importOpenAIfrom"openai";constopenai=newOpenAI();constmessages=[{role: "user",content: "world",},];// This failsawaitopenai.chat.completions.create({model: "gpt-4",messages: messages,});
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:
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):
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: