Set Aspect Ratio in xAI Grok Imagine Image Generation API

Written by

in

Framing the Canvas: Mastering aspect_ratio in xAI’s grok-imagine-image API

When generating visuals programmatically, the difference between an amateur output and a production-ready asset often comes down to framing. A stunning landscape crushed into a square or a mobile wallpaper awkwardly cropped from a widescreen render forces developers into messy post-processing pipelines. Within xAI’s developer documentation (docs.x.ai), the /v1/images/generations endpoint solves this at the root through a single, high-leverage parameter: aspect_ratio.

Designed for the grok-imagine-image model family—most notably the flagship grok-imagine-image-2.0—this parameter dictates the exact geometric proportions of the generated canvas before a single pixel is synthesized.


The Supported Aspect Ratio Matrix

Rather than forcing developers to calculate arbitrary pixel dimensions (which often triggers artifacts or tiling in diffusion and autoregressive image models), xAI standardizes framing around curated ratios tailored to real-world media formats.

aspect_ratio Value Orientation Primary Use Cases
auto (Default) Dynamic Lets the model infer the best framing directly from the prompt context.
1:1 Square Social media feeds, profile avatars, album covers, and thumbnails.
16:9 / 9:16 Widescreen / Vertical YouTube thumbnails, desktop wallpapers, mobile stories, and short-form video backdrops.
4:3 / 3:4 Standard / Portrait Slide presentations, editorial portraits, and tablet displays.
3:2 / 2:3 Classic Photo DSLR-style photography, print layouts, and physical posters.
19.5:9 / 9:19.5 Tall Mobile Full-bleed lock screens and UI mockups for modern iPhone displays.
20:9 / 9:20 Tall Mobile Full-bleed lock screens and UI mockups for modern Android displays.
21:9 Ultrawide Cinematic film stills and ultrawide monitor headers (exclusive to 2.0).
5:2 Panoramic Banner Website hero headers and ultra-wide digital signage (exclusive to 2.0).

Model Evolution Note: Following the retirement of the legacy grok-imagine-image-quality slug in November 2026, requests automatically route to grok-imagine-image-2.0. Upgrading your model string to grok-imagine-image-2.0 unlocks the ultra-wide 21:9 and 5:2 ratios alongside explicit quality (low, medium, auto) and resolution (1k, 2k) controls.


Implementing aspect_ratio Across SDKs

xAI maintains compatibility with both its native SDK and the standard OpenAI client schema, making integration straightforward regardless of your existing stack.

1. Direct REST API (cURL)

When hitting [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations) directly, pass aspect_ratio as a top-level string in your JSON payload:

curl -X POST https://api.x.ai/v1/images/generations 
  -H "Authorization: Bearer $XAI_API_KEY" 
  -H "Content-Type: application/json" 
  -d '{
    "model": "grok-imagine-image-2.0",
    "prompt": "A minimalist brutalist concrete library bathed in golden hour sunlight, 35mm lens",
    "aspect_ratio": "3:2",
    "resolution": "2k",
    "quality": "medium",
    "n": 1
  }'

2. Native xAI Python SDK

Using xai_sdk, aspect_ratio is passed cleanly into client.image.sample() (for single images) or client.image.sample_batch() (for up to 10 images at once):

import xai_sdk

client = xai_sdk.Client()

response = client.image.sample(
    prompt="Neon-lit rain puddles reflecting a cyberpunk street market in Tokyo",
    model="grok-imagine-image-2.0",
    aspect_ratio="21:9",
)

print(response.url)

3. OpenAI-Compatible Python Client

Because xAI mirrors OpenAI’s /v1/images/generations endpoint structure, teams migrating existing codebases only need to swap the base_url and pass aspect_ratio:

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="Isometric cutaway illustration of a cozy subterranean greenhouse",
    aspect_ratio="4:3",
    n=1,
)

print(response.data[0].url)

How aspect_ratio Behaves in Image Edits

While /v1/images/generations defaults to aspect_ratio: "auto", xAI’s sister endpoint—/v1/images/edits—handles proportions differently:

  • Inheritance by Default: When editing one or up to five reference images on grok-imagine-image-2.0, omitting aspect_ratio instructs the model to inherit the exact dimensions of the first input image.
  • Intentional Reframing: Explicitly passing aspect_ratio in an edit request overrides the source image’s proportions. This allows you to outpaint or recompose a vertical 9:16 portrait into a 16:9 widescreen banner while preserving the original subject’s identity.

Best Practices for Production Workflows

  1. Lock Ratios Across Visual Series: When generating storyboards, product catalogs, or multi-slide decks, never leave aspect_ratio on auto. Pinning a consistent ratio (such as 3:2 for editorial photography or 16:9 for presentation slides) ensures visual cohesion without layout shifts.
  2. Match Composition Cues to Canvas Shape: Aspect ratio changes how the model distributes subjects. When requesting 21:9 or 5:2, use spatial language in your prompt (

Comments

Leave a Reply

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