Skip to content

Upgrade OpenAI SDK and migrate API routes to new Responses/Images API#85

Open
yisding wants to merge 1 commit into
mainfrom
codex/modernize-application-codebase-with-gpt-5.5
Open

Upgrade OpenAI SDK and migrate API routes to new Responses/Images API#85
yisding wants to merge 1 commit into
mainfrom
codex/modernize-application-codebase-with-gpt-5.5

Conversation

@yisding
Copy link
Copy Markdown
Owner

@yisding yisding commented May 24, 2026

Motivation

  • Upgrade the project to the latest OpenAI Node SDK and migrate existing API routes to the new Responses and Images endpoints for improved compatibility and structured output support.
  • Remove legacy usage patterns that relied on the older OpenAI client and chat completion endpoints to adopt the newer client interface and model naming.
  • Use a configurable model fallback via OPENAI_MODEL to allow switching models without code changes.

Description

  • Bumped openai dependency in package.json and updated package-lock.json to ^6.39.0, which also removes older transitive dependencies tied to the legacy client.
  • Replaced old OpenAIApi/Configuration import with the new OpenAI client and initialized it with apiKey: process.env.OPENAI_API_KEY in all API routes (image.ts, wordgrade.ts, wordpieces.ts, words.ts).
  • Migrated calls to the new API shape: use openai.images.generate with model: "gpt-image-1" in image.ts, and openai.responses.create with instructions, input, max_output_tokens and text.format JSON schema options for structured JSON responses in wordgrade.ts, wordpieces.ts, and words.ts.
  • Updated response parsing to use the new response fields (e.g. response.output_text and response.data?.[0]?.url) and added a model fallback via process.env.OPENAI_MODEL ?? "gpt-5.5" where appropriate.

Testing

  • No new automated unit tests were added or changed in this PR.
  • Ran a local install and build (npm ci / npm install and next build) to validate TypeScript compilation and ensure the updated client usage does not break the build, and the build completed successfully.

Codex Task

@yisding yisding marked this pull request as ready for review May 24, 2026 18:55
@yisding yisding requested a review from Copilot May 24, 2026 18:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 openai from ^3.2.1 to ^6.39.0 and update lockfile accordingly.
  • Migrate word-related API routes to openai.responses.create() with text.format JSON 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.

Comment thread pages/api/wordpieces.ts
Comment on lines +36 to 40
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" } ]
Comment thread pages/api/wordpieces.ts
Comment on lines 41 to +43

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" } ]`;
Comment thread pages/api/words.ts
Comment on lines 74 to 80
Output: ["purple", "sorry", ...]

Reading Grade Level: 3,
Output: ["balloon", "riding", ...]

Reading Grade Level: 11
Output: ["undulate", "articulate", ...]
Comment thread pages/api/image.ts
});

res.status(200).json({ url: response.data.data[0].url });
res.status(200).json({ url: response.data?.[0]?.url });
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread pages/api/image.ts
Comment on lines +25 to 27
model: "gpt-image-1",
prompt: `clip art of ${word}`,
n: 1,
size: "256x256",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread pages/api/image.ts
});

res.status(200).json({ url: response.data.data[0].url });
res.status(200).json({ url: response.data?.[0]?.url });
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown

@charliecreates charliecreates Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking feedback

  1. gpt-image-1 is wired with an incompatible request/response contract here: size: "256x256" is not supported for GPT image models, and this handler reads response.data?.[0]?.url even though gpt-image-1 returns base64 image data instead of URLs. This can make /api/image fail or return no usable image URL — .../api/image.ts#L27-L30.

If you want me to apply a fix, reply with please fix 1.

Comment thread pages/api/image.ts
model: "gpt-image-1",
prompt: `clip art of ${word}`,
n: 1,
size: "256x256",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants