Grok Imagine Image API: Aspect Ratio & Generations Guide

Written by

in

Mastering aspect_ratio in xAI’s grok-imagine-image API

When generating images programmatically via the xAI API (docs.x.ai), getting the composition right before you ever refine a prompt starts with a single parameter: aspect_ratio. While early image-generation endpoints forced developers to calculate exact pixel dimensions—often resulting in awkward cropping or distorted subjects—xAI’s grok-imagine-image model abstracts canvas geometry into intuitive, standardized ratios on the /v1/images/generations endpoint.

Here is a complete guide to how aspect_ratio works in grok-imagine-image, the supported values, and how to integrate it cleanly into production workflows.


How aspect_ratio Works on /v1/images/generations

Instead of passing rigid width and height integers, the xAI image generation endpoint accepts a string value for aspect_ratio. Behind the scenes, grok-imagine-image maps that ratio to an optimal native resolution grid, ensuring that subject framing, focal depth, and edge details are composed for that specific canvas shape rather than stretched or center-cropped after generation.

If you omit the aspect_ratio parameter from your JSON payload, the API defaults to a standard 1:1 square canvas.

Supported Aspect Ratios & Use Cases

aspect_ratio Value Orientation Common Real-World Equivalents Best Use Cases
"1:1" (Default) Square Instagram feed posts, album art, avatars Product thumbnails, profile icons, symmetrical compositions
"16:9" Landscape (Widescreen) YouTube thumbnails, HD/4K monitors, hero banners Website headers, presentation slides, cinematic establishing shots
"9:16" Portrait (Vertical) TikTok, Instagram Reels, YouTube Shorts, mobile wallpapers Mobile-first full-screen media, vertical story ads
"4:3" Landscape (Classic) Traditional photography, iPad screens, standard displays Editorial illustrations, blog post inline graphics
"3:4" Portrait (Classic) Print magazines, digital posters, portrait photography Character portraits, book covers, vertical flyers
"3:2" Landscape (35mm) DSLR cameras, classic photo prints Documentary-style scenes, wide environmental photography
"2:3" Portrait (35mm) Vertical DSLR framing, Pinterest pins Fashion lookbooks, vertical architectural shots
"21:9" Ultrawide Anamorphic cinema, ultrawide desktop monitors Panoramic landscapes, dramatic sci-fi or widescreen banners

(Note: Depending on your xAI API SDK version and endpoint updates, passing an unsupported ratio string will return a 400 Bad Request validation error listing the currently accepted enum values.)


Code Examples: Setting aspect_ratio

Because xAI maintains compatibility with standard REST conventions and the OpenAI SDK ecosystem—while also offering its own native xai-sdk—you can specify aspect_ratio cleanly in either Python or cURL.

1. Python (Using the Official xai_sdk)

import os
from xai_sdk import Client

client = Client(api_key=os.getenv("XAI_API_KEY"))

response = client.image.sample(
    model="grok-imagine-image",
    prompt="A sleek brutalist observatory perched on a snowy mountain ridge at twilight, volumetric fog, 35mm photography",
    aspect_ratio="16:9",
    image_format="url"  # or "base64"
)

print(f"Generated Image URL: {response.url}")

2. Python (Using the OpenAI-Compatible Client)

If you are routing requests through the openai Python library pointed at [https://api.x.ai/v1](https://api.x.ai/v1), pass aspect_ratio inside extra_body so the client forwards the parameter directly to xAI’s servers:

import os
from openai import OpenAI

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

response = client.images.generate(
    model="grok-imagine-image",
    prompt="Minimalist studio macro shot of a mechanical watch movement, dramatic rim lighting",
    n=1,
    extra_body={
        "aspect_ratio": "4:3"
    }
)

print(response.data[0].url)

3. Direct REST Call via cURL

curl https://api.x.ai/v1/images/generations 
  -H "Content-Type: application/json" 
  -H "Authorization: Bearer $XAI_API_KEY" 
  -d '{
    "model": "grok-imagine-image",
    "prompt": "Cyberpunk street food stall in Seoul during a warm summer rainstorm, neon reflections on wet asphalt",
    "aspect_ratio": "9:16",
    "n": 1
  }'

Prompting Tips for Different Aspect Ratios

Changing aspect_ratio does more than resize the frame—it changes how grok-imagine-image interprets spatial language in your prompt:

  1. Match Camera Language to the Frame: Pair "16:9" or "21:9" with cues like wide-angle establishing shot, panoramic horizon, or rule-of-thirds composition. Conversely, pair "9:16" or "2:3" with full-body portrait, low-angle vertical perspective, or tall architectural framing.
  2. Avoid Fighting the Canvas: Asking for a “full head-to-toe standing portrait” inside a "21:9" ultrawide frame forces the model to either shrink the subject into a tiny speck or awkwardly crop the figure. Let your UI layout dictate the aspect_ratio, then write the prompt to fill that geometry naturally.
  3. Standardize Ratios in Multi-Asset Pipelines: When generating a batch of images for a carousel, storyboard, or web grid (n > 1), lock aspect_ratio as a constant at the application layer rather than letting users type dimensions into the prompt text—grok-imagine-image ignores aspect-ratio hints written inside the prompt string in favor of the explicit aspect_ratio API parameter.

Comments

Leave a Reply

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