stream_image: Stream Image Generation

View source: R/image_api.R

stream_imageR Documentation

Stream Image Generation

Description

Stream image generation with partial-image previews, useful for showing the user progressive renders before the final image arrives. Currently implemented for OpenAI's Responses API (partial_images 0–3); other providers raise an informative error directing callers back to generate_image().

Usage

stream_image(
  model,
  prompt,
  callback,
  output_dir = tempdir(),
  partial_images = 2,
  registry = NULL,
  ...
)

Arguments

model

An ImageModelV1 object or provider:model string.

prompt

Prompt describing the desired image.

callback

A function called for each partial/final event. The event is a named list with type ("partial" or "completed"), bytes (raw vector), media_type (e.g. "image/png"), index (integer for partials), and done (logical).

output_dir

Directory where the final image is written. Defaults to tempdir().

partial_images

Integer 0–3 — how many preview frames to request. Defaults to 2. Set to 0 to disable previews and only receive the final image via the callback.

registry

Optional provider registry.

...

Additional arguments passed to the model implementation (e.g. quality, output_format, background, size).

Value

A GenerateImageResult with the final image.

Examples

## Not run: 
provider <- create_openai()
model <- provider$image_model("gpt-image-1.5")

stream_image(model, "a glowing nebula", callback = function(event) {
  if (event$type == "partial") {
    message(sprintf("Preview #%d (%d bytes)", event$index, length(event$bytes)))
  } else {
    message("Final image arrived: ", length(event$bytes), " bytes")
  }
})

## End(Not run)

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