Grok Imagine Image API: Aspect Ratio & Generations Guide

Written by

in

Framing Images with grok-imagine-image-2.0 and aspect_ratio

Generating images programmatically through xAI’s /v1/images/generations endpoint with grok-imagine-image-2.0 gives you explicit control over framing, resolution, batch size, and output quality. Among those controls, the aspect_ratio parameter shapes the canvas before a single pixel is rendered—aligning your output with social feeds, widescreen displays, photography presets, or mobile screens without post-hoc cropping.


How aspect_ratio Works on /v1/images/generations

When you send a request to [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations) using grok-imagine-image-2.0, aspect_ratio sets the proportional dimensions of the generated image.

  • Default (auto): If you omit aspect_ratio or pass "auto", the model selects the aspect ratio it judges best suited to your prompt.
  • Explicit framing: Passing a ratio string such as "16:9", "3:2", or "9:19.5" locks the geometry to that target format.
  • Agentic tool caveat: If you instead invoke Grok’s conversational image_generation tool (for example alongside a chat model), the model chooses the ratio dynamically unless you specify the desired ratio in natural language inside the prompt itself.

Supported Aspect Ratios and Practical Use Cases

xAI’s image generation documentation covers standard photographic, screen, and banner ratios alongside auto:

Aspect Ratio Typical Use Cases
1:1 Social media posts, square thumbnails
16:9 / 9:16 Widescreen video/headers, vertical mobile stories
4:3 / 3:4 Presentations, classic portraits
3:2 / 2:3 Photography, editorial stills
2:1 / 1:2 Wide banners, tall split layouts
19.5:9 / 9:19.5 Modern iPhone-style smartphone displays
20:9 / 9:20 Modern Android-style smartphone displays
21:9 Cinematic ultrawide framing
5:2 Extra-wide web or hero banners
auto Automatic ratio selection based on prompt context

Code Examples Across SDKs and REST

You can pass aspect_ratio directly via curl, the OpenAI-compatible SDK, the xAI Python SDK, or the Vercel AI SDK (aspectRatio in camelCase).

REST API (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": "Overhead shot of a ceramic pour-over coffee dripper on a pale oak table in morning light",
    "aspect_ratio": "3:2",
    "resolution": "2k",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }'

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="Mountain landscape at sunrise",
    aspect_ratio="16:9",
)

print(response.data[0].url)

Python (xai_sdk)

import xai_sdk

client = xai_sdk.Client()
response = client.image.sample(
    prompt="Mountain landscape at sunrise",
    model="grok-imagine-image-2.0",
    aspect_ratio="16:9",
)

print(response.url)

TypeScript / JavaScript (AI SDK)

import { xai } from "@ai-sdk/xai";
import { generateImage } from "ai";

const { image } = await generateImage({
  model: xai.image("grok-imagine-image-2.0"),
  prompt: "Mountain landscape at sunrise",
  aspectRatio: "16:9",
});

console.log(image.base64);

Pairing aspect_ratio with Resolution, Quality, and Delivery Options

Getting crisp framing at scale depends on combining aspect_ratio with the companion parameters on /v1/images/generations:

  • Resolution (1k or 2k): Defaults to 1k when omitted; set resolution: "2k" when higher pixel detail is required.
  • Quality (low, medium, or auto): Supported on grok-imagine-image-2.0. auto currently defaults to low for generations and medium for edits, while pinning "medium" alongside "2k" helps preserve fine details.
  • Batch count (n): Generate between 1 and 10 images per request on the REST API or OpenAI-compatible SDKs (default 1), or use sample_batch(n=...) in the xAI Python SDK.
  • Output format & persistence: response_format defaults to "url" (temporary hosted URLs); request "b64_json" when you want inline base64 data to save or embed immediately. For large batches or non-blocking workflows, set deferred: true and poll GET /v1/images/{request_id} (which returns "url" results retained for 24 hours) or listen via webhooks (image.generation.completed, image.generation.failed).

Prompting Tips That Match Your Aspect Ratio

A chosen aspect_ratio works best when your prompt’s spatial description matches that frame:

  1. Name concrete subjects and counts: State exact objects, materials, and quantities (for example, “one ceramic mug” rather than vague labels).
  2. Keep camera angle and composition consistent: If you pick a wide 16:9 or 21:9 ratio, describe horizontal staging and what the frame cuts off; if you pick 9:16 or 9:19.5, frame vertical subjects so key elements are not awkwardly clipped.
  3. Separate light and color: Specify light direction/hardness and distinct subject vs. background colors so high-contrast framing stays legible.

Comments

Leave a Reply

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