Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions apps/gateway/src/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,37 @@ describe("api", () => {
expect(res.status).toBe(400);
});

for (const model of [
"gemini-3-pro-image-preview",
"gemini-3.1-flash-image-preview",
]) {
test(`/v1/chat/completions rejects image_config.n > 1 for ${model}`, async () => {
const res = await app.request("/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer fake",
},
body: JSON.stringify({
model,
messages: [
{
role: "user",
content: "Generate a simple image of a red circle.",
},
],
image_config: {
n: 2,
},
}),
});

expect(res.status).toBe(400);
const json = await res.json();
expect(json.message).toContain("image_config.n > 1");
});
}

// test for missing Content-Type header
test("/v1/chat/completions missing Content-Type header", async () => {
const res = await app.request("/v1/chat/completions", {
Expand Down
22 changes: 17 additions & 5 deletions apps/gateway/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ import { validateModelCapabilities } from "./tools/validate-model-capabilities.j
import type { OriginalRequestParams } from "./tools/resolve-provider-context.js";
import type { ServerTypes } from "@/vars.js";

const googleSingleImageModels = new Set([
"gemini-3-pro-image-preview",
"gemini-3.1-flash-image-preview",
]);

/**
* Filter expanded region entries to only those with available API keys.
* - Non-regional mappings (no region) pass through unchanged.
Expand Down Expand Up @@ -669,11 +674,9 @@ chat.openapi(completions, async (c) => {
const requestedRegion = parseResult.requestedRegion;

// Count input images from messages for cost calculation
const inputImageCount =
requestedModel === "gemini-3-pro-image-preview" ||
requestedModel === "gemini-3.1-flash-image-preview"
? countInputImages(messages)
: 0;
const inputImageCount = googleSingleImageModels.has(requestedModel)
? countInputImages(messages)
: 0;

// Resolve model info and filter deactivated providers
const modelInfoResult = resolveModelInfo(
Expand Down Expand Up @@ -733,6 +736,15 @@ chat.openapi(completions, async (c) => {
});
}

if (
googleSingleImageModels.has(requestedModel) &&
(image_config?.n ?? 1) > 1
) {
Comment on lines +740 to +742
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Scope n>1 rejection to Google providers only

This check rejects purely by requestedModel, but parseModelInput maps unknown provider/model inputs to requestedProvider === "custom" while preserving the model name. As a result, a custom provider request like acme/gemini-3-pro-image-preview with image_config.n: 2 now fails with a 400 even though the gateway cannot assume Google model limits for custom backends. Please gate this condition on provider/family (non-custom Google paths) so custom OpenAI-compatible providers are not incorrectly blocked.

Useful? React with 👍 / 👎.

throw new HTTPException(400, {
message: `Model ${requestedModel} does not support image_config.n > 1. Use n=1.`,
});
}

// === Early API key and organization validation for coding model restriction ===
// We need to fetch these early to check coding model restrictions before capability checks
const auth = c.req.header("Authorization");
Expand Down
3 changes: 2 additions & 1 deletion packages/actions/src/prepare-request-body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1722,12 +1722,13 @@ export async function prepareRequestBody(
) {
// Set responseModalities to enable image output
requestBody.generationConfig.responseModalities = ["TEXT", "IMAGE"];
requestBody.generationConfig.imageConfig = {};
if (image_config.aspect_ratio !== undefined) {
requestBody.generationConfig.imageConfig ??= {};
requestBody.generationConfig.imageConfig.aspectRatio =
image_config.aspect_ratio;
}
if (image_config.image_size !== undefined) {
requestBody.generationConfig.imageConfig ??= {};
requestBody.generationConfig.imageConfig.imageSize =
mapGoogleImageSize(image_config.image_size);
}
Expand Down
Loading