Generating Images with Grok Imagine: Mastering aspect_ratio and response_format
xAI’s image generation endpoint (/v1/images/generations), powered by models such as grok-imagine-image-2.0, gives developers clean control over framing, output count, resolution, quality, and payload delivery. Two parameters—aspect_ratio and response_format—shape how generated images fit your interface and how your backend ingests them.
How /v1/images/generations Works
At its core, /v1/images/generations takes a text prompt and a target model (such as grok-imagine-image-2.0), returning one or more generated images. You can call it directly via HTTP ([https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations)), through an OpenAI-compatible SDK pointed at [https://api.x.ai/v1](https://api.x.ai/v1), or via xAI’s native SDKs and AI SDK integrations.
Beyond aspect_ratio and response_format, requests commonly configure:
n(1–10): Generates multiple images in one call on REST and OpenAI-compatible clients (defaulting to1). In the xAI Python SDK, usesample()for a single image andsample_batch(n=...)for batches.resolution: Controls output resolution (such as1kor2k), helpful when fine details must stay crisp.quality: Supported ongrok-imagine-image-2.0(low,medium, orauto), letting you pin quality or let the service select it automatically.deferred: Set totrueon/v1/images/generations(or/v1/images/edits) for long-running batches so the API immediately returns arequest_idthat you poll viaGET /v1/images/{request_id}(with results retained for 24 hours and supportingurloutput).
Controlling Framing with aspect_ratio
The aspect_ratio parameter defines the proportions of the generated canvas. When omitted, it defaults to auto, allowing the model to choose a ratio suited to your prompt.
| Aspect Ratio | Typical Use Case |
|---|---|
1:1 |
Social media posts and thumbnails |
16:9 / 9:16 |
Widescreen displays, mobile views, and stories |
4:3 / 3:4 |
Presentations and portraits |
3:2 / 2:3 |
Classic photography framing |
2:1 / 1:2 |
Banners and headers |
19.5:9 / 9:19.5 |
Modern smartphone displays (such as iPhone) |
20:9 / 9:20 |
Modern smartphone displays (such as Android) |
21:9 |
Cinematic widescreen |
5:2 |
Wide panoramic layouts supported on grok-imagine-image-2.0 |
Picking the right ratio upfront prevents awkward cropping later and aligns your prompt’s composition—such as wide landscapes versus vertical portraits—with the available canvas.
Choosing response_format: url vs. b64_json
The response_format parameter determines how the API returns image data in the response payload:
url(Default): Returns temporary hosted URLs. This keeps JSON payloads lightweight and works well for quick previews or immediate client rendering, though you should download or persist assets you intend to keep long term rather than relying on temporary links. It is also the format supported for deferred requests.b64_json: Returns base64-encoded image data directly in the response. Useb64_jsonwhen your server or pipeline needs to save bytes straight to disk or object storage, pass data directly into downstream processing, or avoid an extra network fetch from a temporary URL.
Practical Request Examples
REST (curl) Requesting Base64 at 3:2
curl -X POST https://api.x.ai/v1/images/generations
-H "Authorization: Bearer $XAI_API_KEY"
-H "Content-Type: application/json"
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "Ceramic pour-over coffee dripper on a pale oak counter in morning sunlight, crisp focus, warm natural tones",
"aspect_ratio": "3:2",
"resolution": "2k",
"quality": "medium",
"response_format": "b64_json",
"n": 1
}'
Python with the OpenAI-Compatible Client
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 low valley mist and warm golden light",
aspect_ratio="16:9",
response_format="url",
)
print(response.data[0].url)
Best Practices for Reliable Image Pipelines
- Match prompt framing to
aspect_ratio: Describe subjects, crops, and camera angles that naturally fit the chosen ratio instead of fighting a narrow or wide frame. - Persist
urloutputs or requestb64_json: Because hosted URLs are temporary, store images in your own storage or requestb64_jsonwhen durability matters. - Parallelize distinct prompts: When generating many unrelated images with different prompts, use async concurrency (such as
AsyncClientwithasyncio.gather) rather than waiting on sequential requests one by one. - Iterate with edits when layout works: Once a generation nails the composition and subject, use
/v1/images/editsto adjust specific details while keeping the scene consistent.
Leave a Reply