Grok Imagine Image API: Aspect Ratio & Generations Guide

Written by

in

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 between 1 and 10 images per request on the REST API and OpenAI-compatible SDKs (defaulting to 1 when omitted). In the xAI Python SDK, use client.image.sample(...) for a single image and client.image.sample_batch(n=...) when generating multiple images.
  • Resolution (resolution): Supports 1k (the default when omitted) and 2k.
  • Quality (quality): Supported on grok-imagine-image-2.0 with allowed values low, medium, and auto (the default, which currently selects low for image generation and medium for image editing).
  • Output format (response_format): Defaults to url (returning temporary hosted URLs), or can be set to b64_json / base64 depending 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: true on /v1/images/generations returns a request_id immediately so you do not hold a connection open. Poll GET /v1/images/{request_id} for results, which use response_format: "url" and are retained for 24 hours.
  • Webhooks: Instead of polling, you can register webhooks for image.generation.completed and image.generation.failed (as well as image edit events).
  • Concurrent distinct prompts: When generating many unrelated images with different prompts, using AsyncClient with asyncio.gather is much faster than sending requests sequentially.
  • Conversational image_generation tool: If image creation is part of a broader chat or agent loop rather than a standalone asset call, enabling the image_generation tool 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 target aspect_ratio or resolution, calling /v1/images/generations directly gives you explicit parameter control.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *