Generate 16:9 Images with xAI Grok Imagine API

Written by

in

Generating 16:9 Widescreen Images with Grok Imagine on the xAI API

xAI’s image generation endpoint (/v1/images/generations) lets developers create widescreen visuals from text prompts using Grok Imagine models—most notably grok-imagine-image-2.0 (as well as grok-imagine-image)—with explicit control over framing via aspect_ratio: "16:9". Setting aspect_ratio to "16:9" targets widescreen canvases such as video frames, landscape banners, and wide editorial scenes rather than letting the model guess dimensions with auto.

How aspect_ratio: "16:9" Works in /v1/images/generations

When you omit aspect_ratio, the API defaults to auto, letting the model pick a framing based on your prompt. Passing "16:9" locks the composition into a 16:9 widescreen ratio—useful when your UI, player, or hero layout expects horizontal space.

Across xAI’s documentation and SDKs, 16:9 appears in two slightly different casing conventions depending on the client:

  • REST API and OpenAI-compatible / xAI Python SDKs: Use snake_case aspect_ratio: "16:9" when calling /v1/images/generations, client.images.generate(...), or client.image.sample(...).
  • Vercel AI SDK (@ai-sdk/xai): Uses camelCase aspectRatio: "16:9" inside generateImage(...).

Model note (as of November 2026): grok-imagine-image-2.0 is xAI’s current flagship image generation model, supporting aspect_ratio, batch generation via n (1–10), resolution (1K or 2K), and quality (low, medium, or auto). Meanwhile, the older grok-imagine-image-quality slug retired on November 2, 2026, and redirects to grok-imagine-image-2.0 at low quality.

Practical Code Examples

Python (xAI SDK)

import xai_sdk

client = xai_sdk.Client()

response = client.image.sample(
    prompt="Mountain landscape at sunrise",
    model="grok-imagine-image-2.0",
    aspect_ratio="16:9",
)

print(response.url)

Python (OpenAI SDK pointed at [https://api.x.ai/v1](https://api.x.ai/v1))

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.x.ai/v1",
)

response = client.images.generate(
    model="grok-imagine-image-2.0",
    prompt="Mountain landscape at sunrise",
    aspect_ratio="16:9",
)

print(response.data[0].url)

TypeScript / JavaScript (AI SDK)

import { xai } from "@ai-sdk/xai";
import { generateImage } from "ai";

const { image } = await generateImage({
  model: xai.image("grok-imagine-image-2.0"),
  prompt: "Mountain landscape at sunrise",
  aspectRatio: "16:9",
});

console.log(image.base64);

Key Parameters to Pair with 16:9

Parameter What It Controls Practical Behavior
aspect_ratio Output framing ("16:9", "9:16", "1:1", "4:3", "auto", etc.) Defaults to "auto"; set "16:9" for widescreen compositions.
n Number of variations (1–10) Defaults to 1 on REST/OpenAI SDKs; use sample_batch(n=...) in the xAI Python SDK for multiple images.
resolution Output resolution tier (1K or 2K) Lets you request higher-resolution output when needed.
quality Generation quality ("low", "medium", "auto") Supported on grok-imagine-image-2.0; "auto" defaults to "low" for generation and "medium" for edits.
response_format Return format ("url" or "base64") Defaults to temporary hosted URLs ("url"); use base64 to embed directly.
deferred Async execution (true) Returns a request_id immediately so you can poll GET /v1/images/{request_id} (results retained for 24 hours; "url" format only).

Tips for Cleaner Widescreen Results

  • Compose for the horizontal frame: Describe foreground, midground, and background depth across the horizontal axis rather than stacking everything vertically.
  • Pin aspect_ratio in production: Use "auto" while exploring ideas, then lock "16:9" in production so layout proportions stay predictable.
  • Use deferred: true for heavy batches: If generating multiple variations or high-resolution assets in bulk, deferred mode avoids holding synchronous connections open.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *