Generating images via xAI’s Grok image API (grok-imagine-image-2.0) through the /v1/images/generations endpoint revolves around framing and configuration: picking the right aspect_ratio, setting resolution and output count (n), and deciding between synchronous responses, deferred jobs, or agentic tool calls.
How /v1/images/generations Works with grok-imagine-image-2.0
When you call POST [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations) with grok-imagine-image-2.0, you can generate images from a text prompt using either the xAI Python SDK, OpenAI-compatible SDKs, the Vercel AI SDK (@ai-sdk/xai), or raw HTTP requests.
At its core, a generation request pairs a prompt and model with optional layout and delivery controls:
- Batch size (
n): Generates between1and10images per request on the REST API and OpenAI-compatible SDKs (defaulting to1when omitted). In the xAI Python SDK, useclient.image.sample(...)for a single image andclient.image.sample_batch(n=...)when generating multiple images. - Resolution (
resolution): Supports1k(the default when omitted) and2k. - Quality (
quality): Supported ongrok-imagine-image-2.0with allowed valueslow,medium, andauto(the default, which currently selectslowfor image generation andmediumfor image editing). - Output format (
response_format): Defaults tourl(returning temporary hosted URLs), or can be set tob64_json/base64depending on the SDK or REST payload when you want inline image data without a separate download step.
Controlling Framing with aspect_ratio
Instead of forcing you to guess pixel dimensions and crop later, grok-imagine-image-2.0 exposes framing directly through the aspect_ratio parameter. Omitting aspect_ratio sets it to auto, allowing the model to choose the ratio that best fits your prompt.
| Aspect Ratio | Typical Use Case |
|---|---|
1:1 |
Social media posts and thumbnails |
16:9 / 9:16 |
Widescreen displays, mobile screens, and vertical stories |
4:3 / 3:4 |
Presentations and portrait compositions |
3:2 / 2:3 |
Classic photography framing |
2:1 / 1:2 |
Wide banners and tall headers |
19.5:9 / 9:19.5 |
Modern iPhone-style smartphone displays |
20:9 / 9:20 |
Modern Android-style smartphone displays |
21:9 |
Cinematic widescreen |
5:2 |
Extra-wide banners |
auto |
Model selects the best aspect ratio for the prompt |
Practical Tip: auto vs. Explicit Ratios
auto is great for rapid ideation when you want the model to match the composition to the subject (such as a wide landscape versus a tall portrait). In production UIs, cards, and mobile screens, explicitly passing aspect_ratio avoids awkward cropping and keeps layout geometry predictable.
Code Examples Across Clients
You can pass aspect_ratio (or aspectRatio in @ai-sdk/xai) alongside model and prompt:
import xai_sdk
client = xai_sdk.Client()
response = client.image.sample(
prompt="Mountain landscape at sunrise with crisp alpine light",
model="grok-imagine-image-2.0",
aspect_ratio="16:9",
)
print(response.url)
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": "Minimalist architectural studio desk at golden hour",
"aspect_ratio": "3:2",
"resolution": "2k",
"quality": "medium",
"response_format": "b64_json",
"n": 1
}'
Scaling Requests: Deferred Jobs, Webhooks, and Agent Tools
Most /v1/images/generations requests finish within the HTTP request lifecycle, but xAI provides several patterns for higher throughput and multi-step workflows:
- Deferred generation: Setting
deferred: trueon/v1/images/generationsreturns arequest_idimmediately so you do not hold a connection open. PollGET /v1/images/{request_id}for results, which useresponse_format: "url"and are retained for 24 hours. - Webhooks: Instead of polling, you can register webhooks for
image.generation.completedandimage.generation.failed(as well as image edit events). - Concurrent distinct prompts: When generating many unrelated images with different prompts, using
AsyncClientwithasyncio.gatheris much faster than sending requests sequentially. - Conversational
image_generationtool: If image creation is part of a broader chat or agent loop rather than a standalone asset call, enabling theimage_generationtool lets Grok write the prompt, choose an aspect ratio, and even chain generation and editing steps within a conversation. When you already know the exact prompt and targetaspect_ratioorresolution, calling/v1/images/generationsdirectly gives you explicit parameter control.
Leave a Reply