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
20 changes: 11 additions & 9 deletions content/providers/01-ai-sdk-providers/04-azure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,9 @@ const result = await generateText({

#### Image Generation Tool

<Note type="warning">
Azure OpenAI Responses API
[image_generation](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses?tabs=python-secure#image-generation-preview)
is now Preview release(Not GA release).Image tool does not currently support
streaming mode. You can not use the image tool with `streamText` currently.
</Note>

Azure OpenAI's Responses API supports multi-modal image generation as a provider-defined tool.
Availability is restricted to specific models (for example, `gpt-5` variants).

You can use the image tool with `generateText`.

```ts
import { createAzure } from '@ai-sdk/azure';
import { generateText } from 'ai';
Expand All @@ -459,6 +450,17 @@ for (const toolResult of result.staticToolResults) {
}
```

<Note>
The tool must be named `image_generation` when using Azure OpenAI's image
generation functionality. This name is required by Azure OpenAI's API
specification and cannot be customized.
</Note>

<Note>
The 'image_generation' tool is only supported with the default responses API,
and is not supported when using 'azure.chat' or 'azure.completion'
</Note>

<Note>
To use image_generation, you must first create an image generation model. You
must add a deployment specification to the header
Expand Down
21 changes: 0 additions & 21 deletions examples/ai-core/src/stream-text/azure-image-generation-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@ import { presentImages } from '../lib/present-image';
import { run } from '../lib/run';
import { convertBase64ToUint8Array } from '../lib/convert-base64';

/**
*
* *** NOTICE ***
* The image_generation function is currently preview(Not GA).
* Unfortunately ,This example code does not work, now.
* Because image_generation tool is not supported stream mode on Azure OpenAI, yet.
* So it doesn't work on streamText function.
*
* This example finish error with this message.
* "ImageGen as a tool is not supported in streaming mode."
*
*
* ` The Responses API image generation tool does not currently support streaming mode. `
* link:
* https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses?tabs=python-secure#image-generation-preview
*
*
* When updated on Azure , it will work on streamText function in the future.
* And then this example code will be fixed.
*/

run(async () => {
const azure = createAzure({
headers: {
Expand Down
24 changes: 24 additions & 0 deletions examples/next-openai/agent/azure-image-generation-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createAzure, azure } from '@ai-sdk/azure';
import { InferAgentUIMessage, ToolLoopAgent } from 'ai';

export const azureImageGenerationAgent = new ToolLoopAgent({
model: createAzure({
headers: {
'x-ms-oai-image-generation-deployment': 'gpt-image-1', // use your own image model deployment
},
})('gpt-4.1-mini'),
tools: {
image_generation: azure.tools.imageGeneration({
partialImages: 3,
quality: 'low',
size: '1024x1024',
}),
},
onStepFinish: ({ request }) => {
console.log(JSON.stringify(request.body, null, 2));
},
});

export type AzureImageGenerationMessage = InferAgentUIMessage<
typeof azureImageGenerationAgent
>;
11 changes: 11 additions & 0 deletions examples/next-openai/app/api/chat-azure-image-generation/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createAgentUIStreamResponse } from 'ai';
import { azureImageGenerationAgent } from '@/agent/azure-image-generation-agent';

export async function POST(req: Request) {
const { messages } = await req.json();

return createAgentUIStreamResponse({
agent: azureImageGenerationAgent,
messages,
});
}
40 changes: 40 additions & 0 deletions examples/next-openai/app/test-azure-image-generation/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use client';

import { AzureImageGenerationMessage } from '@/agent/azure-image-generation-agent';
import ChatInput from '@/components/chat-input';
import ImageGenerationView from '@/components/tool/openai-image-generation-view';
import { useChat } from '@ai-sdk/react';
import { DefaultChatTransport } from 'ai';

export default function TestOpenAIImageGeneration() {
const { status, sendMessage, messages } =
useChat<AzureImageGenerationMessage>({
transport: new DefaultChatTransport({
api: '/api/chat-azure-image-generation',
}),
});

return (
<div className="flex flex-col py-24 mx-auto w-full max-w-md stretch">
<h1 className="mb-4 text-xl font-bold">
Azure OpenAI Image Generation Test
</h1>

{messages.map(message => (
<div key={message.id} className="whitespace-pre-wrap">
{message.role === 'user' ? 'User: ' : 'AI: '}
{message.parts.map((part, index) => {
switch (part.type) {
case 'text':
return <div key={index}>{part.text}</div>;
case 'tool-image_generation':
return <ImageGenerationView key={index} invocation={part} />;
}
})}
</div>
))}

<ChatInput status={status} onSubmit={text => sendMessage({ text })} />
</div>
);
}

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions packages/azure/src/__fixtures__/azure-image-generation-tool.1.json

Large diffs are not rendered by default.

This file was deleted.

224 changes: 210 additions & 14 deletions packages/azure/src/__snapshots__/azure-openai-provider.test.ts.snap

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion packages/azure/src/azure-openai-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ describe('responses', () => {
let result: Awaited<ReturnType<LanguageModelV3['doGenerate']>>;

beforeEach(async () => {
prepareJsonFixtureResponse('openai-image-generation-tool.1');
prepareJsonFixtureResponse('azure-image-generation-tool.1');

result = await createModel('test-deployment').doGenerate({
prompt: TEST_PROMPT,
Expand Down Expand Up @@ -1694,4 +1694,24 @@ describe('responses', () => {
).toMatchSnapshot();
});
});
describe('image generation tool', () => {
it('should stream image generation tool results include', async () => {
prepareChunksFixtureResponse('azure-image-generation-tool.1');
const result = await createModel('test-deployment').doStream({
prompt: TEST_PROMPT,
tools: [
{
type: 'provider-defined',
id: 'openai.image_generation',
name: 'image_generation',
args: {},
},
],
});

expect(
await convertReadableStreamToArray(result.stream),
).toMatchSnapshot();
});
});
});
Loading