Upgrade OpenAI SDK and migrate API routes to new Responses/Images API#85
Upgrade OpenAI SDK and migrate API routes to new Responses/Images API#85yisding wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Upgrades the project to the OpenAI Node SDK v6 and migrates the existing Next.js API routes from legacy Chat Completions / image endpoints to the newer Responses and Images APIs, including structured JSON outputs and a configurable OPENAI_MODEL fallback.
Changes:
- Bump
openaifrom^3.2.1to^6.39.0and update lockfile accordingly. - Migrate word-related API routes to
openai.responses.create()withtext.formatJSON schema structured outputs. - Migrate the image API route to
openai.images.generate().
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| pages/api/words.ts | Uses Responses API with JSON schema to return a word list; adds OPENAI_MODEL fallback. |
| pages/api/wordpieces.ts | Uses Responses API with JSON schema to return phoneme/character splits; adds OPENAI_MODEL fallback. |
| pages/api/wordgrade.ts | Uses Responses API with JSON schema to return a grade object; adds OPENAI_MODEL fallback. |
| pages/api/image.ts | Switches to Images API images.generate() and updates response parsing. |
| package.json | Updates OpenAI SDK dependency to v6. |
| package-lock.json | Lockfile updates reflecting the SDK upgrade and dependency graph changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Word: pig | ||
| Output: [ { "phonemes": "p", "characters": "p" }, { "phonemes": "ɪ", characters: "i" }, { "phonemes": "g", "characters": "g" } ] | ||
|
|
||
| Word: blue | ||
| Output: [ { "phonemes": "b", "characters": "b" }, { "phonemes": "l", characters: "l" }, { "phonemes": "u:", "characters": "ue" } ] |
|
|
||
| Word: happy | ||
| Output: [ { "phonemes": "h", "characters": "h" }, { "phonemes": "æ", characters: "a" }, { "phonemes": "p", "characters": "pp" },{ "phonemes": "i", "characters": "y" } ]`, | ||
| }, | ||
| { | ||
| role: "user", | ||
| content: `Word: ${word} | ||
| Output:`, | ||
| }, | ||
| ]; | ||
| Output: [ { "phonemes": "h", "characters": "h" }, { "phonemes": "æ", characters: "a" }, { "phonemes": "p", "characters": "pp" },{ "phonemes": "i", "characters": "y" } ]`; |
| Output: ["purple", "sorry", ...] | ||
|
|
||
| Reading Grade Level: 3, | ||
| Output: ["balloon", "riding", ...] | ||
|
|
||
| Reading Grade Level: 11 | ||
| Output: ["undulate", "articulate", ...] |
| }); | ||
|
|
||
| res.status(200).json({ url: response.data.data[0].url }); | ||
| res.status(200).json({ url: response.data?.[0]?.url }); |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 443c4ead6b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| model: "gpt-image-1", | ||
| prompt: `clip art of ${word}`, | ||
| n: 1, | ||
| size: "256x256", |
There was a problem hiding this comment.
Use a supported size for
gpt-image-1 generation
This route now calls openai.images.generate with model: "gpt-image-1" but still sends size: "256x256"; GPT image models only accept larger sizes (for example 1024x1024/1024x1536/1536x1024), so requests can fail validation at runtime instead of returning an image. In practice this can make /api/image fail for every POST request after this migration.
Useful? React with 👍 / 👎.
| }); | ||
|
|
||
| res.status(200).json({ url: response.data.data[0].url }); | ||
| res.status(200).json({ url: response.data?.[0]?.url }); |
There was a problem hiding this comment.
Read image bytes instead of unsupported
url field
For gpt-image-1, the Images API returns b64_json image data and does not provide a url field, but this handler responds with response.data?.[0]?.url. That means successful generations still return { "url": undefined }, which breaks downstream clients expecting a usable image payload.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Blocking feedback
gpt-image-1is wired with an incompatible request/response contract here:size: "256x256"is not supported for GPT image models, and this handler readsresponse.data?.[0]?.urleven thoughgpt-image-1returns base64 image data instead of URLs. This can make/api/imagefail or return no usable image URL — .../api/image.ts#L27-L30.
If you want me to apply a fix, reply with please fix 1.
| model: "gpt-image-1", | ||
| prompt: `clip art of ${word}`, | ||
| n: 1, | ||
| size: "256x256", |
There was a problem hiding this comment.
gpt-image-1 doesn't match this endpoint's assumptions. In openai-node v6.39, GPT image models support 1024x1024/1536x1024/1024x1536 sizes and return b64_json output, while URL output is unsupported for GPT image models (see src/resources/images.ts: https://github.com/openai/openai-node/blob/v6.39.0/src/resources/images.ts).
With size: "256x256" and response.data?.[0]?.url, this route can either fail the image call or return an empty URL.
Suggested fix: either (a) switch to a DALL·E model if you want URL output, or (b) keep gpt-image-1, read b64_json, and return a usable image source (for example a data:image/png;base64,... string or a stored-file URL).
Motivation
OPENAI_MODELto allow switching models without code changes.Description
openaidependency inpackage.jsonand updatedpackage-lock.jsonto^6.39.0, which also removes older transitive dependencies tied to the legacy client.OpenAIApi/Configurationimport with the newOpenAIclient and initialized it withapiKey: process.env.OPENAI_API_KEYin all API routes (image.ts,wordgrade.ts,wordpieces.ts,words.ts).openai.images.generatewithmodel: "gpt-image-1"inimage.ts, andopenai.responses.createwithinstructions,input,max_output_tokensandtext.formatJSON schema options for structured JSON responses inwordgrade.ts,wordpieces.ts, andwords.ts.response.output_textandresponse.data?.[0]?.url) and added amodelfallback viaprocess.env.OPENAI_MODEL ?? "gpt-5.5"where appropriate.Testing
npm ci/npm installandnext build) to validate TypeScript compilation and ensure the updated client usage does not break the build, and the build completed successfully.Codex Task