Mastering xAI Image Generation: Aspect Ratios, Quality Control, and API Parameters
xAI’s image generation API brings the power of the Grok image models directly into developer workflows. Whether you are building automated content pipelines, creative applications, or agentic tools, fine-tuning your image outputs requires precise control over dimensions, rendering quality, and output formats.
This guide breaks down how to leverage key configuration options—specifically aspect_ratio, resolution settings, and the quality parameter—using the official xAI API specifications.
1. Controlling Image Dimensions with aspect_ratio
When generating images via POST /v1/images/generations, omitting the aspect ratio defaults to auto, which allows the model to intelligently select the best composition based on your text prompt. However, if your application requires specific framing, xAI supports a wide array of precise aspect ratios:
| Aspect Ratio | Common Use Cases & Target Formats |
|---|---|
1:1 |
Standard social media posts, square avatars, profile thumbnails |
16:9 / 9:16 |
Widescreen displays, desktop wallpapers, mobile stories, vertical video assets |
4:3 / 3:4 |
Traditional presentation slides, standard portrait photography |
3:2 / 2:3 |
Classic digital photography and print framing |
21:9 |
Cinematic ultra-wide landscapes and banners |
19.5:9 / 20:9 |
Modern smartphone display mockups (optimized for iPhone and Android ratios) |
auto |
Model automatically chooses the optimal ratio for the prompt |
2. Managing Output Fidelity: Resolution and Quality Parameters
To balance generation speed, cost, and visual crispness, xAI exposes explicit controls over resolution and render quality tiers on supported models like grok-imagine-image-2.0:
- Resolution (
resolution): Choose between standard1k(default) for faster iterations and lightweight payloads, or2kfor high-resolution asset production. - Quality Tier (
quality): Acceptslow,medium, orauto. When set toauto, the service automatically scales quality based on the task (servinglowfor standard generations andmediumfor edits). - Pro Tip: Pin
mediumquality and2kresolution when fine details—such as legible text within an image or intricate textures—must remain recognizable.
3. Implementation Example
Here is how you configure these parameters using an OpenAI-compatible client pointed at the xAI endpoint ([https://api.x.ai/v1](https://api.x.ai/v1)):
{
"model": "grok-imagine-image-2.0",
"prompt": "A futuristic cyberpunk Tokyo street at dusk with neon reflections in puddles",
"aspect_ratio": "16:9",
"resolution": "2k",
"quality": "medium",
"response_format": "url",
"n": 1
}
Using Python with the OpenAI SDK:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
response = client.images.generate(
model="grok-imagine-image-2.0",
prompt="A cinematic drone shot of a misty mountain range at sunrise",
aspect_ratio="21:9",
resolution="2k",
quality="medium"
)
print(response.data[0].url)
Leave a Reply