Grok Image API Guide: Aspect Ratio, Quality & b64_json

Written by

in

Understanding xAI’s Image Generation API: Framing, Quality, and Payloads

Generating images programmatically through xAI’s /v1/images/generations endpoint centers on a few practical choices that shape latency, cost, and how your application handles binary assets. In xAI’s Grok image ecosystem—anchored by grok-imagine-image-2.0, while older models such as grok-imagine-image and grok-imagine-image-quality have been withdrawn or retired—getting predictable outputs comes down to configuring aspect_ratio, quality (alongside resolution), and response_format (b64_json vs. url) deliberately.


1. Controlling Dimensions with aspect_ratio

Rather than forcing developers to guess exact pixel widths and heights for every target layout, xAI’s image generation API exposes aspect_ratio. When omitted, it defaults to auto, allowing the model to select a ratio suited to the prompt. Explicitly passing a ratio gives you deterministic framing across surfaces:

  • Square & Classic Frames: 1:1 fits social feeds and thumbnails, while 4:3 / 3:4 and 3:2 / 2:3 align cleanly with editorial layouts, slides, and photography.
  • Widescreen & Vertical: 16:9 and 9:16 cover standard widescreen and vertical story formats, with wider banners (2:1, 5:2, 21:9) and tall mobile phone aspect ratios (19.5:9, 20:9, and their portraits) available for specialized UI slots.
  • Resolution Pairing: aspect_ratio sets the proportions, while resolution (1k by default, or 2k) controls output scale and detail density.

2. Quality Tiers and Model Lifecycle (grok-imagine-image-quality vs. grok-imagine-image-2.0)

The quality parameter on grok-imagine-image-2.0 accepts low, medium, or auto:

  • auto (Default): Lets the service choose the quality per request. In current behavior documented by xAI, auto serves low for image generation and medium for image editing.
  • low vs. medium: Pinning low prioritizes speed and lower per-image cost for rapid iteration or drafts, whereas pinning medium (paired with 2k resolution) helps preserve finer textures and recognizable details in final assets. Billing reflects the quality tier served.

If you run across references to grok-imagine-image-quality or grok-imagine-image, treat them as legacy/retired model identifiers rather than targets for new builds. Standardizing on grok-imagine-image-2.0 keeps your integration aligned with xAI’s current quality and resolution controls.


3. Returning b64_json vs. Temporary URLs

By default, /v1/images/generations returns temporary hosted URLs (response_format: "url"). Because those URLs are short-lived, relying on them directly in persistent UIs or delayed jobs risks broken links.

Setting "response_format": "b64_json" returns Base64-encoded image bytes directly inside the JSON response payload:

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": "A minimalist desk lamp on a light wood desk, crisp morning light",
    "aspect_ratio": "3:2",
    "resolution": "2k",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }'

When to choose b64_json:

  • You want to decode bytes immediately in backend workers, upload straight to your own object storage, or pass inline data without an extra download hop.
  • Note the trade-off: Base64 payloads are roughly a third larger than raw binary bytes and increase JSON response size, especially when generating multiple images (n up to 10) at 2k. For large or deferred batches (deferred: true), xAI’s documentation notes that deferred requests use URL output rather than b64_json.

4. Practical Checklist for Reliable Image Pipelines

  1. Be explicit in production: Set aspect_ratio, resolution, quality, and response_format explicitly instead of letting defaults drift across environments.
  2. Persist immediately: Whether you decode b64_json or fetch a url right away, save the final asset to durable storage before serving it to end users.
  3. Describe concrete subjects first: Lead prompts with clear subjects, counts, materials, and lighting rather than buzzwords so higher resolution and quality tiers have concrete details to render.

(Tip: Signing in to a Google Account makes it easy to save this chat history and revisit notes or code snippets later.)

Comments

Leave a Reply

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