xAI Image Generation API: Aspect Ratio & Quality Guide

Written by

in

Framing and Fidelity in xAI’s Image Generations API: aspect_ratio and grok-imagine-image-quality

When building image-generation workflows on xAI’s Grok Imagine platform, two levers shape whether a generated asset fits your layout and meets your visual bar: aspect_ratio and quality control (including how grok-imagine-image-quality maps onto grok-imagine-image-2.0). Understanding how these parameters behave across /v1/images/generations and xAI’s SDKs helps avoid awkward crops, unexpected billing behavior, and brittle integrations.


How aspect_ratio Shapes Composition

On xAI’s image generation endpoint (POST [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations)) and SDKs, aspect_ratio (aspectRatio in JavaScript/AI SDK) controls the width-to-height proportions of the output image:

  • Default (auto): Omitting aspect_ratio (or setting "auto") lets the model select the ratio that best fits the prompt. This works well for open-ended creative prompts where strict UI or ad dimensions are not required.
  • Explicit ratios: When rendering into fixed containers—such as widescreen hero banners (16:9), vertical mobile/story slots (9:16), or square thumbnails (1:1)—passing an explicit ratio prevents post-generation cropping that could slice off subjects or text.
  • Consistent framing during edits: When editing existing visuals, keeping framing automatic or aligned with the source avoids unwanted shifts in composition.

Example: Specifying aspect_ratio in Python

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 with warm golden light",
    aspect_ratio="16:9",
)

print(response.data[0].url)

grok-imagine-image-quality and grok-imagine-image-2.0

xAI introduced dedicated Quality Mode (grok-imagine-image-quality) to push realism, fine texture detail, text rendering, and prompt adherence further than earlier baseline generation. With the arrival of grok-imagine-image-2.0, quality control is consolidated into the quality parameter on grok-imagine-image-2.0:

Aspect Behavior & Guidance
Transition note As of November 2, 2026, requests targeting grok-imagine-image-quality are routed to grok-imagine-image-2.0 at low quality (at a lower per-image price than legacy quality requests). Prefer targeting grok-imagine-image-2.0 directly so you explicitly control the quality tier you pay for.
quality values Supported on grok-imagine-image-2.0 with values "low", "medium", and "auto" (the default).
auto behavior Lets the service choose quality per request—currently using low for image generation and medium for image editing.
Billing alignment Images are billed at the quality tier at which they are served. Pin "low" or "medium" when predictable cost and visual fidelity matter more than automatic selection.

Practical Integration Patterns

  • Batching and concurrency: Use n (1–10) on /v1/images/generations (or sample_batch(n=...) in the xAI Python SDK) to generate multiple variations of the same prompt in one call. For distinct prompts, run concurrent requests with async clients rather than sequential loops.
  • Output format & lifetime: response_format defaults to "url" (temporary hosted URLs—download or persist assets promptly) or can be set to "base64" for direct inline embedding.
  • Deferred execution & moderation: For larger batches or long-running requests, deferred: true returns a request_id immediately for polling via GET /v1/images/{request_id} (with "url" responses retained for 24 hours). Always inspect moderation flags on SDK responses (response.respect_moderation) in case a prompt or output is filtered.

Comments

Leave a Reply

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