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: resolve qwen-image-edit imageUrls conversion issue (#9414)

Maple Gaoec5af1b4c72d
diff --git a/packages/model-runtime/src/providers/qwen/createImage.test.ts b/packages/model-runtime/src/providers/qwen/createImage.test.ts
index 5bd8509086a..9f13c29d781 100644
--- a/packages/model-runtime/src/providers/qwen/createImage.test.ts
+++ b/packages/model-runtime/src/providers/qwen/createImage.test.ts
@@ -700,5 +700,167 @@ describe('createQwenImage', () => {
}),
);
});
+
+ it('should convert imageUrls array to imageUrl for qwen-image-edit', async () => {
+ const mockImageUrl =
+ 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/imageUrls-converted.jpg';
+
+ global.fetch = vi.fn().mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: {
+ choices: [
+ {
+ message: {
+ content: [{ image: mockImageUrl }],
+ },
+ },
+ ],
+ },
+ request_id: 'req-imageUrls-123',
+ }),
+ });
+
+ const payload: CreateImagePayload = {
+ model: 'qwen-image-edit',
+ params: {
+ prompt: 'Edit this image to add a dog',
+ imageUrls: [
+ 'https://example.com/source-image-1.jpg',
+ 'https://example.com/source-image-2.jpg',
+ ],
+ },
+ };
+
+ const result = await createQwenImage(payload, mockOptions);
+
+ expect(result).toEqual({
+ imageUrl: mockImageUrl,
+ });
+
+ const [url, options] = (fetch as any).mock.calls[0];
+ const body = JSON.parse(options.body);
+
+ // Verify that the first imageUrl from imageUrls array was used
+ expect(body.input.messages[0].content[0].image).toBe(
+ 'https://example.com/source-image-1.jpg',
+ );
+ });
+
+ it('should use first imageUrl when imageUrls has multiple elements', async () => {
+ const mockImageUrl = 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/first-element.jpg';
+
+ global.fetch = vi.fn().mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: {
+ choices: [
+ {
+ message: {
+ content: [{ image: mockImageUrl }],
+ },
+ },
+ ],
+ },
+ request_id: 'req-first-element',
+ }),
+ });
+
+ const payload: CreateImagePayload = {
+ model: 'qwen-image-edit',
+ params: {
+ prompt: 'Use the first image only',
+ imageUrls: [
+ 'https://example.com/first-image.jpg',
+ 'https://example.com/second-image.jpg',
+ 'https://example.com/third-image.jpg',
+ ],
+ },
+ };
+
+ await createQwenImage(payload, mockOptions);
+
+ const [url, options] = (fetch as any).mock.calls[0];
+ const body = JSON.parse(options.body);
+
+ // Should use only the first image from the array
+ expect(body.input.messages[0].content[0].image).toBe('https://example.com/first-image.jpg');
+ });
+
+ it('should throw error when imageUrls is empty array', async () => {
+ const payload: CreateImagePayload = {
+ model: 'qwen-image-edit',
+ params: {
+ prompt: 'Edit this image',
+ imageUrls: [], // Empty array
+ },
+ };
+
+ await expect(createQwenImage(payload, mockOptions)).rejects.toEqual(
+ expect.objectContaining({
+ errorType: 'ProviderBizError',
+ provider: 'qwen',
+ }),
+ );
+ });
+
+ it('should prioritize imageUrl over imageUrls when both are provided', async () => {
+ const mockImageUrl = 'https://dashscope.oss-cn-beijing.aliyuncs.com/aigc/priority-test.jpg';
+
+ global.fetch = vi.fn().mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ output: {
+ choices: [
+ {
+ message: {
+ content: [{ image: mockImageUrl }],
+ },
+ },
+ ],
+ },
+ request_id: 'req-priority-test',
+ }),
+ });
+
+ const payload: CreateImagePayload = {
+ model: 'qwen-image-edit',
+ params: {
+ prompt: 'Test priority between imageUrl and imageUrls',
+ imageUrl: 'https://example.com/priority-image.jpg',
+ imageUrls: [
+ 'https://example.com/should-not-use-1.jpg',
+ 'https://example.com/should-not-use-2.jpg',
+ ],
+ },
+ };
+
+ await createQwenImage(payload, mockOptions);
+
+ const [url, options] = (fetch as any).mock.calls[0];
+ const body = JSON.parse(options.body);
+
+ // Should use imageUrl, not imageUrls
+ expect(body.input.messages[0].content[0].image).toBe(
+ 'https://example.com/priority-image.jpg',
+ );
+ });
+
+ it('should throw error when neither imageUrl nor imageUrls are provided', async () => {
+ const payload: CreateImagePayload = {
+ model: 'qwen-image-edit',
+ params: {
+ prompt: 'Edit this image',
+ // Neither imageUrl nor imageUrls provided
+ },
+ };
+
+ await expect(createQwenImage(payload, mockOptions)).rejects.toEqual(
+ expect.objectContaining({
+ errorType: 'ProviderBizError',
+ provider: 'qwen',
+ }),
+ );
+ });
});
});
diff --git a/packages/model-runtime/src/providers/qwen/createImage.ts b/packages/model-runtime/src/providers/qwen/createImage.ts
index b7264458ce3..3c279008d72 100644
--- a/packages/model-runtime/src/providers/qwen/createImage.ts
+++ b/packages/model-runtime/src/providers/qwen/createImage.ts
@@ -98,8 +98,15 @@ async function createImageEdit(
const endpoint = `https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation`;
log('Creating image edit with model: %s, endpoint: %s', model, endpoint);
- if (!params.imageUrl) {
- throw new Error('imageUrl is required for qwen-image-edit model');
+ // Handle imageUrls to imageUrl conversion
+ let imageUrl = params.imageUrl;
+ if (!imageUrl && params.imageUrls && params.imageUrls.length > 0) {
+ imageUrl = params.imageUrls[0];
+ log('Converting imageUrls to imageUrl: using first image %s', imageUrl);
+ }
+
+ if (!imageUrl) {
+ throw new Error('imageUrl or imageUrls is required for qwen-image-edit model');
}
const response = await fetch(endpoint, {
@@ -107,7 +114,7 @@ async function createImageEdit(
input: {
messages: [
{
- content: [{ image: params.imageUrl }, { text: params.prompt }],
+ content: [{ image: imageUrl }, { text: params.prompt }],
role: 'user',
},
],
@@ -152,10 +159,10 @@ async function createImageEdit(
throw new Error('No image found in response content');
}
- const imageUrl = imageContent.image;
- log('Image edit generated successfully: %s', imageUrl);
+ const resultImageUrl = imageContent.image;
+ log('Image edit generated successfully: %s', resultImageUrl);
- return { imageUrl };
+ return { imageUrl: resultImageUrl };
}
/**
@@ -224,11 +231,11 @@ export async function createQwenImage(
};
}
- const imageUrl = taskStatus.output.results[0].url;
- log('Image generated successfully: %s', imageUrl);
+ const generatedImageUrl = taskStatus.output.results[0].url;
+ log('Image generated successfully: %s', generatedImageUrl);
return {
- data: { imageUrl },
+ data: { imageUrl: generatedImageUrl },
status: 'success',
};
}