Grok Imagine Image API: Aspect Ratio & Generations Guide

Written by

in

Controlling Aspect Ratios in Grok Image Generation (grok-imagine-image) via /v1/images/generations

xAI’s image generation endpoint (/v1/images/generations) lets developers generate visuals from text prompts using Grok image models—such as grok-imagine-image-2.0—while controlling framing via the aspect_ratio parameter. Rather than forcing developers to guess pixel dimensions or crop output after rendering, aspect_ratio sets the target proportions directly in the generation request.


How aspect_ratio Works in /v1/images/generations

When calling /v1/images/generations (or using the xAI SDK, OpenAI-compatible client, or Vercel AI SDK), aspect_ratio is passed alongside model and prompt.

  • Default behavior (auto): When aspect_ratio is omitted or set to auto, the service selects a framing ratio suited to the prompt.
  • Explicit framing: Passing a ratio string such as "16:9", "9:16", "1:1", or "3:4" locks the composition to that shape.
  • Pairing with resolution and quality: Aspect ratio controls proportions, while resolution (1k by default, or 2k) sets output scale and quality (low, medium, or auto on grok-imagine-image-2.0) governs quality selection.
  • Batching (n): You can request 1 to 10 images per call (n defaults to 1 on REST and OpenAI-compatible clients; the xAI Python SDK uses sample() for a single image and sample_batch(n=...) for batches).

Supported Aspect Ratios and Common Targets

Aspect Ratio Typical Use Cases
1:1 Social media feeds, square thumbnails, avatars
16:9 / 9:16 Widescreen landscapes, video covers, vertical stories/reels
4:3 / 3:4 Slide decks, editorial cards, portrait compositions
3:2 / 2:3 Classic photography framing, prints, vertical layouts
19.5:9 / 9:19.5 Modern tall smartphone displays (such as iPhone screens)
20:9 / 9:20 Ultra-tall mobile screens (such as Android displays)
21:9 / 5:2 Ultra-wide cinematic shots, banners, and headers

Code Examples Across SDKs

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-Compatible Client)

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)

JavaScript / TypeScript (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);

Practical Tips for Production Pipelines

  1. Match ratio to the container first: Specify aspect_ratio explicitly when rendering into fixed UI slots (like a 9:16 vertical feed or 16:9 hero banner) instead of relying on auto and post-cropping, which can clip key subjects.
  2. Use 2k deliberately: Leave resolution at 1k for fast iteration or thumbnails, and switch to 2k when high-detail output is needed.
  3. Handle large batches asynchronously: For heavy batch jobs or long-lived connections, use deferred: true on /v1/images/generations and poll GET /v1/images/{request_id} (which returns url outputs retained for 24 hours), or register webhooks for image.generation.completed and image.generation.failed. For distinct prompts, run concurrent requests with AsyncClient and asyncio.gather.

Comments

Leave a Reply

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