xAI Grok Image API: Aspect Ratio Guide for Image Generation

Written by

in

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 auto behavior: Omitting aspect_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/generations directly (or via client.image.sample() / sample_batch() or OpenAI-compatible SDKs) gives explicit parameter control over aspect_ratio, resolution (1k default or 2k), quality (low, medium, or auto on grok-imagine-image-2.0), n (1–10), response_format (url or b64_json), and deferred async execution. By contrast, when using Grok’s built-in image_generation tool 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, or 9:20, and reserve 16:9, 21:9, or 5:2 for 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: true for large or slow batches: When generating larger batches or avoiding long-held HTTP connections on /v1/images/generations, set deferred: true to receive a request_id immediately, poll GET /v1/images/{request_id} (or listen to image.generation.completed / image.generation.failed webhooks), and retrieve results within the 24-hour retention window using response_format: "url".

Comments

Leave a Reply

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