Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(model-runtime): stop injecting n into OpenAI-compatible image requests (#20494)

Arvin Xu7b08086728f5
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/createImage.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/createImage.ts
index 6e5c87dc5d7..07cbc261f5c 100644
--- a/packages/model-runtime/src/core/openaiCompatibleFactory/createImage.ts
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/createImage.ts
@@ -85,8 +85,9 @@ async function generateByImageMode(
const isGptImage1Family = /^gpt-image-1(?:$|[-.])/.test(routingModel);
const supportsInputFidelity = isImageEdit && isGptImage1Family && !routingModel.includes('mini');
+ // Leave `n` to the API default (1): each generation is its own request, and
+ // strict-schema OpenAI-compatible gateways reject properties the model does not declare.
const defaultInput = {
- n: 1,
...(routingModel.includes('dall-e') ? { response_format: 'b64_json' } : {}),
// https://platform.openai.com/docs/api-reference/images/createEdit#images_createedit-input_fidelity
...(supportsInputFidelity ? { input_fidelity: 'high' } : {}),
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts
index de8243ced29..3d4e7436772 100644
--- a/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/index.test.ts
@@ -2235,7 +2235,6 @@ describe('LobeOpenAICompatibleFactory', () => {
expect(instance['client'].images.generate).toHaveBeenCalledWith({
model: 'dall-e-3',
- n: 1,
prompt: 'A beautiful sunset',
quality: 'standard',
response_format: 'b64_json',
@@ -2304,7 +2303,6 @@ describe('LobeOpenAICompatibleFactory', () => {
// size: 'auto' should be removed from the options
expect(instance['client'].images.generate).toHaveBeenCalledWith({
model: 'dall-e-3',
- n: 1,
prompt: 'A beautiful sunset',
response_format: 'b64_json',
});
@@ -2330,7 +2328,6 @@ describe('LobeOpenAICompatibleFactory', () => {
// gpt-image-1 model should not include response_format parameter
expect(instance['client'].images.generate).toHaveBeenCalledWith({
model: 'gpt-image-1',
- n: 1,
prompt: 'A modern digital artwork',
size: '1024x1024',
});
@@ -2367,7 +2364,6 @@ describe('LobeOpenAICompatibleFactory', () => {
image: expect.any(File),
mask: 'https://example.com/mask.jpg',
model: 'dall-e-2',
- n: 1,
prompt: 'Add a rainbow to this image',
response_format: 'b64_json',
});
@@ -2412,7 +2408,6 @@ describe('LobeOpenAICompatibleFactory', () => {
expect(instance['client'].images.edit).toHaveBeenCalledWith({
image: [mockFile1, mockFile2],
model: 'dall-e-2',
- n: 1,
prompt: 'Merge these images',
response_format: 'b64_json',
});
@@ -2464,7 +2459,6 @@ describe('LobeOpenAICompatibleFactory', () => {
image: expect.any(File),
input_fidelity: 'high',
model: 'gpt-image-1',
- n: 1,
prompt: 'Edit this image with gpt-image-1',
});
@@ -2497,7 +2491,6 @@ describe('LobeOpenAICompatibleFactory', () => {
expect(editArgs).not.toHaveProperty('input_fidelity');
expect(editArgs).toMatchObject({
model: 'gpt-image-2',
- n: 1,
});
});
@@ -2629,12 +2622,31 @@ describe('LobeOpenAICompatibleFactory', () => {
customParam: 'should remain unchanged',
image: expect.any(File),
model: 'dall-e-2',
- n: 1,
prompt: 'Test prompt',
response_format: 'b64_json',
});
});
+ it('should not inject `n` that the caller never sent', async () => {
+ // Strict-schema OpenAI-compatible gateways (e.g. a custom provider fronting
+ // flux-1-schnell) reject unknown root properties with
+ // "Additional or unevaluated properties '/n' at '/' not allowed".
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue({
+ data: [{ url: 'https://example.com/flux.png' }],
+ } as any);
+
+ await (instance as any).createImage({
+ model: 'flux-1-schnell',
+ params: { prompt: 'A lighthouse at dusk', size: '1024x1024' },
+ });
+
+ expect(instance['client'].images.generate).toHaveBeenCalledWith({
+ model: 'flux-1-schnell',
+ prompt: 'A lighthouse at dusk',
+ size: '1024x1024',
+ });
+ });
+
it('should handle parameters without imageUrls', async () => {
const mockResponse = {
data: [{ b64_json: 'test-base64' }],
@@ -2655,7 +2667,6 @@ describe('LobeOpenAICompatibleFactory', () => {
expect(instance['client'].images.generate).toHaveBeenCalledWith({
model: 'dall-e-3',
- n: 1,
prompt: 'Test prompt',
quality: 'hd',
response_format: 'b64_json',