Implementation:Openai Openai node Images Resource
| Knowledge Sources | |
|---|---|
| Domains | SDK, Images, Generation |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
The Images resource class provides methods for generating, editing, and creating variations of images through the OpenAI API.
Description
The Images class extends APIResource and exposes three primary operations: generate for creating images from text prompts, edit for modifying existing images based on prompts and optional masks, and createVariation for generating variations of a given image. The resource supports multiple model families including GPT image models (gpt-image-1.5, gpt-image-1, gpt-image-1-mini) and DALL-E models (dall-e-2, dall-e-3).
Both generate and edit support streaming via method overloads. When streaming is enabled, the API returns a Stream of partial image events (ImageGenStreamEvent or ImageEditStreamEvent) that deliver progressive base64-encoded image data, culminating in a completed event with the final image and usage statistics. Non-streaming calls return an ImagesResponse containing an array of Image objects with optional base64 data, URLs, or revised prompts.
The module defines extensive TypeScript types for all parameters and response shapes, including configurable options for image size (1024x1024, 1536x1024, 1024x1536, etc.), quality levels, output formats (png, jpeg, webp), background transparency, and compression levels. File uploads are handled through the Uploadable type with multipart form encoding.
Usage
Use this resource when you need to generate images from text descriptions, edit existing images with inpainting or outpainting, or create variations of source images. The streaming mode is particularly useful for providing progressive rendering feedback in user interfaces.
Code Reference
Source Location
- Repository: openai-node
- File: src/resources/images.ts
Signature
export class Images extends APIResource {
createVariation(body: ImageCreateVariationParams, options?: RequestOptions): APIPromise<ImagesResponse>;
edit(body: ImageEditParamsNonStreaming, options?: RequestOptions): APIPromise<ImagesResponse>;
edit(body: ImageEditParamsStreaming, options?: RequestOptions): APIPromise<Stream<ImageEditStreamEvent>>;
generate(body: ImageGenerateParamsNonStreaming, options?: RequestOptions): APIPromise<ImagesResponse>;
generate(body: ImageGenerateParamsStreaming, options?: RequestOptions): APIPromise<Stream<ImageGenStreamEvent>>;
}
Import
import OpenAI from 'openai';
I/O Contract
Inputs (generate)
| Name | Type | Required | Description |
|---|---|---|---|
| prompt | string |
Yes | Text description of the desired image (max 32000 chars for GPT image models) |
| model | ImageModel | null | No | Model to use: dall-e-2, dall-e-3, gpt-image-1, gpt-image-1-mini, gpt-image-1.5 |
| n | null | No | Number of images to generate (1-10; dall-e-3 only supports 1) |
| size | '1024x1024' | '1536x1024' | '1024x1536' | ... | No | Image dimensions |
| quality | 'hd' | 'low' | 'medium' | 'high' | 'auto' | No | Image quality level |
| output_format | 'jpeg' | 'webp' | null | No | Output format (GPT image models only) |
| background | 'opaque' | 'auto' | null | No | Background transparency (GPT image models only) |
| stream | null | No | Enable streaming mode for progressive rendering |
| partial_images | null | No | Number of partial images during streaming (0-3) |
| style | 'natural' | null | No | Image style (dall-e-3 only) |
Inputs (edit)
| Name | Type | Required | Description |
|---|---|---|---|
| image | Array<Uploadable> | Yes | Source image(s) to edit (up to 16 for GPT image models) |
| prompt | string |
Yes | Description of the desired edit |
| mask | Uploadable |
No | Image mask indicating areas to edit (transparent areas) |
| input_fidelity | 'low' | null | No | How closely to match input image features |
Outputs
| Name | Type | Description |
|---|---|---|
| created | number |
Unix timestamp of when the image was created |
| data | Array<Image> |
List of generated images with b64_json, url, or revised_prompt |
| usage | ImagesResponse.Usage |
Token usage information (GPT image models) |
| background | 'opaque' | Background parameter used |
| output_format | 'webp' | 'jpeg' | Output format used |
| quality | 'medium' | 'high' | Quality level used |
| size | '1024x1536' | '1536x1024' | Image dimensions used |
Usage Examples
import OpenAI from 'openai';
import fs from 'fs';
const client = new OpenAI();
// Generate an image
const imagesResponse = await client.images.generate({
prompt: 'A cute baby sea otter',
model: 'gpt-image-1',
size: '1024x1024',
quality: 'high',
});
// Edit an image with a mask
const editResponse = await client.images.edit({
image: fs.createReadStream('path/to/image.png'),
prompt: 'A cute baby sea otter wearing a beret',
mask: fs.createReadStream('path/to/mask.png'),
});
// Create a variation (dall-e-2 only)
const variationResponse = await client.images.createVariation({
image: fs.createReadStream('otter.png'),
});
// Streaming image generation
const stream = await client.images.generate({
prompt: 'A futuristic cityscape',
model: 'gpt-image-1',
stream: true,
partial_images: 2,
});
for await (const event of stream) {
if (event.type === 'image_generation.partial_image') {
console.log('Partial image received:', event.partial_image_index);
} else if (event.type === 'image_generation.completed') {
console.log('Final image ready, tokens used:', event.usage.total_tokens);
}
}
Key Types
Image
interface Image {
b64_json?: string;
revised_prompt?: string;
url?: string;
}
ImageModel
type ImageModel = 'gpt-image-1.5' | 'dall-e-2' | 'dall-e-3'
| 'gpt-image-1' | 'gpt-image-1-mini';
Stream Event Types
type ImageGenStreamEvent = ImageGenPartialImageEvent | ImageGenCompletedEvent;
type ImageEditStreamEvent = ImageEditPartialImageEvent | ImageEditCompletedEvent;