Generating Images with xAI’s Grok Imagine API
xAI’s image generation documentation (docs.x.ai) covers how developers generate and edit visuals using the Grok Imagine image models (including grok-imagine-image and grok-imagine-image-2.0) over the REST API, OpenAI-compatible SDKs, the xAI SDK, and the Vercel AI SDK. When building pipelines around image generations, two configuration parameters shape how assets fit into your app and how your backend handles payloads: aspect_ratio and response_format.
Controlling Framing with aspect_ratio
The aspect_ratio parameter controls the proportions of the generated canvas. Instead of forcing every layout into a square or guessing dimensions inside your text prompt, you can pass explicit ratios or let the model infer framing automatically:
- Automatic framing (
auto): When omitted or set toauto, the service selects an aspect ratio suited to the prompt. This works well for open-ended exploration or rapid prototyping when layout constraints are flexible. - Explicit ratios: Passing ratios such as
1:1,16:9,9:16,4:3,3:4, or2:1aligns the generated image with your target container—whether that is a widescreen hero banner, a vertical mobile feed card, or a square avatar. - Resolution pairing: Grok Imagine supports 1K and 2K output resolutions alongside these aspect ratios, letting you balance visual detail against latency and per-image pricing.
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="Minimalist architectural studio overlooking coastal cliffs at dusk",
aspect_ratio="16:9",
)
Choosing Delivery Payloads with response_format
While aspect_ratio determines what shape the image takes, response_format determines how the API returns the image bytes in the response payload:
url(Default): Whenresponse_formatis omitted or set tourl, the API returns temporary hosted URLs. This keeps JSON responses lightweight and fast to parse, making it ideal when you want to inspect results, pass URLs directly into downstream jobs, or download and persist assets into your own object storage before temporary links expire.base64: Requestingbase64returns encoded image data directly in the response so you can embed, transform, or write files without a separate HTTP download step. (Note that in thexai_sdkPython client, image output format can be specified viaimage_format="base64"onclient.image.sample(...).)
| Option | Behavior | Best Fit | Trade-offs |
|---|---|---|---|
url (default) |
Returns temporary hosted image URLs | Lightweight API responses, server-side downloads to permanent storage, quick previews | Links are temporary—persist files if you need long-term retention |
base64 |
Returns base64-encoded image data inline | Immediate in-memory processing, direct embedding, skipping extra fetch hops | Larger JSON payloads and higher memory/bandwidth overhead per image |
Practical Integration Tips
- Batching variations: Use
n(from 1 to 10 on the REST API and OpenAI-compatible clients, orsample_batch(n=...)in the xAI Python SDK) to generate multiple candidates from a single prompt while keepingaspect_ratioandresponse_formatconsistent across the batch. - Consistent framing across edits: When chaining text-to-image generation with natural-language image editing or multi-image compositing, lock in
aspect_ratioearly so layouts stay predictable across iterations. - Safe storage hygiene: Treat
urloutputs as transient—download and store approved assets in durable cloud storage rather than hardcoding returned URLs into long-lived database records.
Leave a Reply