xAI Grok Image API: Aspect Ratio & b64_json Guide

Written by

in

Framing and Payload Control in Grok Image Generation (grok-imagine-image)

xAI’s image generation documentation (docs.x.ai) centers on the /v1/images/generations endpoint and models in the grok-imagine-image family (including grok-imagine-image-2.0). When building production pipelines around text-to-image generation, two parameters determine how cleanly your output fits into downstream systems: aspect_ratio for framing and response_format: "b64_json" for inline payload delivery.


How /v1/images/generations Works

Requests to [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations) take an Authorization: Bearer $XAI_API_KEY header, a model identifier, and a prompt. Beyond the prompt, the endpoint exposes practical controls for batching, dimensions, quality, delivery format, and asynchronous execution:

  • Batch count (n): Accepts values from 1 to 10 per request (optional and defaults to 1 on the REST API and OpenAI-compatible SDKs). In the xAI Python SDK, single-image calls use sample() while multi-image batches use sample_batch(n=...) with n required.
  • Aspect ratio (aspect_ratio): Controls image proportions and defaults to auto when omitted, letting the model pick a ratio suited to the prompt.
  • Resolution (resolution): Supports 1k and 2k (lowercase k), helping preserve fine details when 2k is selected alongside higher quality.
  • Quality (quality): Supported on grok-imagine-image-2.0 with values low, medium, or auto (which defaults to low for generation and medium for editing, with billing tied to the served quality).
  • Response format (response_format): Defaults to url (returning temporary hosted URLs) or accepts b64_json to return base64-encoded image strings directly in the JSON payload.
  • Deferred mode (deferred): Setting deferred: true returns a request_id right away so clients can poll GET /v1/images/{request_id} (results are retained for 24 hours), though deferred requests only support response_format: "url".

Controlling Framing with aspect_ratio

Leaving aspect_ratio on auto works well for exploratory prompts or one-off visuals where composition should follow the subject. In UI cards, galleries, or multi-shot series, however, locking a ratio prevents layout shifts and keeps framing consistent across images.

Aspect Ratio Typical Use Cases Framing Notes
1:1 Social media posts, avatars, thumbnails Balanced square frame for cards and profile slots
16:9 / 21:9 Widescreen views, banners, headers, cinematic shots Wide horizontal stage for landscapes and scenes
9:16 Mobile stories, vertical feeds Tall portrait orientation for full-height vertical slots
4:3 / 3:4 Presentations, portraits Classic presentation or vertical portrait framing
3:2 / 2:3 Photography, editorial series Standard photographic proportions for consistent sets
2:1 / 1:2 / 5:2 Banners, wide headers Extended horizontal or vertical banner proportions
19.5:9 / 20:9 (and inverses) Modern smartphone screens (iPhone / Android) Matches tall edge-to-edge mobile displays
auto Ad-hoc exploration Model selects the ratio based on the prompt

Why Request b64_json Instead of Temporary URLs?

By default (response_format: "url"), /v1/images/generations returns temporary URLs that must be downloaded or handled promptly rather than treated as permanent links. Setting "response_format": "b64_json" embeds the encoded image data directly in the response object (data[i].b64_json), which changes how your backend moves bytes around:

  • Immediate persistence: Backend workers can decode the base64 string straight to disk or object storage without making a second HTTP request before a temporary URL expires.
  • Fewer network hops: Pipelines that immediately pass images into editing steps, data URIs, or internal storage avoid extra download round-trips.
  • Payload size trade-off: Base64 encoding expands binary size by roughly one-third inside JSON. Generating large batches (n up to 10) at 2k resolution produces substantial JSON payloads, so ensure HTTP client buffers, timeouts, and memory limits are sized accordingly (and note that deferred: true is limited to "url").

Example Request with aspect_ratio and b64_json

Here is a direct curl call to /v1/images/generations using grok-imagine-image-2.0 with a locked 3:2 aspect ratio, 2k resolution, medium quality, and inline b64_json output:

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": "A ceramic pour-over coffee carafe on a pale oak counter in bright morning light, 45-degree angle",
    "aspect_ratio": "3:2",
    "resolution": "2k",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }'

When the response returns, each item in data contains b64_json rather than a temporary url, ready to decode or forward within your application.

Comments

Leave a Reply

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