fix(images): reject non-PNG output_format on Codex GPT Image routes (#19252)

This commit is contained in:
diegosouzapw
2026-11-19 04:32:42 -03:00
parent cc59ad70b2
commit e17c636eef
5 changed files with 113 additions and 0 deletions

View File

@@ -0,0 +1 @@
- fix(images): Codex GPT Image routes now reject a non-PNG `output_format` with a clear 400 instead of silently returning PNG (#19252)

View File

@@ -385,6 +385,11 @@ reproducible. `n` is capped at 10. The request fails as a whole if any call fail
of fan-out formats is `SINGLE_IMAGE_FANOUT_FORMATS` in
`open-sse/handlers/imageGeneration/fanOut.ts`.
Codex GPT Image models (`codex/gpt-image-*`) return PNG only: the upstream answers `200` with
PNG bytes even when `jpeg` or `webp` is requested. A request with any other `output_format`
(`webp`, `jpeg`, ...) is rejected with HTTP 400 instead of being silently ignored; omit it or
send `png`. `output_compression` is not forwarded.
ZenMux reuses the existing API-key connection and accepts `zenmux/` or `zm/` prefixes:
- `zenmux/openai/gpt-image-2` uses ZenMux's OpenAI Images API. Options include `size`,

View File

@@ -22,6 +22,7 @@ import {
saveImageErrorResult,
saveImageSuccessResult,
} from "../../imageGeneration.ts";
import { checkCodexOutputFormat } from "./codexOutputFormat.ts";
type ImageLog = {
info: (tag: string, message: string) => void;
@@ -149,6 +150,17 @@ export async function handleCodexImagesApi({
path: logPath,
});
}
const formatCheck = checkCodexOutputFormat(body);
if (!formatCheck.ok) {
return saveImageErrorResult({
provider,
model,
status: 400,
startTime,
error: formatCheck.error,
path: logPath,
});
}
const isEdit = referenceImages.length > 0;
const url = codexImagesUrl(baseUrl, isEdit ? "edits" : "generations");
const prompt = String(body.prompt);
@@ -162,6 +174,7 @@ export async function handleCodexImagesApi({
if (typeof body.background === "string" && body.background.trim()) {
upstreamBody.background = body.background.trim();
}
if (formatCheck.forward) upstreamBody.output_format = formatCheck.forward;
if (isEdit) {
upstreamBody.images = referenceImages.map((image) => ({
image_url: `data:${image.mime || "image/png"};base64,${image.bytes.toString("base64")}`,

View File

@@ -0,0 +1,23 @@
// Codex GPT Image output_format contract (#19252).
// The ChatGPT-backed Codex Images endpoint answers 200 with PNG bytes even when
// jpeg/webp is requested (probed upstream, 2026-11-15). Silently dropping the field made
// callers decode PNG as the format they asked for, so a non-png request is rejected.
export type CodexOutputFormatCheck = {
ok: boolean;
forward: "png" | null;
error?: string;
};
export function checkCodexOutputFormat(body: Record<string, unknown>): CodexOutputFormatCheck {
const raw = body.output_format;
if (raw === undefined || raw === null || raw === "") return { ok: true, forward: null };
const value = typeof raw === "string" ? raw.trim().toLowerCase() : "";
if (value === "png") return { ok: true, forward: "png" };
const shown = typeof raw === "string" ? raw.trim().slice(0, 32) : typeof raw;
return {
ok: false,
forward: null,
error: `Codex image models only return PNG output; output_format "${shown}" is not supported. Omit output_format or use "png".`,
};
}

View File

@@ -0,0 +1,71 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-codex-fmt-19252-"));
process.env.DATA_DIR = DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "codex-fmt-19252-secret";
const core = await import("../../src/lib/db/core.ts");
const { handleImageGeneration } = await import("../../open-sse/handlers/imageGeneration.ts");
const originalFetch = globalThis.fetch;
const PNG_B64 = "iVBORw0KGgo=";
function pngResponse() {
return new Response(
JSON.stringify({ created: 1, data: [{ b64_json: PNG_B64 }], output_format: "png" }),
{ status: 200, headers: { "content-type": "application/json" } }
);
}
test.after(() => {
globalThis.fetch = originalFetch;
core.resetDbInstance();
fs.rmSync(DIR, { recursive: true, force: true });
});
async function run(extra: Record<string, unknown>) {
const calls: Record<string, unknown>[] = [];
globalThis.fetch = async (_u, o: RequestInit = {}) => {
calls.push(JSON.parse(String(o.body)));
return pngResponse();
};
const result = (await handleImageGeneration({
body: { model: "codex/gpt-image-2.5-flare", prompt: "blue circle", ...extra },
credentials: { accessToken: "t" },
log: null,
})) as {
success: boolean;
status?: number;
error?: unknown;
data: { data: Array<{ url: string }> };
};
return { result, calls };
}
for (const fmt of ["webp", "jpeg", "JPG"]) {
test(`#19252 output_format=${fmt} is rejected with a clear 400, no upstream call`, async () => {
const { result, calls } = await run({ output_format: fmt, output_compression: 80 });
assert.equal(result.success, false);
assert.equal(result.status, 400);
assert.match(String(result.error), /PNG/);
assert.equal(calls.length, 0);
});
}
test("#19252 output_format=png is accepted and forwarded", async () => {
const { result, calls } = await run({ output_format: "PNG", response_format: "url" });
assert.equal(result.success, true);
assert.equal(calls.length, 1);
assert.equal(calls[0].output_format, "png");
assert.match(result.data.data[0].url, /^data:image\/png;base64,/);
});
test("#19252 request without output_format keeps the upstream body unchanged", async () => {
const { result, calls } = await run({});
assert.equal(result.success, true);
assert.equal("output_format" in calls[0], false);
});