Can't build NextJS project with openai library. Getting: Type error: Private identifiers are only available when targeting ECMAScript 2015 and higher.
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
Getting this kind of error on build time on NextJS 14 and I don't know why
this is my tsconfig.json
{
"compilerOptions": {
"target": "ES2023",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"exclude": ["node_modules"]
}
Thanks for help.
To Reproduce
- Install and use library on nextjs
- import something like
import typoe { Message } from 'openai/resources/beta/threads/messages';
Code snippets
No response
OS
macOS
Node version
v22.3.0
Library version
4.52.3
updates:
if I remove every import from 'openai/resources/beta/****'; all works
but the question is about using proper types. Any ideas?
Hi @giacomoalonzi can you provide a simplified reproduction I can checkout and run locally?
My assumption is this an issue with either the project configuration or Next.js' automatic dependency bundling feature.
A reproduction will be helpful for me to dig deeper. Thanks!
did u ended up finding out the solution
updates: if I remove every import
from 'openai/resources/beta/****';all works but the question is about using proper types. Any ideas?
The same issue with you, I copied that interface and types to my code then remove that interface and type imported from 'openai/resources/xxx' then I can build now. But I think it's not the best way to fix it.
I had the same issue and was able to reproduce this with.a fresh project using the example in the readme as index.ts
OS: OSX Sonoma 14.5 Node: v22.5.1 OpenAI Library: 4.53.0
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env['OPENAI_API_KEY'], // This is the default and can be omitted
});
async function main() {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: 'user', content: 'Say this is a test' }],
model: 'gpt-3.5-turbo',
});
}
main();
The error appeared when running npx tsc index.ts I reran as npx tsc and it complied the project after a minor modification.
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
async function main(): Promise<void> {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: "user", content: "Say this is a test" }],
model: "gpt-4o-mini",
});
// Extract and log the message content with a type guard
const messageContent = chatCompletion.choices[0].message.content;
if (messageContent !== null) {
console.log(messageContent);
} else {
console.error("Message content is null");
}
}
main();
My understanding from the tsc readme is that passing a file will skip the tsconfig.ts which was generating the issue for me.
I reproduce similar issue by importing
openai/src/resources/audio/speech
just remove the src may able build right now
openai/resources/audio/speech
I suspect the original report here is because of an openai/src import somewhere.
If anyone can reproduce this issue without an openai/src import, please open a new issue with a minimal reproduction so we can investigate!