OpenAIImageModel: OpenAI Image Model

OpenAIImageModelR Documentation

OpenAI Image Model

Description

Image model implementation for OpenAI's image generation and editing APIs. Exported so that OpenAI-compatible providers in companion packages (e.g. aisdk.providers) can construct image models across package boundaries.

Super class

aisdk::ImageModelV1 -> OpenAIImageModel

Methods

Public methods

Inherited methods

Method new()

Initialize the OpenAI image model.

Usage
OpenAIImageModel$new(model_id, config)
Arguments
model_id

The model ID (e.g., "gpt-image-2", "gpt-image-1.5").

config

Configuration list.


Method do_generate_image()

Generate images.

Tries the classic POST /v1/images/generations endpoint first. If that returns a 404 with invalid_api_path / "not available" — the signal some OpenAI-compatible proxies emit when they only expose the newer Responses API — falls back to POST /v1/responses with the image_generation tool and decodes the returned base64 image.

On the fallback path, the standard image params (quality, size, output_format, output_compression, background, moderation, n) are forwarded into the tool config, and a previous_response_id from a prior fallback call is auto-attached so multi-turn edits ("now make it realistic") work the same as on the language-model path. Use get_last_response_id() / reset() to inspect or clear that state.

Usage
OpenAIImageModel$do_generate_image(params)
Arguments
params

A list of call options.

Returns

A GenerateImageResult object.


Method do_generate_image_classic()

Generate images via the classic POST /v1/images/generations endpoint. Called by do_generate_image(); exposed for callers that want to bypass the Responses-API fallback on proxies they trust.

Usage
OpenAIImageModel$do_generate_image_classic(params)
Arguments
params

A list of call options (see do_generate_image).

Returns

A GenerateImageResult object.


Method do_generate_image_via_responses()

Generate images via POST /v1/responses with the image_generation tool. Used as a fallback when the classic ⁠/v1/images/generations⁠ endpoint is unreachable (e.g. OpenAI-compatible proxies that only expose the Responses API).

Usage
OpenAIImageModel$do_generate_image_via_responses(params)
Arguments
params

A list of call options (see do_generate_image).

Returns

A GenerateImageResult object.


Method looks_like_missing_classic_endpoint()

Heuristic check used by do_generate_image() / do_edit_image() to decide whether a classic-endpoint error looks like "endpoint not available" on the proxy, in which case the Responses-API fallback is taken. Matches both the ⁠/images/generations⁠ and ⁠/images/edits⁠ paths.

Usage
OpenAIImageModel$looks_like_missing_classic_endpoint(err)
Arguments
err

An error condition raised by the classic-endpoint call.

Returns

TRUE if the error message matches the "missing endpoint" shape.


Method get_last_response_id()

Return the most recent Responses-API response id captured during the ⁠/v1/responses⁠ fallback path. Used to chain multi-turn image edits via previous_response_id.

Usage
OpenAIImageModel$get_last_response_id()
Returns

Character scalar or NULL if no fallback call has succeeded yet.


Method reset()

Clear any stored previous_response_id, ending the current multi-turn image session on the Responses-API fallback path.

Usage
OpenAIImageModel$reset()
Returns

The model, invisibly.


Method do_edit_image()

Edit images.

Tries the classic POST /v1/images/edits multipart endpoint first. If that returns the same "missing endpoint" 404 signal handled by do_generate_image(), falls back to POST /v1/responses with the source image inlined as an input_image data URL and the optional mask passed via input_image_mask on the image_generation tool.

The Responses fallback accepts a single source image per turn (multi-reference edit is classic-only). Image params (quality, size, output_format, background, output_compression, moderation, input_fidelity) are forwarded into the tool config, and previous_response_id is auto-attached from any prior fallback call so iterative edits chain.

Usage
OpenAIImageModel$do_edit_image(params)
Arguments
params

A list of call options.

Returns

A GenerateImageResult object.


Method do_edit_image_classic()

Edit images via the classic POST /v1/images/edits multipart endpoint. Called by do_edit_image(); exposed for callers that want to bypass the Responses-API fallback.

Usage
OpenAIImageModel$do_edit_image_classic(params)
Arguments
params

A list of call options (see do_edit_image).

Returns

A GenerateImageResult object.


Method do_stream_image()

Stream image generation with partial-image previews via POST /v1/responses. Sets stream = TRUE and partial_images on the image_generation tool config; dispatches SSE events to the user-supplied callback (one per partial frame, one final). Uses the same Responses-API path as the non-streaming fallback, with the same previous_response_id chaining.

Usage
OpenAIImageModel$do_stream_image(params, callback)
Arguments
params

A list of call options. The partial_images field (0–3) controls how many preview frames the API emits before the final image; default 2.

callback

A function receiving each event list.

Returns

A GenerateImageResult with the final image.


Method do_edit_image_via_responses()

Edit images via POST /v1/responses with the image_generation tool in edit mode. Inlines the source image as a base64 data URL inside an input_image block; passes the optional mask via the tool's input_image_mask field.

Usage
OpenAIImageModel$do_edit_image_via_responses(params)
Arguments
params

A list of call options (see do_edit_image).

Returns

A GenerateImageResult object.


Method clone()

The objects of this class are cloneable with this method.

Usage
OpenAIImageModel$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


aisdk documentation built on May 29, 2026, 9:07 a.m.