xAI Grok Image API: Aspect Ratio & Response Format Guide

Written by

in

Shaping Visuals with Grok Image Generation (grok-imagine-image-2.0)

Generating images programmatically is only half the battle—getting the right framing and output format for your application is where production pipelines either shine or break. In xAI’s Grok API (docs.x.ai), the /v1/images/generations endpoint powered by grok-imagine-image-2.0 gives developers direct control over image composition, resolution, batch size, and payload delivery.

Two parameters do much of the heavy lifting in real-world integrations: aspect_ratio and response_format. Together, they let you match screen geometry and downstream storage or rendering needs without post-processing hacks.


Controlling Frame Geometry with aspect_ratio

When you send a prompt to /v1/images/generations, omitting aspect_ratio defaults to auto, letting grok-imagine-image-2.0 select a ratio that fits your description. For predictable layouts—such as hero banners, vertical stories, or presentation slides—explicitly passing aspect_ratio locks in the frame proportions upfront.

Aspect Ratio Typical Use Cases
1:1 Social media feeds, square thumbnails, avatars
16:9 / 9:16 Widescreen video/web frames, mobile screens, vertical stories
4:3 / 3:4 Presentation decks, classic editorial portraits
3:2 / 2:3 Standard photography framing
2:1 / 1:2 Wide banners, tall split layouts, headers
19.5:9 / 9:19.5 Modern iPhone-style tall displays
20:9 / 9:20 Modern Android-style tall displays
21:9 Ultra-wide and cinematic compositions

Pair aspect_ratio with clear composition cues in your prompt. Naming the subject, count, camera angle, and what sits near the edges keeps grok-imagine-image-2.0 from awkwardly cropping key details when you switch from a wide 16:9 frame to a vertical 9:16 one.


Choosing Between url and b64_json in response_format

The response_format parameter controls how image data comes back from /v1/images/generations:

  • url (Default): Returns temporary hosted image URLs. This keeps JSON payloads lightweight and works well for quick previews or passing a URL onward. Because those URLs are temporary, you should not treat them as permanent asset links. Also note that deferred generation requests (deferred: true, which return a request_id to poll via GET /v1/images/{request_id}) support response_format: "url" only, with results retained for 24 hours.
  • b64_json: Returns base64-encoded image data directly in the response. Choose this when you want to decode and save files locally, persist assets to your own storage, or embed image payloads immediately without making a second download hop.

(Note: If you use the official xai-sdk Python client, image sampling exposes its own SDK parameters such as image_format="base64" on client.image.sample(...), whereas REST and OpenAI-compatible clients use response_format.)


Putting It Together in a Request

Here is a complete cURL example calling /v1/images/generations with grok-imagine-image-2.0, specifying both aspect_ratio and response_format alongside resolution and quality options:

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": "Overhead view of a ceramic pour-over coffee dripper on a pale oak table with two stoneware cups and warm morning sunlight from the left",
    "aspect_ratio": "3:2",
    "resolution": "2k",
    "quality": "medium",
    "response_format": "b64_json",
    "n": 1
  }'

A few practical rules of thumb from the documentation keep production runs predictable:

  • Pin quality and resolution when detail matters: quality supports low, medium, and auto (with auto currently defaulting to low for generations and medium for edits), while resolution supports 1k (default) and 2k. Pin medium and 2k when fine textures or small details must stay crisp.
  • Batch wisely: You can request n between 1 and 10 images in a single call, or run concurrent requests asynchronously when generating distinct prompts.
  • Persist what you keep: Because url outputs are temporary, request b64_json (or save via the Files API) whenever your app needs durable storage.

Comments

Leave a Reply

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