xAI Image Generation API: Aspect Ratio & Quality Guide

Written by

in

Controlling Aspect Ratio and Image Quality in the xAI Image Generation API

Generating visuals through an API becomes far more practical when you can lock down framing and fidelity up front instead of cropping or re-rolling outputs after the fact. In the xAI image generation documentation (docs.x.ai), text-to-image requests hit /v1/images/generations (or use the xAI Python SDK and compatible TypeScript/OpenAI SDKs) and expose controls for aspect ratio, resolution, batch count, and quality.

An important recent change in the documentation affects teams migrating older integrations: the standalone grok-imagine-image-quality model slug was retired on November 2, 2026 (following a 60-day notice that started September 2, 2026). Rather than targeting a separate quality-specific model slug, current image generation centers on grok-imagine-image-2.0 and uses the quality parameter alongside aspect_ratio and resolution to tune output behavior and cost.


Framing Outputs with aspect_ratio

The aspect_ratio parameter controls the proportions of generated images on /v1/images/generations and in SDK helpers such as client.image.sample(...).

  • Default (auto): When aspect_ratio is omitted or set to auto, the service chooses an aspect ratio suited to the prompt. That works well for exploratory prototyping, but explicit ratios are safer when UI containers or layout slots have strict geometry.
  • Common ratios and use cases: The docs highlight presets such as 16:9 and 9:16 for widescreen, mobile, and story layouts, and 4:3 and 3:4 for presentations and portraits.
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)

Managing Quality, Resolution, and Cost

With grok-imagine-image-2.0, quality and dimensions are separated into distinct controls so you can balance speed, fine detail, and spend:

Parameter Options & Behavior Practical Guidance
quality low, medium, or auto (default) Supported on grok-imagine-image-2.0. auto currently defaults to low for image generation and medium for image editing.
resolution 1k (default) or 2k Pin medium quality and 2k resolution when fine details must stay clear and recognizable.
n 1 to 10 (default 1 on REST/OpenAI SDKs; required on sample_batch(n=...) in xAI Python SDK) Generate multiple variations in one call when comparing compositions.
response_format url (default) or base64 Temporary URLs expire, so request base64 (or b64_json) when embedding or persisting assets directly.

Billing aligns with how the image is served and processed: images are billed at the quality level served, with flat per-image pricing for generation (and billing for both input and output on edits).

import xai_sdk

client = xai_sdk.Client()
response = client.image.sample(
    prompt="A watercolor painting of a lighthouse at dawn",
    model="grok-imagine-image-2.0",
    aspect_ratio="3:4",
    resolution="2k",
    quality="medium",
)
if response.respect_moderation:
    print(response.url)

Production Details Worth Checking

Two operational details in docs.x.ai often catch teams off guard when moving from a quick script to production:

  1. Moderation checks: Responses in the xAI SDK expose metadata such as response.respect_moderation so you can verify whether an output passed content filtering before rendering or saving it.
  2. Deferred execution and concurrency: Larger batches or long-running calls can set deferred: true on /v1/images/generations and poll GET /v1/images/{request_id} (with results retained for 24 hours and using default url format), or register webhooks for image generation events. When generating many unrelated prompts, concurrent calls via AsyncClient and asyncio.gather outperform sequential requests.

Comments

Leave a Reply

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