Skip to content

Commit 09d5cf7

Browse files
Copilotpelikhan
andauthored
Fix generateImage edit mode content type error by using global.fetch for FormData (#1914)
* Initial plan * Fix generateImage edit mode content type error by using global.fetch for FormData Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> * genai: /docs [skip ci] --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com> Co-authored-by: Peli de Halleux <pelikhan@users.noreply.github.com>
1 parent c3ea379 commit 09d5cf7

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/core/src/openai.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ export async function OpenAIImageGeneration(
445445

446446
traceFetchPost(trace, url, freq.headers, body);
447447

448-
const res = await fetch(url, freq as any);
448+
// TODO: switch back to cross-fetch in the future
449+
const res = isMultipart ? await global.fetch(url, freq as any) : await fetch(url, freq as any);
449450
dbg(`response: %d %s`, res.status, res.statusText);
450451
trace?.itemValue(`status`, `${res.status} ${res.statusText}`);
451452
if (!res.ok)

packages/core/test/image-generation.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, test, expect, beforeAll } from "vitest";
1+
import { describe, test, expect, beforeAll, vi } from "vitest";
22
import { writeFileSync } from "fs";
33
import { join } from "path";
44
import { CreateImageRequest } from "../src/chat.js";
@@ -216,6 +216,8 @@ describe("Image Generation", () => {
216216
model: "dall-e-3",
217217
base: "https://example.openai.azure.com",
218218
type: "azure",
219+
token: "test-token",
220+
modelId: "dall-e-3",
219221
};
220222

221223
// This test verifies the request structure is valid for Azure edit mode
@@ -237,6 +239,8 @@ describe("Image Generation", () => {
237239
model: "dall-e-3",
238240
base: "https://example.openai.azure.com",
239241
type: "azure",
242+
token: "test-token",
243+
modelId: "dall-e-3",
240244
};
241245

242246
// This test verifies the request structure is valid for Azure generation mode
@@ -259,6 +263,8 @@ describe("Image Generation", () => {
259263
model: "dall-e-2",
260264
base: "https://api.openai.com/v1",
261265
type: "openai",
266+
token: "test-token",
267+
modelId: "dall-e-2",
262268
};
263269

264270
// This test verifies the request structure is valid for OpenAI edit mode
@@ -267,4 +273,54 @@ describe("Image Generation", () => {
267273
expect(cfg.provider).toBe(MODEL_PROVIDER_OPENAI);
268274
});
269275
});
276+
277+
describe("HTTP Request Handling", () => {
278+
test("edit mode request structure is correctly prepared for FormData", () => {
279+
const req: CreateImageRequest = {
280+
model: "dall-e-2",
281+
prompt: "Edit this image",
282+
mode: "edit",
283+
image: createTestImage(),
284+
};
285+
286+
const cfg: LanguageModelConfiguration = {
287+
provider: MODEL_PROVIDER_OPENAI,
288+
model: "dall-e-2",
289+
base: "https://api.openai.com/v1",
290+
type: "openai",
291+
token: "test-token",
292+
modelId: "dall-e-2",
293+
};
294+
295+
// Verify the request structure is correct for edit mode
296+
expect(req.mode).toBe("edit");
297+
expect(req.image).toBeInstanceOf(Buffer);
298+
expect(cfg.provider).toBe(MODEL_PROVIDER_OPENAI);
299+
300+
// The fix ensures that when mode === "edit" (isMultipart = true),
301+
// global.fetch is used instead of cross-fetch to properly handle FormData
302+
});
303+
304+
test("generate mode uses standard JSON body", () => {
305+
const req: CreateImageRequest = {
306+
model: "dall-e-3",
307+
prompt: "Generate a new image",
308+
mode: "generate",
309+
};
310+
311+
const cfg: LanguageModelConfiguration = {
312+
provider: MODEL_PROVIDER_OPENAI,
313+
model: "dall-e-3",
314+
base: "https://api.openai.com/v1",
315+
type: "openai",
316+
token: "test-token",
317+
modelId: "dall-e-3",
318+
};
319+
320+
// Verify structure for standard generation mode
321+
expect(req.mode).toBe("generate");
322+
expect(req.image).toBeUndefined();
323+
expect(cfg.provider).toBe(MODEL_PROVIDER_OPENAI);
324+
});
325+
});
270326
});

0 commit comments

Comments
 (0)