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 to1on the REST API and OpenAI-compatible SDKs). In the xAI Python SDK, single-image calls usesample()while multi-image batches usesample_batch(n=...)withnrequired. - Aspect ratio (
aspect_ratio): Controls image proportions and defaults toautowhen omitted, letting the model pick a ratio suited to the prompt. - Resolution (
resolution): Supports1kand2k(lowercasek), helping preserve fine details when2kis selected alongside higher quality. - Quality (
quality): Supported ongrok-imagine-image-2.0with valueslow,medium, orauto(which defaults tolowfor generation andmediumfor editing, with billing tied to the served quality). - Response format (
response_format): Defaults tourl(returning temporary hosted URLs) or acceptsb64_jsonto return base64-encoded image strings directly in the JSON payload. - Deferred mode (
deferred): Settingdeferred: truereturns arequest_idright away so clients can pollGET /v1/images/{request_id}(results are retained for 24 hours), though deferred requests only supportresponse_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 (
nup to10) at2kresolution produces substantial JSON payloads, so ensure HTTP client buffers, timeouts, and memory limits are sized accordingly (and note thatdeferred: trueis 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.
Leave a Reply