Framing is half the composition—and in programmatic image generation, aspect_ratio is how you control the frame before a single pixel is rendered. In xAI’s Grok image generation documentation (docs.x.ai), the /v1/images/generations endpoint and Grok Imagine models (such as grok-imagine-image-2.0) pair explicit framing controls with batching, resolution, and quality settings so developers can match layouts across feeds, screens, and banners without post-crop distortion.
How aspect_ratio Works in Grok Image Generations
When generating images through xAI’s image generation API or SDKs, aspect_ratio sets the proportions of the output canvas:
- Default
autobehavior: Omittingaspect_ratio(or passing"auto") lets the model select the ratio that best fits the prompt’s subject and implied composition. - Explicit framing: Passing a ratio string such as
"16:9","3:2", or"9:19.5"locks the output proportions to match your target container. - Direct endpoint vs. conversational tool: Calling
/v1/images/generationsdirectly (or viaclient.image.sample()/sample_batch()or OpenAI-compatible SDKs) gives explicit parameter control overaspect_ratio,resolution(1kdefault or2k),quality(low,medium, orautoongrok-imagine-image-2.0),n(1–10),response_format(urlorb64_json), anddeferredasync execution. By contrast, when using Grok’s built-inimage_generationtool inside a chat/agentic loop, the model chooses the framing automatically unless you specify the desired ratio in natural language.
Supported Aspect Ratios and Practical Use Cases
The documentation highlights standard photographic, social, banner, cinematic, and modern mobile display ratios alongside auto:
| Aspect Ratio | Typical Use Case |
|---|---|
1:1 |
Social media posts, thumbnails |
16:9 / 9:16 |
Widescreen layouts, mobile screens, stories |
4:3 / 3:4 |
Presentations, portraits |
3:2 / 2:3 |
Classic photography framing |
2:1 / 1:2 |
Banners, headers |
19.5:9 / 9:19.5 |
Modern smartphone displays (iPhone) |
20:9 / 9:20 |
Modern smartphone displays (Android) |
21:9 |
Cinematic widescreen |
5:2 |
Wide banners |
auto |
Model selects the best ratio for the prompt |
Code Examples Across Clients
REST (cURL)
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": "Minimalist architectural courtyard at golden hour",
"aspect_ratio": "3:2",
"resolution": "2k",
"quality": "medium",
"response_format": "b64_json",
"n": 1
}'
xAI Python SDK
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.image.sample(
prompt="Mountain landscape at sunrise",
model="grok-imagine-image-2.0",
aspect_ratio="16:9",
)
if response.respect_moderation:
print(response.url)
else:
print("Image filtered by moderation")
For multi-image batches in the xAI Python SDK, use client.image.sample_batch(n=...) rather than sample().
OpenAI-Compatible SDK (JavaScript / TypeScript)
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);
Tips for Reliable Production Results
- Match ratio to subject and container: Pair tall portraits or vertical mobile UI slots with ratios like
3:4,9:16,9:19.5, or9:20, and reserve16:9,21:9, or5:2for wide scenes and headers so key subjects are not awkwardly cramped. - Lead with subject clarity in prompts: Per xAI’s image prompting guidance, prioritize naming the exact subject, visible elements, counts, and lighting before styling or film grain so the generated image accurately fulfills the prompt.
- Use
deferred: truefor large or slow batches: When generating larger batches or avoiding long-held HTTP connections on/v1/images/generations, setdeferred: trueto receive arequest_idimmediately, pollGET /v1/images/{request_id}(or listen toimage.generation.completed/image.generation.failedwebhooks), and retrieve results within the 24-hour retention window usingresponse_format: "url".
Leave a Reply