Grok Imagine API: Set Aspect Ratio & Response Format

Written by

in

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.0 is the current generation model supporting 1K and 2K output resolutions and up to 10 images per request (n from 1 to 10 on REST and OpenAI-compatible SDKs). In the xAI Python SDK, client.image.sample(...) generates a single image, while client.image.sample_batch(n=..., ...) generates multiple variations.
  • Quality control: On grok-imagine-image-2.0, the optional quality parameter accepts low, medium, or auto (defaulting to auto, which currently selects low for image generation and medium for image edits).
  • Deferred execution: Setting deferred: true returns a request_id immediately so you can poll GET /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_moderation in 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, making url ideal when passing links to downstream workers, object storage uploaders, or client UIs. Note that deferred requests (deferred: true) support response_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. (In xai_sdk, passing image_format="base64" exposes decoded image bytes on response.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

  1. Match response_format to your transport budget: Base64 payloads grow significantly larger than URLs, so use url for high-volume batching or deferred jobs, and reserve base64 for immediate in-memory processing, immediate file writes, or direct inline delivery.
  2. Parallelize distinct prompts: When generating many unrelated images with different prompts, issue concurrent requests (for example, using an async client with asyncio.gather in Python) rather than waiting sequentially on one prompt at a time.
  3. Use the right tool for conversational edits: Calling /v1/images/generations directly gives you explicit control over aspect_ratio, response_format, and n. 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 conversation image_generation tool instead.

Comments

Leave a Reply

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