Grok Imagine API: Aspect Ratio & Image Quality Guide

Written by

in

Framing and Fidelity in Grok Image Generation

xAI’s image generation endpoint (/v1/images/generations) centers on grok-imagine-image-2.0, exposing explicit controls for framing, output dimensions, and rendering quality alongside batching and asynchronous execution. Two parameters in particular—aspect_ratio and quality (paired with resolution)—govern how a prompt becomes a finished visual and how much you pay per image.


Controlling Dimensions with aspect_ratio

The aspect_ratio parameter sets the width-to-height proportion of generated images. When omitted, it defaults to auto, allowing the model to pick a ratio suited to the prompt. Passing an explicit string locks the canvas shape to match your target layout, whether that is a square social post, a vertical mobile screen, or a wide banner.


Aspect Ratio Typical Use Cases
1:1 Social media posts and thumbnails
16:9 / 9:16 Widescreen displays, mobile screens, and stories
4:3 / 3:4 Presentations and portraits
3:2 / 2:3 Photography compositions
2:1 / 1:2 Banners and 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 ratio for the prompt

When using the conversational image_generation tool rather than calling the image endpoint directly, there is no aspect_ratio parameter; instead, you state the desired ratio in your prompt text (such as “in a 9:16 vertical aspect ratio”).


Tuning quality and resolution on grok-imagine-image-2.0

The optional quality parameter accepts low, medium, or auto, and is supported only on grok-imagine-image-2.0. When omitted, it defaults to auto, letting the service choose the quality per request.

  • Current auto behavior: auto currently serves low for image generation and medium for image editing.
  • Billing alignment: Images are billed at the quality level at which they are served, so pinning low or medium gives predictable cost control alongside fidelity control.
  • Pairing with resolution: The resolution parameter supports 1k (default when omitted) and 2k. For scenes where fine textures, small objects, or intricate details must stay recognizable, xAI’s prompting guidance recommends pinning medium quality together with 2k resolution.

Practical Request Patterns

Across the OpenAI-compatible client, the xAI Python SDK, and the Vercel AI SDK, you pass aspect_ratio (or aspectRatio in JS/TS AI SDK) and quality right alongside model and prompt:

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="Overhead view of two ceramic mugs and an open notebook on a pale oak desk in bright morning light",
    aspect_ratio="3:2",
    quality="medium",
    resolution="2k",
)

print(response.data[0].url)

For production pipelines, keep three operational details in mind:

  • Storage: Default url responses are temporary, so request b64_json (or save via Files API outputs) when you need persistent storage.
  • Batching and scale: Request n from 1 to 10 images in a single call, use AsyncClient with asyncio.gather for distinct prompts, or set deferred: true for asynchronous polling (which returns url outputs retained for 24 hours).
  • Prompt clarity: Name concrete subjects, counts, lighting, and camera angles directly rather than relying on defect terms like dust or scratches, which the model can render literally.

Comments

Leave a Reply

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