Import error with OpenAI package: Cannot find 'Configuration' export

I’m working on a Next.js project and trying to connect to the GPT-3.5 API, but I’m running into some import errors. When attempting to import Configuration and OpenAIApi from the ‘openai’ library, I get warnings stating these members don’t exist. My console displays that Configuration is not a constructor and OpenAIApi seems to be missing as well. I’m using Clerk for authentication and currently setting up a POST endpoint for chat completions. Here’s my configuration:

import { Setup, OpenAIClient } from "openai";
import { getAuth } from "@clerk/nextjs";
import { NextResponse } from "next/server";

const clientSetup = new Setup({
    apiKey: process.env.OPENAI_API_KEY
});

const aiClient = new OpenAIClient(clientSetup);

export async function POST(request: Request) {
    try {
        const { userId } = getAuth();
        const requestData = await request.json();
        const { chatMessages } = requestData;

        if (!userId) {
            return new NextResponse("Unauthorized access", { status: 401 });
        }

        if (!clientSetup.apiKey) {
            return new NextResponse("API Key missing", { status: 500 });
        }

        if (!chatMessages) {
            return new NextResponse("Chat messages required", { status: 400 });
        }

        const result = await aiClient.generateChatResponse({
            model: "gpt-3.5-turbo",
            messages: chatMessages
        });

        return NextResponse.json(result.data.choices[0].message);

    } catch (err) {
        console.log("[CHAT_ERROR]", err);
        return new NextResponse("Server error", { status: 500 });
    }
}

My build fails with module not found errors. Has anyone else encountered this issue? What am I missing in my import statement?

Had this exact problem when transitioning a project - I was following docs from the wrong version. That import statement references classes that got completely removed in the v4.0 SDK update. What really tripped me up was my IDE’s autocomplete still showing those old exports even after updating the package. Double-check you’ve actually updated to the latest version and cleared your node_modules cache. The new syntax is way cleaner - just import OpenAI from 'openai' and initialize with new OpenAI({ apiKey: 'your-key' }). Your response structure’s also slightly different, so adjust your return statement to match the new completion object format.

I hit the same issue migrating old tutorial code. You’re using import names that don’t exist in the current OpenAI SDK. Those Setup and OpenAIClient classes aren’t in the official package - they look like examples from old docs or some different wrapper library. Make sure you’re installing the actual openai package, not a third-party wrapper. After fixing your imports, your API call should be await openai.chat.completions.create() with your model and messages. Also check your package.json - I’ve seen people accidentally install community packages with similar names.

Your import names don’t match the current OpenAI package structure. I encountered this exact issue last month when upgrading my project. You need to import OpenAI directly as the default export - Configuration and OpenAIApi have been deprecated in newer versions. Here’s what your imports should look like:

import OpenAI from 'openai';

Then initialize the client directly:

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY
});

For API calls, use openai.chat.completions.create() instead of the methods you’re trying to access. Ensure you install the latest version with npm install openai@latest as there were major changes around version 4.0 that removed those legacy exports you’re referencing.

You’re mixing syntax from different versions. Those class names don’t exist in the current OpenAI package - it got restructured big time. I ran into the same thing following old tutorials that used the pre-v4 API structure. Your code looks like it’s from those outdated examples. Here’s what it should look like now:

import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

const completion = await openai.chat.completions.create({
  model: "gpt-3.5-turbo",
  messages: chatMessages,
});

Check your package.json - make sure you’ve got the official openai package, not some wrapper or old fork.

you’ve got the wrong import names - those classes aren’t in the openai lib. i ran into the same mess upgrading from v3 to v4. use import OpenAI from 'openai' then new OpenAI({ apiKey: process.env.OPENAI_API_KEY }). no separate config object needed anymore, much cleaner.