> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dartantic.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Image Generation

> Generate images on the provider and receive both previews and final assets in one streaming response.

The OpenAI Responses provider exposes an image generation tool that allows you
to generate images on the provider.

## Enable Image Generation

The image generation tool can be enabled via the `chatModelOptions` parameter of
the `Agent` constructor:

```dart theme={null}
final agent = Agent(
  'openai-responses',
  chatModelOptions: const OpenAIResponsesChatModelOptions(
    serverSideTools: {OpenAIServerSideTool.imageGeneration},
    imageGenerationConfig: ImageGenerationConfig(
      partialImages: 3,
      quality: ImageGenerationQuality.low,
      size: ImageGenerationSize.square256,
    ),
  ),
);
```

`ImageGenerationConfig` lets you request progressive previews (`partialImages`),
choose the quality profile, and select a size. Omit these values to fall back to
the provider defaults.

## Streaming Previews

Partial renders arrive as base64-encoded bytes inside metadata events:

```dart theme={null}
void handleMetadata(Map<String, dynamic> metadata) {
  final events = metadata['image_generation'] as List?;
  if (events == null) return;

  for (final event in events) {
    final preview = event['partial_image_b64'] as String?;
    if (preview != null) {
      // import 'dart:convert' to use base64Decode
      final bytes = base64Decode(preview);
      savePreview(bytes, event['partial_image_index'] as int);
    }
  }
}
```

Each event also includes the tool stage (`response.image_generation_call.*`) so
you can display progress indicators while the image renders.

## Final Image

Completed images are attached to the assistant message as `DataPart` instances
with an `image/*` MIME type:

```dart theme={null}
final result = await agent.send(prompt);
for (final msg in result.messages) {
  for (final part in msg.parts) {
    if (part is DataPart && part.mimeType.startsWith('image/')) {
      saveFinalImage(part.bytes);
    }
  }
}
```

No extra download step is required; Dartantic fetches the file from the provider
and hands you the bytes.

## Example

* [Server-side image generation demo](https://github.com/csells/dartantic_ai/blob/main/packages/dartantic_ai/example/bin/server_side_tools/server_side_image_gen.dart)

## Related Topics

* [Server-Side Tools](/server-side-tools)
* [Code Interpreter](/server-side-tools/code-interpreter)
