Generating images through the xAI API centers on the /v1/images/generations endpoint and Grok’s image models (grok-imagine-image and grok-imagine-image-2.0). Among the available request parameters, aspect_ratio controls the framing and proportions of generated outputs before pixels are rendered, letting developers target mobile screens, widescreen layouts, print-like frames, or banners without post-hoc cropping.
How aspect_ratio Works on /v1/images/generations
When you send a POST request to [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations), aspect_ratio defines the width-to-height ratio of the resulting image.
- Default behavior (
auto): If you omitaspect_ratioor pass"auto", the model selects the framing it judges best suited to the prompt. - Explicit framing: Passing a ratio string such as
"16:9","9:16","1:1", or"21:9"locks the composition to that shape. - Companion parameters:
aspect_ratiopairs withresolution(1kby default, or2k),quality(auto,low, ormediumongrok-imagine-image-2.0),n(1to10images per request), andresponse_format(urlfor temporary hosted links orb64_jsonfor inline base64 data).
Persistence tip: Default
urlresponses are temporary. Useb64_json(or download and store outputs immediately) whenever you need durable storage or plan to feed generated assets into downstream pipelines.
Supported Aspect Ratios and Target Use Cases
The xAI image generation documentation maps standard and device-specific aspect ratios to common layout targets:
| Aspect Ratio | Typical Use Cases |
|---|---|
1:1 |
Square social posts, avatars, product cards, and thumbnails |
16:9 / 9:16 |
Widescreen video/hero frames (16:9) and vertical mobile stories or feeds (9:16) |
4:3 / 3:4 |
Slide decks and presentations (4:3) and vertical editorial or portrait framing (3:4) |
3:2 / 2:3 |
Classic photographic proportions across horizontal and vertical layouts |
19.5:9 / 9:19.5 |
Modern tall smartphone screens such as iPhone displays |
20:9 / 9:20 |
Modern tall Android smartphone displays |
21:9 |
Ultrawide and cinematic widescreen compositions |
5:2 |
Wide horizontal banners and headers |
auto |
Automatic model selection based on prompt context |
Request Examples Across Clients
You can set aspect_ratio directly in raw HTTP calls, OpenAI-compatible clients, or xAI’s official Python SDK.
cURL (POST /v1/images/generations)
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 atrium with daylight streaming through skylights",
"aspect_ratio": "16:9",
"resolution": "2k",
"quality": "medium",
"response_format": "url",
"n": 1
}'
Python (xAI SDK)
import xai_sdk
client = xai_sdk.Client()
response = client.image.sample(
prompt="Minimalist architectural atrium with daylight streaming through skylights",
model="grok-imagine-image-2.0",
aspect_ratio="16:9",
)
print(response.url)
JavaScript / TypeScript (OpenAI-Compatible Client)
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: "Minimalist architectural atrium with daylight streaming through skylights",
aspect_ratio: "9:16",
});
console.log(response.data[0].url);
Practical Guidelines for Choosing Ratios
- Match framing to subject geometry: Wide panoramas, skylines, and architectural interiors breathe better in
16:9or21:9, whereas vertical posters, single-subject portraits, and mobile story cards fit3:4,9:16, or9:19.5. - Align prompt composition with the ratio: State camera distance, focal point, and what stays inside or outside the frame consistently so the model fills wide or tall borders naturally.
- Reserve
2kandmediumquality for detail-heavy outputs: Keep iteration loops fast and economical with1kandauto/lowquality while testing aspect ratios, then step up to2kandmediumfor final assets. - Use
deferred: truefor heavy batches: For large multi-image jobs or long-running generations where holding an open HTTP connection is undesirable, setdeferred: trueon/v1/images/generationsand pollGET /v1/images/{request_id}(or listen via webhooks).
Leave a Reply