Generating 16:9 Widescreen Images with xAI’s grok-imagine-image API
Generating widescreen visuals programmatically is a staple of modern media pipelines, video pre-visualization, UI hero banners, and slide decks. In xAI’s Grok API documentation, the Image Generation workflow (grok-imagine-image and grok-imagine-image-2.0) exposes clean controls for framing, resolution, quality, and batching—including explicit 16:9 widescreen support across SDKs and REST endpoints.
How Aspect Ratio & Image Generation Work
When you call xAI’s image generation endpoint (/v1/images/generations) or use a supported SDK wrapper, you pass a prompt, a model (grok-imagine-image or grok-imagine-image-2.0), and framing/output parameters:
- Aspect ratio (
aspect_ratio): Setting"16:9"produces standard widescreen compositions well suited for 16:9 video frames, landscape banners, and desktop headers. When omitted, the API defaults to"auto", allowing the model to pick a ratio based on your prompt. Recent release notes also highlight wider options such as"21:9"(cinematic widescreen) and"5:2"(wide banners) alongside standard ratios. - Quality & resolution (
quality,resolution): Ongrok-imagine-image-2.0,qualitysupports"low","medium", and"auto"(which defaults to"low"for generation and"medium"for editing), while provider options in SDKs can also target higher resolutions such as"2k". (Note that the legacygrok-imagine-image-qualityslug was retired on November 2, 2026, routing togrok-imagine-image-2.0withqualityset to"low".) - Batching & formats (
n,response_format): You can request multiple images in one call (nfrom 1 to 10 on REST/OpenAI-compatible clients, orsample()vs.sample_batch(n=...)in the xAI Python SDK) and receive either hosted URLs (url, the default) or base64 data (b64_json/base64). - Async & webhooks (
deferred): For large batches or long-running jobs where you do not want to keep a connection open, settingdeferred: truereturns arequest_idimmediately so you can pollGET /v1/images/{request_id}(retained for 24 hours withurlformat) or listen for webhook events likeimage.generation.completedandimage.generation.failed.
Code Examples Across SDKs
xAI Python SDK (xai_sdk)
Use client.image.sample() for a single widescreen image and pass aspect_ratio="16:9":
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)
If you need base64 or raw bytes saved directly to disk, use image_format="base64" and write response.image. For multiple distinct prompts, firing concurrent requests via AsyncClient with asyncio.gather is faster than sequential calls.
OpenAI-Compatible Client (Python / JS)
Because xAI provides an OpenAI-compatible base URL ([https://api.x.ai/v1](https://api.x.ai/v1)), you can call client.images.generate(...) with aspect_ratio: "16:9":
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: "https://api.x.ai/v1",
});
const response = await client.images.generate({
model: "grok-imagine-image-2.0",
prompt: "Mountain landscape at sunrise",
aspect_ratio: "16:9",
});
console.log(response.data[0].url);
Vercel AI SDK (@ai-sdk/xai)
Using generateImage from ai with xai.image(...), pass aspectRatio: "16:9" (and optional providerOptions such as { xai: { resolution: "2k" } }):
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);
Direct Endpoint vs. Conversational Image Tool
If you are building an agentic chat workflow rather than a deterministic asset pipeline, xAI also offers a built-in image_generation tool in chat/Responses APIs. The key difference is control:
| Approach | Best For | Aspect Ratio Control |
|---|---|---|
Image Generation API (/v1/images/generations & SDK image methods) |
Deterministic pipelines, exact framing (16:9), explicit quality/resolution, batching, and deferred jobs |
Explicit aspect_ratio / aspectRatio parameter ("16:9", "auto", etc.) |
Chat image_generation Tool |
Multi-turn conversations where the model decides when to generate or edit images and chains tool calls | Model selects framing automatically; specify desired framing (such as "16:9" or "9:16") in natural language |
When you already know your exact prompt and need predictable 16:9 widescreen output—such as generating keyframes before video generation or populating widescreen UI slots—calling the image generation API directly with aspect_ratio="16:9" gives you immediate, repeatable control.
Leave a Reply