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.

✨ feat: Added support for Azure OpenAI Image Generation (#8898)

Jamie Stivala604234029c18
diff --git a/packages/model-bank/src/aiModels/azure.ts b/packages/model-bank/src/aiModels/azure.ts
index 2fd170b55fa..a764541a789 100644
--- a/packages/model-bank/src/aiModels/azure.ts
+++ b/packages/model-bank/src/aiModels/azure.ts
@@ -1,4 +1,4 @@
-import { AIChatModelCard } from '@/types/aiModel';
+import { AIChatModelCard, AIImageModelCard } from '@/types/aiModel';
const azureChatModels: AIChatModelCard[] = [
{
@@ -282,6 +282,67 @@ const azureChatModels: AIChatModelCard[] = [
},
];
-export const allModels = [...azureChatModels];
+const azureImageModels: AIImageModelCard[] = [
+ {
+ description: 'ChatGPT Image 1',
+ displayName: 'GPT Image 1',
+ enabled: true,
+ id: 'gpt-image-1',
+ parameters: {
+ imageUrl: { default: null },
+ prompt: { default: '' },
+ size: {
+ default: 'auto',
+ enum: ['auto', '1024x1024', '1536x1024', '1024x1536'],
+ },
+ },
+ type: 'image',
+ },
+ {
+ description: 'DALL·E 3',
+ displayName: 'DALL·E 3',
+ id: 'dall-e-3',
+ parameters: {
+ imageUrl: { default: null },
+ prompt: { default: '' },
+ size: {
+ default: 'auto',
+ enum: ['auto', '1024x1024', '1792x1024', '1024x1792'],
+ },
+ },
+ resolutions: ['1024x1024', '1024x1792', '1792x1024'],
+ type: 'image',
+ },
+ {
+ description: 'FLUX.1 Kontext [pro]',
+ displayName: 'FLUX.1 Kontext [pro]',
+ enabled: true,
+ id: 'FLUX.1-Kontext-pro',
+ parameters: {
+ imageUrl: { default: null },
+ prompt: { default: '' },
+ size: {
+ default: 'auto',
+ enum: ['auto', '1024x1024', '1792x1024', '1024x1792'],
+ },
+ },
+ releasedAt: '2025-06-23',
+ type: 'image',
+ },
+ {
+ description: 'FLUX.1.1 Pro',
+ displayName: 'FLUX.1.1 Pro',
+ enabled: true,
+ id: 'FLUX-1.1-pro',
+ parameters: {
+ imageUrl: { default: null },
+ prompt: { default: '' },
+ },
+ releasedAt: '2025-06-23',
+ type: 'image',
+ },
+];
+
+export const allModels = [...azureChatModels, ...azureImageModels];
export default allModels;
diff --git a/packages/model-runtime/src/azureOpenai/index.test.ts b/packages/model-runtime/src/azureOpenai/index.test.ts
index 3e1f924b3e6..600e832e6ed 100644
--- a/packages/model-runtime/src/azureOpenai/index.test.ts
+++ b/packages/model-runtime/src/azureOpenai/index.test.ts
@@ -340,6 +340,153 @@ describe('LobeAzureOpenAI', () => {
});
});
+ describe('createImage', () => {
+ beforeEach(() => {
+ // ensure images namespace exists and is spy-able
+ expect(instance['client'].images).toBeTruthy();
+ });
+
+ it('should generate image and return url from object response', async () => {
+ const url = 'https://example.com/image.png';
+ const generateSpy = vi
+ .spyOn(instance['client'].images, 'generate')
+ .mockResolvedValue({ data: [{ url }] } as any);
+
+ const res = await instance.createImage({
+ model: 'gpt-image-1',
+ params: { prompt: 'a cat' },
+ });
+
+ expect(generateSpy).toHaveBeenCalledTimes(1);
+ const args = vi.mocked(generateSpy).mock.calls[0][0] as any;
+ expect(args).not.toHaveProperty('image');
+ expect(res).toEqual({ imageUrl: url });
+ });
+
+ it('should parse string JSON response from images.generate', async () => {
+ const url = 'https://example.com/str.png';
+ const payload = JSON.stringify({ data: [{ url }] });
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue(payload as any);
+
+ const res = await instance.createImage({ model: 'gpt-image-1', params: { prompt: 'dog' } });
+ expect(res).toEqual({ imageUrl: url });
+ });
+
+ it('should parse bodyAsText JSON response', async () => {
+ const url = 'https://example.com/bodyAsText.png';
+ const bodyAsText = JSON.stringify({ data: [{ url }] });
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue({ bodyAsText } as any);
+
+ const res = await instance.createImage({ model: 'gpt-image-1', params: { prompt: 'bird' } });
+ expect(res).toEqual({ imageUrl: url });
+ });
+
+ it('should parse body JSON response', async () => {
+ const url = 'https://example.com/body.png';
+ const body = JSON.stringify({ data: [{ url }] });
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue({ body } as any);
+
+ const res = await instance.createImage({ model: 'gpt-image-1', params: { prompt: 'fish' } });
+ expect(res).toEqual({ imageUrl: url });
+ });
+
+ it('should prefer b64_json and return data URL', async () => {
+ const b64 = 'AAA';
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue({
+ data: [{ b64_json: b64 }],
+ } as any);
+
+ const res = await instance.createImage({ model: 'gpt-image-1', params: { prompt: 'sun' } });
+ expect(res.imageUrl).toBe(`data:image/png;base64,${b64}`);
+ });
+
+ it('should throw wrapped error for empty data array', async () => {
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue({ data: [] } as any);
+
+ await expect(
+ instance.createImage({ model: 'gpt-image-1', params: { prompt: 'moon' } }),
+ ).rejects.toMatchObject({
+ endpoint: 'https://***.openai.azure.com/',
+ errorType: 'AgentRuntimeError',
+ provider: 'azure',
+ error: {
+ name: 'Error',
+ cause: undefined,
+ message: expect.stringContaining('Invalid image response: missing or empty data array'),
+ },
+ });
+ });
+
+ it('should throw wrapped error when missing both b64_json and url', async () => {
+ vi.spyOn(instance['client'].images, 'generate').mockResolvedValue({
+ data: [{}],
+ } as any);
+
+ await expect(
+ instance.createImage({ model: 'gpt-image-1', params: { prompt: 'stars' } }),
+ ).rejects.toEqual({
+ endpoint: 'https://***.openai.azure.com/',
+ errorType: 'AgentRuntimeError',
+ provider: 'azure',
+ error: {
+ name: 'Error',
+ cause: undefined,
+ message: 'Invalid image response: missing both b64_json and url fields',
+ },
+ });
+ });
+
+ it('should call images.edit when imageUrl provided and strip size:auto', async () => {
+ const url = 'https://example.com/edited.png';
+ const editSpy = vi
+ .spyOn(instance['client'].images, 'edit')
+ .mockResolvedValue({ data: [{ url }] } as any);
+
+ const helpers = await import('../utils/openaiHelpers');
+ vi.spyOn(helpers, 'convertImageUrlToFile').mockResolvedValue({} as any);
+
+ const res = await instance.createImage({
+ model: 'gpt-image-1',
+ params: { prompt: 'edit', imageUrl: 'https://example.com/in.png', size: 'auto' as any },
+ });
+
+ expect(editSpy).toHaveBeenCalledTimes(1);
+ const arg = vi.mocked(editSpy).mock.calls[0][0] as any;
+ expect(arg).not.toHaveProperty('size');
+ expect(res).toEqual({ imageUrl: url });
+ });
+
+ it('should convert multiple imageUrls and pass images array to edit', async () => {
+ const url = 'https://example.com/edited2.png';
+ const editSpy = vi
+ .spyOn(instance['client'].images, 'edit')
+ .mockResolvedValue({ data: [{ url }] } as any);
+
+ const helpers = await import('../utils/openaiHelpers');
+ const spy = vi.spyOn(helpers, 'convertImageUrlToFile').mockResolvedValue({} as any);
+
+ await instance.createImage({
+ model: 'gpt-image-1',
+ params: { prompt: 'edit', imageUrls: ['u1', 'u2'] },
+ });
+
+ expect(spy).toHaveBeenCalledTimes(2);
+ const arg = vi.mocked(editSpy).mock.calls[0][0] as any;
+ expect(arg).toHaveProperty('image');
+ });
+
+ it('should not include image in generate options', async () => {
+ const generateSpy = vi
+ .spyOn(instance['client'].images, 'generate')
+ .mockResolvedValue({ data: [{ url: 'https://x/y.png' }] } as any);
+
+ await instance.createImage({ model: 'gpt-image-1', params: { prompt: 'no image' } });
+
+ const arg = vi.mocked(generateSpy).mock.calls[0][0] as any;
+ expect(arg).not.toHaveProperty('image');
+ });
+ });
+
describe('private method', () => {
describe('tocamelCase', () => {
it('should convert string to camel case', () => {
diff --git a/packages/model-runtime/src/azureOpenai/index.ts b/packages/model-runtime/src/azureOpenai/index.ts
index 1cf76a3f41c..4c76838d240 100644
--- a/packages/model-runtime/src/azureOpenai/index.ts
+++ b/packages/model-runtime/src/azureOpenai/index.ts
@@ -1,3 +1,4 @@
+import debug from 'debug';
import OpenAI, { AzureOpenAI } from 'openai';
import type { Stream } from 'openai/streaming';
@@ -13,13 +14,15 @@ import {
EmbeddingsPayload,
ModelProvider,
} from '../types';
+import { CreateImagePayload, CreateImageResponse } from '../types/image';
import { AgentRuntimeError } from '../utils/createError';
import { debugStream } from '../utils/debugStream';
import { transformResponseToStream } from '../utils/openaiCompatibleFactory';
-import { convertOpenAIMessages } from '../utils/openaiHelpers';
+import { convertImageUrlToFile, convertOpenAIMessages } from '../utils/openaiHelpers';
import { StreamingResponse } from '../utils/response';
import { OpenAIStream } from '../utils/streams';
+const azureImageLogger = debug('lobe-image:azure');
export class LobeAzureOpenAI implements LobeRuntimeAI {
client: AzureOpenAI;
@@ -116,6 +119,117 @@ export class LobeAzureOpenAI implements LobeRuntimeAI {
}
}
+ // Create image using Azure OpenAI Images API (gpt-image-1 or DALL·E deployments)
+ async createImage(payload: CreateImagePayload): Promise<CreateImageResponse> {
+ const { model, params } = payload;
+ azureImageLogger('Creating image with model: %s and params: %O', model, params);
+
+ try {
+ // Clone params and remap imageUrls/imageUrl -> image
+ const userInput: Record<string, any> = { ...params };
+
+ // Convert imageUrls to 'image' for edit API
+ if (Array.isArray(userInput.imageUrls) && userInput.imageUrls.length > 0) {
+ const imageFiles = await Promise.all(
+ userInput.imageUrls.map((url: string) => convertImageUrlToFile(url)),
+ );
+ userInput.image = imageFiles.length === 1 ? imageFiles[0] : imageFiles;
+ }
+
+ // Backward compatibility: single imageUrl -> image
+ if (userInput.imageUrl && !userInput.image) {
+ userInput.image = await convertImageUrlToFile(userInput.imageUrl);
+ }
+
+ // Remove non-API parameters to avoid unknown_parameter errors
+ delete userInput.imageUrls;
+ delete userInput.imageUrl;
+
+ const isImageEdit = Boolean(userInput.image);
+
+ azureImageLogger('Is Image Edit: ' + isImageEdit);
+ // Azure/OpenAI Images: remove unsupported/auto values where appropriate
+ if (userInput.size === 'auto') delete userInput.size;
+
+ // Build options: do not force response_format for gpt-image-1
+ const options: any = {
+ model,
+ n: 1,
+ ...(isImageEdit ? { input_fidelity: 'high' } : {}),
+ ...userInput,
+ };
+
+ // For generate, ensure no 'image' field is sent
+ if (!isImageEdit) delete options.image;
+
+ // Call Azure Images API
+ const img = isImageEdit
+ ? await this.client.images.edit(options)
+ : await this.client.images.generate(options);
+
+ // Normalize possible string JSON response -- Sometimes Azure Image API returns a text/plain Content-Type
+ let result: any = img as any;
+ if (typeof result === 'string') {
+ try {
+ result = JSON.parse(result);
+ } catch {
+ const truncated = result.length > 500 ? result.slice(0, 500) + '...[truncated]' : result;
+ azureImageLogger(
+ `Failed to parse string response from images API. Raw response: ${truncated}`,
+ );
+ throw new Error('Invalid image response: expected JSON string but parsing failed');
+ }
+ } else if (result && typeof result === 'object') {
+ // Handle common Azure REST shapes
+ if (typeof (result as any).bodyAsText === 'string') {
+ try {
+ result = JSON.parse((result as any).bodyAsText);
+ } catch {
+ const rawText = (result as any).bodyAsText;
+ const truncated =
+ rawText.length > 500 ? rawText.slice(0, 500) + '...[truncated]' : rawText;
+ azureImageLogger(
+ `Failed to parse bodyAsText from images API. Raw response: ${truncated}`,
+ );
+ throw new Error('Invalid image response: bodyAsText not valid JSON');
+ }
+ } else if (typeof (result as any).body === 'string') {
+ try {
+ result = JSON.parse((result as any).body);
+ } catch {
+ azureImageLogger('Failed to parse body from images API response');
+ throw new Error('Invalid image response: body not valid JSON');
+ }
+ }
+ }
+
+ // Validate response
+ if (!result || !result.data || !Array.isArray(result.data) || result.data.length === 0) {
+ throw new Error(
+ `Invalid image response: missing or empty data array. Response: ${JSON.stringify(result)}`,
+ );
+ }
+
+ const imageData: any = result.data[0];
+ if (!imageData)
+ throw new Error('Invalid image response: first data item is null or undefined');
+
+ // Prefer base64 if provided, otherwise URL
+ if (imageData.b64_json) {
+ const mimeType = 'image/png';
+ return { imageUrl: `data:${mimeType};base64,${imageData.b64_json}` };
+ }
+
+ if (imageData.url) {
+ return { imageUrl: imageData.url };
+ }
+
+ throw new Error('Invalid image response: missing both b64_json and url fields');
+ } catch (e) {
+ return this.handleError(e, model);
+ }
+ }
+
protected handleError(e: any, model?: string): never {
let error = e as { [key: string]: any; code: string; message: string };