Light Dark

Types

AspectRatio

GenerateImageRequest

GenerateImageRequest type {
    prompt: Str,
    numberOfImages: Int?,
    aspectRatio: AspectRatio?,
    negativePrompt: Str?,
    safetyFilterLevel: SafetyFilterLevel?,
    personGeneration: PersonGeneration?
}

GenerateImageResponse

GenerateImageResponse type {
    predictions: Vec?
}

ImagePrediction

ImagePrediction type {
    bytesBase64Encoded: Str,
    mimeType: Str
}

PersonGeneration

SafetyFilterLevel

Functions

generate

fn (request: GenerateImageRequest): GenerateImageResponse | HttpError

Generate images using Imagen model.

Returns a list of generated images as base64-encoded data.

Example

response generate({
    prompt: "A serene lake at sunset",
    numberOfImages: 2,
    aspectRatio: "16:9"
})

images response.predictions

generate-batch-to-files

fn (directory: Str, request: GenerateImageRequest): AIMediaBatch | HttpError

Generate multiple images and save them to files.

Returns an AIMediaBatch with all generated AIMedia.Image items.

Example

result generate-batch-to-files("output", {
    prompt: "A magical forest",
    numberOfImages: 3,
    aspectRatio: "1:1"
})

println(`Generated ${result.succeeded} images`)

generate-inline

fn (prompt: Str): Map | HttpError

Generate images using Gemini 2.0 Flash's native image generation.

This uses the generateContent API with responseModalities set to IMAGE, allowing image generation within the chat context.

Example

response generate-inline("Draw a cute robot watering plants")

// Response contains inline_data parts with base64 images
parts response.candidates[0].content.parts
images filter(parts, (p) { not(is-null(p.inlineData)) })

generate-inline-to-file

fn (path: Str, prompt: Str): AIMedia | HttpError

Generate an image using Gemini 2.0 Flash and save it to a file.

Uses native image generation in the chat context.

Example

result generate-inline-to-file("output/robot.png", "Draw a cute robot")

match result {
    AIMedia.Image => { println(result.media.file.path) }
    HttpError => { println("Error!") }
}

generate-to-file

fn (path: Str, prompt: Str): AIMedia | HttpError
fn (path: Str, request: GenerateImageRequest): AIMedia | HttpError

Generate an image and save it to a file.

Returns an AIMedia.Image with file metadata and AI generation details.

Example

result generate-to-file("uploads/sunset.png", "A serene lake at sunset")

match result {
    AIMedia.Image => {
        println(result.media.file.path)  // "uploads/sunset.png"
        println(result.prompt)           // "A serene lake at sunset"
    }

    HttpError => { println("Error!") }
}

With options

result generate-to-file("output/city.png", {
    prompt: "A futuristic city at night",
    aspectRatio: "16:9",
    negativePrompt: "blurry, low quality"
})