Controlling Aspect Ratios in xAI’s Grok Imagine Image Generations API
If you searched for aspect_ratio and 50:11 in xAI’s Grok Imagine documentation (docs.x.ai), you are looking at how xAI’s image generation endpoint (/v1/images/generations) frames outputs—and testing whether an extreme panoramic ratio like 50:11 (roughly 4.55:1) is supported out of the box.
In xAI’s documented Grok Imagine image generation API (centered on grok-imagine-image-2.0), aspect_ratio accepts auto plus a defined set of standard and widescreen/mobile ratios rather than arbitrary custom ratios like 50:11. Understanding how aspect_ratio works—and how to handle ultra-wide canvases when a preset ratio does not match—helps avoid failed requests or awkward crops.
How aspect_ratio Works on /v1/images/generations
xAI’s image generation API lets you generate images from text prompts using models such as grok-imagine-image-2.0, with support for batch sizes (n from 1 to 10), resolution presets (1k default, or 2k), quality settings (low, medium, or auto), and aspect_ratio.
When you omit aspect_ratio, it defaults to auto, allowing the model to select a ratio suited to your prompt. When you pass an explicit ratio, the model composes the scene to match that framing rather than simply cropping a square image after the fact.
Documented Aspect Ratios in Grok Imagine
According to xAI’s image generation documentation and Grok Imagine model specifications, supported values center on standard photography, screen, and banner ratios rather than bespoke fractions like 50:11:
| Ratio | Typical Use Case |
|---|---|
auto |
Lets the model pick the framing for the prompt (default) |
1:1 |
Square posts, avatars, and thumbnails |
16:9 / 9:16 |
Widescreen video/headers and vertical stories/reels |
4:3 / 3:4 |
Presentations, editorial slots, and portrait framing |
3:2 / 2:3 |
Classic camera and print proportions |
2:1 / 1:2 |
Wide web banners and tall vertical strips |
21:9 / 5:2 |
Cinematic ultrawide and wide banner layouts |
19.5:9 / 20:9 (and vertical 9:19.5 / 9:20) |
Modern tall smartphone screens and panoramic/full-length compositions |
(Note: Earlier variants like grok-imagine-image and grok-imagine-image-pro have been marked deprecated in favor of grok-imagine-image-2.0, and grok-imagine-image-quality was retired in November 2026.)
Why 50:11 Needs a Workaround
A 50:11 ratio is an extreme horizontal strip—more than twice as wide as 21:9 (2.33:1) and nearly double 5:2 (2.5:1). Because image generation APIs rely on validated ratio/dimension presets, passing an unvalidated string such as "50:11" directly to aspect_ratio is not a standard documented preset on docs.x.ai.
If your design system or LED/billboard canvas actually requires a 50:11 proportion:
- Generate at the closest wide preset (
21:9or5:2) at2kresolution: Requesting2kgives you higher pixel density to work with before any post-processing. - Compose with clean margins or extend the background: If the target is a
50:11banner, you can place the21:9or5:2artwork inside a wider50:11canvas with matching background color/gradient fill or outpainting/extension in your image pipeline—keeping key subjects away from extreme edges so nothing critical gets clipped. - Use the conversational Image Generation Tool when appropriate: In chat or agentic workflows using the
image_generationtool rather than direct endpoint parameters, the model chooses framing based on the prompt context, though direct API calls on/v1/images/generationsgive you explicit parameter control (aspect_ratio,resolution,quality,n,response_format).
Code Examples Using grok-imagine-image-2.0
xAI’s API is compatible with OpenAI-style image generation clients as well as xAI’s own SDKs:
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="Wide architectural view of a glass train station at dawn, clean lines",
aspect_ratio="21:9",
resolution="2k",
n=1,
)
print(response.data[0].url)
xAI Python SDK
import xai_sdk
client = xai_sdk.Client()
result = client.image.sample(
model="grok-imagine-image-2.0",
prompt="Wide architectural view of a glass train station at dawn, clean lines",
aspect_ratio="21:9",
)
print(result.url)
cURL (POST /v1/images/generations)
curl 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": "Wide architectural view of a glass train station at dawn, clean lines",
"aspect_ratio": "21:9",
"resolution": "2k",
"response_format": "url"
}'
Practical Tips for Production Workflows
- Download URLs promptly or use
base64: Default URL responses are temporary; fetch and store assets in durable object storage, or setresponse_formatto"base64"when embedding directly. - Use
deferred: trueor webhooks for larger jobs: For slower or batch workloads on/v1/images/generations,deferred: truereturns arequest_idto poll viaGET /v1/images/{request_id}(results retained for 24 hours with URL output), and webhook events (image.generation.completed,image.generation.failed) avoid open-connection timeouts. - Tune
qualityandresolutionfor cost vs. detail: Use1kandlowquality for rapid prompt iteration, then step up to2kandmedium(orauto) for final assets.
Leave a Reply