Generating Images with Grok Imagine on xAI’s API
xAI’s image generation endpoint (/v1/images/generations)—powered by Grok Imagine (grok-imagine-image-2.0, along with legacy/partner model slugs such as grok-imagine-image)—turns text prompts into images while giving developers explicit control over framing, delivery payload, batch size, quality, and asynchronous execution. Two parameters do much of the heavy lifting in production pipelines: aspect_ratio, which governs image proportions, and response_format, which determines whether you receive temporary hosted URLs or inline encoded image data.
Because the endpoint follows REST conventions and works with OpenAI-compatible clients as well as xAI’s native SDKs (xai_sdk in Python and @ai-sdk/xai in JavaScript), teams can adopt it quickly without overhauling their existing media workflows.
How /v1/images/generations Fits Together
At its core, /v1/images/generations accepts a prompt and model alongside optional configuration fields:
- Model selection:
grok-imagine-image-2.0is the current generation model supporting 1K and 2K output resolutions and up to 10 images per request (nfrom1to10on REST and OpenAI-compatible SDKs). In the xAI Python SDK,client.image.sample(...)generates a single image, whileclient.image.sample_batch(n=..., ...)generates multiple variations. - Quality control: On
grok-imagine-image-2.0, the optionalqualityparameter acceptslow,medium, orauto(defaulting toauto, which currently selectslowfor image generation andmediumfor image edits). - Deferred execution: Setting
deferred: truereturns arequest_idimmediately so you can pollGET /v1/images/{request_id}(with results retained for 24 hours), useful for long-running batches or avoiding open connections. - Moderation metadata: Responses include moderation status (
respect_moderationin the xAI SDK) so pipelines can verify whether outputs passed safety checks.
Controlling Framing with aspect_ratio
The aspect_ratio parameter sets the target proportions of generated images. When omitted, it defaults to auto, allowing the model to pick a ratio suited to the prompt. Specifying an explicit ratio prevents awkward cropping downstream and aligns generated assets with UI containers, video frames, or print layouts.
| Aspect Ratio | Typical Use Case |
|---|---|
auto (default) |
Lets the model select the ratio that best fits the prompt |
1:1 |
Square social feeds, avatars, and thumbnails |
16:9 / 9:16 |
Widescreen video/displays, mobile screens, and vertical stories |
4:3 / 3:4 |
Presentations, slides, and portrait layouts |
3:2 / 2:3 |
Standard camera photography framing |
2:1 / 1:2 |
Wide banners and tall vertical headers |
19.5:9 / 9:19.5 |
Modern smartphone displays (such as iPhone) |
20:9 / 9:20 |
Tall modern smartphone displays (such as Android) |
21:9 / 5:2 |
Cinematic widescreen and extra-wide banners |
Choosing response_format: URLs vs. Base64
The response_format parameter controls how the generated image bytes are delivered back to your caller:
url(Default): Returns temporary hosted image URLs. JSON payloads stay compact, makingurlideal when passing links to downstream workers, object storage uploaders, or client UIs. Note that deferred requests (deferred: true) supportresponse_format: "url"only.base64: Returns base64-encoded image data so you can embed images directly in responses or write decoded bytes straight to disk without a separate network download step. (Inxai_sdk, passingimage_format="base64"exposes decoded image bytes onresponse.image.)
curl -X POST https://api.x.ai/v1/images/generations
-H "Content-Type: application/json"
-H "Authorization: Bearer $XAI_API_KEY"
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "Minimalist architectural studio at golden hour with long shadows",
"aspect_ratio": "16:9",
"response_format": "url",
"n": 1
}'
Practical Pipeline Tips for Production
- Match
response_formatto your transport budget: Base64 payloads grow significantly larger than URLs, so useurlfor high-volume batching or deferred jobs, and reservebase64for immediate in-memory processing, immediate file writes, or direct inline delivery. - Parallelize distinct prompts: When generating many unrelated images with different prompts, issue concurrent requests (for example, using an async client with
asyncio.gatherin Python) rather than waiting sequentially on one prompt at a time. - Use the right tool for conversational edits: Calling
/v1/images/generationsdirectly gives you explicit control overaspect_ratio,response_format, andn. If you are building a multi-turn chat or agent workflow where Grok decides when to generate or iteratively edit images mid-conversation, use the conversationimage_generationtool instead.
Leave a Reply