Grok Imagine Image API: Aspect Ratio & Base64 Guide

Written by

in

Controlling Framing and Payloads in xAI’s Image Generation API

Building reliable image pipelines with xAI’s Grok image models (grok-imagine-image-2.0 and related grok-imagine-image variants) via /v1/images/generations comes down to getting layout geometry and binary delivery right. Two parameters handle most of that work in production: aspect_ratio and response_format (b64_json).

Because generated URLs are temporary, production systems that persist assets, stream bytes directly into object storage, or avoid a second download hop often request base64 output instead. Meanwhile, locking aspect_ratio prevents UI containers from stretching or awkwardly cropping outputs across grids, feeds, and multi-image batches.


Locking Geometry with aspect_ratio

When you omit aspect_ratio on /v1/images/generations (or through OpenAI-compatible SDKs and the xAI SDK), the parameter defaults to auto, letting the model pick proportions that match the prompt. That works well for exploratory prompts, but fixed UI slots and cohesive visual series need explicit ratios.

aspect_ratio Typical Use Cases Framing Notes
auto (default) Ad-hoc exploration and varied prompts Model chooses proportions based on prompt context.
1:1 Social posts, avatars, thumbnails, uniform grids Balanced square framing for cards and profile slots.
16:9 / 21:9 Widescreen hero banners, presentations, cinematic scenes Wide horizontal stage for landscapes and headers.
9:16 Vertical stories, short-form mobile feeds Tall portrait framing for full-height slots.
4:3 / 3:4 Slide decks, UI cards, portrait compositions Standard presentation and portrait proportions.
3:2 / 2:3 Editorial photography, consistent multi-card series Classic camera proportions for cohesive sets.
2:1 / 1:2 / 5:2 Wide banners, headers, tall vertical strips Extended horizontal or vertical strips.
19.5:9 / 20:9 (and inverses) Modern smartphone displays Matches tall edge-to-edge phone screens.

Returning Inline Images with b64_json

By default, /v1/images/generations sets response_format to url, returning temporary hosted links that you must download or process promptly. Passing "response_format": "b64_json" returns the base64-encoded image string directly inside the JSON response under data[i].b64_json.

curl -sS 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": "Pour-over coffee on a warm oak counter in morning light",
    "aspect_ratio": "3:2",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }' | jq -r '.data[0].b64_json' | base64 --decode > pour_over.png

Trade-offs and Constraints to Plan For

  • Payload size overhead: Base64 encoding expands binary size by roughly one-third. If you generate batches (n from 1 to 10) at higher resolution (2k vs. default 1k), size HTTP client buffers, timeouts, and storage writes accordingly.
  • Deferred mode limitation: Setting deferred: true for long-running requests (which returns a request_id to poll via GET /v1/images/{request_id} with results retained for 24 hours) supports response_format: "url" only, not b64_json.
  • Quality and cost pairing: On grok-imagine-image-2.0, quality accepts low, medium, or auto (which currently defaults to low for image generation and medium for editing), and billing reflects the served quality tier. Pinning medium and 2k helps preserve fine visual details when needed, while low keeps iteration fast and economical.
  • Moderation handling: When using the xAI SDK, verify respect_moderation on responses so your application gracefully handles filtered outputs rather than assuming an image payload is always present.

Practical Habits for Multi-Image Pipelines

  • Lock aspect_ratio across series: When generating multiple assets for a catalog or storyboard, keep aspect_ratio fixed and maintain a consistent prompt structure (subject and setting first, key details and camera angle next, lighting and framing last).
  • Use concurrent requests for distinct prompts: When prompts differ rather than sharing a single batch prompt, issue requests concurrently (such as with AsyncClient and asyncio.gather in Python) instead of waiting on them sequentially.
  • Choose direct endpoints for deterministic control: Call /v1/images/generations directly when you want explicit control over aspect_ratio, quality, resolution, and response_format, reserving conversational image generation tools for agentic flows where the model writes prompts and chooses framing on its own.

Comments

Leave a Reply

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