Grok Imagine Image API: Aspect Ratio & Generations Guide

Written by

in

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 omit aspect_ratio or 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_ratio pairs with resolution (1k by default, or 2k), quality (auto, low, or medium on grok-imagine-image-2.0), n (1 to 10 images per request), and response_format (url for temporary hosted links or b64_json for inline base64 data).

Persistence tip: Default url responses are temporary. Use b64_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

  1. Match framing to subject geometry: Wide panoramas, skylines, and architectural interiors breathe better in 16:9 or 21:9, whereas vertical posters, single-subject portraits, and mobile story cards fit 3:4, 9:16, or 9:19.5.
  2. 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.
  3. Reserve 2k and medium quality for detail-heavy outputs: Keep iteration loops fast and economical with 1k and auto/low quality while testing aspect ratios, then step up to 2k and medium for final assets.
  4. Use deferred: true for heavy batches: For large multi-image jobs or long-running generations where holding an open HTTP connection is undesirable, set deferred: true on /v1/images/generations and poll GET /v1/images/{request_id} (or listen via webhooks).

Comments

Leave a Reply

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