Skip to content

Image generation ​

generateImage() sends a prompt to an ImageGenerationModel and returns normalized image bytes.

1. Generate an image ​

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

const response = await generateImage({
    prompt: 'A clean isometric illustration of a document review workflow',
    model: imageModel,
    width: 1200,
    height: 800,
    providerOptions: {
        output_format: 'png',
    },
    retries: {
        maxAttempts: 3,
    }
})

Width and height default to 1024 and must be positive safe integers. A provider may impose a smaller list of supported dimensions.

response.images[0].data is the first image as Uint8Array. response.images contains all normalized outputs as { data, mediaType? }. rawResponse preserves provider-specific data.

2. Keep provider options at the adapter boundary ​

providerOptions may contain format, quality, style, seed, or safety settings understood by the provider. Keep them in typed, allow-listed application configuration rather than accepting an arbitrary request object.

3. Store generated bytes immediately ​

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

mediaStore is an application adapter. Return asset IDs or signed URLs to clients instead of storing image bytes in agent memory, traces, or database rows.

4. Validate and moderate ​

Validate prompt length, dimensions, count, and format before calling the provider. Apply product content policy before publishing an asset and avoid recording sensitive prompts unnecessarily.

Use a durable worker for bulk or expensive generation. Make publication idempotent so retries do not create duplicate assets.

Next, generate audio.

Built for Anvia.