Skip to content

Image generation ​

Image generation turns a text prompt into image bytes through an ImageGenerationModel. Use the provider-neutral request builder in a server route, worker, or internal service; keep the provider client and credentials outside browser code.

Create the model at the provider boundary as described in Image generation models, then inject it into the workflow that owns the request.

Generate an image ​

ts
import { imageGenerationRequest } from '@anvia/core/image-generation'

const response = await imageGenerationRequest(imageModel)
  .prompt('A clean isometric illustration of a document review workflow')
  .width(1200)
  .height(800)
  .additionalParams({ output_format: 'png' })
  .send()

response.image is the first image as a Uint8Array. response.images contains every generated image as { data, mediaType? }. The response also preserves the raw provider response for integration-specific metadata.

additionalParams(...) is deliberately provider-specific. Keep values such as format, quality, style, seed, or safety options in typed application configuration instead of scattering them throughout product code.

Store the generated assets ​

Generated bytes should normally move directly into application-owned media storage:

ts
const assets = await Promise.all(
  response.images.map((image, index) =>
    mediaStore.put({
      bytes: image.data,
      mediaType: image.mediaType ?? response.mediaType ?? 'image/png',
      metadata: {
        index,
        provider: imageModel.provider ?? 'unknown',
        model: imageModel.defaultModel ?? 'unknown',
      },
    }),
  ),
)

mediaStore represents your object-storage or media-service adapter, not an Anvia primitive. Return an asset ID or signed URL to the UI. Do not place the byte array in agent memory, an event stream, or a product database row.

Validate before generation ​

Treat dimensions, format, prompt length, and the number of outputs as product inputs. Validate them before the provider request, apply your content policy before storing or publishing an asset, and avoid recording sensitive prompts in traces.

Image generation is often too slow or expensive for an interactive HTTP request. Put bulk generation and retryable user jobs behind a worker, and make the job idempotent so a retry does not publish duplicate assets.

Built for Anvia.