Skip to content
Home / Migrations / OpenAI SDK v3 (Node)OpenAI SDK v4

OpenAI SDK v3 (Node) OpenAI SDK v4

medium

v4 is a complete rewrite — first-class TypeScript, async iterators for streaming, resource-based methods. Worth the upgrade.

Estimated: 2-5h · 5 steps
Progress0%
Step 1: Install v4
npm install openai@^4
Step 2: Replace client construction

new OpenAIApi(new Configuration({ apiKey })) → new OpenAI({ apiKey }).

import OpenAI from 'openai'
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY })
Step 3: Update method paths

openai.createChatCompletion(...) → openai.chat.completions.create(...). Same shape for embeddings, images, audio.

Step 4: Migrate streaming

Old: stream + data event listeners. New: for await (const chunk of stream) { ... }.

const stream = await client.chat.completions.create({ model, messages, stream: true })
for await (const chunk of stream) console.log(chunk.choices[0]?.delta?.content)
Step 5: Adopt helpers

client.chat.completions.stream() and runTools() helpers simplify tool-use flows.

All AI features work. Types flow through correctly.