xAI Image Generation API: Aspect Ratio & Quality Guide

Written by

in

Configuring Image Generations with xAI’s Grok Imagine API

Building reliable visual pipelines means balancing framing, detail, and cost before a single asset hits production. With grok-imagine-image-2.0 on xAI’s /v1/images/generations endpoint (and matching SDKs), developers can control aspect ratio, resolution, batch size, and generation quality directly in each request.

Framing with aspect_ratio

The aspect_ratio parameter controls output dimensions across image generation and editing workflows:

  • Automatic selection (auto): Omitting aspect_ratio—or passing "auto"—lets the model choose a ratio suited to the prompt.
  • Explicit ratios: Pass strings such as "16:9", "1:1", or "9:16" when layout constraints are fixed, such as widescreen headers, square cards, or vertical mobile feeds.
  • Multi-image edit behavior: When editing with up to five source images, output aspect ratio defaults to the first input image unless overridden with aspect_ratio.

Controlling Quality and Resolution on grok-imagine-image-2.0

Quality and resolution work together to shape visual fidelity, latency, and spend:

  • quality (low, medium, auto): Supported on grok-imagine-image-2.0. When omitted or set to auto, the service selects the quality per request—currently defaulting to low for image generation and medium for image editing. Pinning low or medium locks in a specific setting, and billing reflects the quality served.
  • resolution (1k, 2k): Defaults to 1k when omitted, with 2k available for higher pixel detail. Pairing medium quality with 2k resolution is recommended when fine details must stay crisp and recognizable.
  • Flat per-image pricing: Image generation uses flat per-image pricing (listed at $0.04 per image on grok-imagine-image-2.0) regardless of prompt length, while image edits bill for both input and generated output images.

Batching, Output Formats, and Deferred Requests

Beyond dimensions and quality, the API supports practical production ergonomics:

  • Batch counts (n): Generate between 1 and 10 images per request (defaulting to 1 on REST and OpenAI-compatible clients). In the xAI Python SDK, use sample() for a single image and sample_batch(n=...) for batches. For distinct prompts, running concurrent calls with AsyncClient and asyncio.gather is faster than sequential requests.
  • Response format (url vs. base64): The default url format returns temporary hosted URLs, whereas requesting base64 / b64_json returns inline image data for immediate embedding or durable storage.
  • Deferred execution (deferred: true): Set deferred: true on /v1/images/generations to receive a request_id immediately and poll GET /v1/images/{request_id}. Deferred requests use response_format: "url" and retain results for 24 hours.

Example: OpenAI-Compatible Client

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://api.x.ai/v1",
});

const response = await client.images.generate({
  model: "grok-imagine-image-2.0",
  prompt: "Editorial architectural study of a glass conservatory in morning mist",
  aspect_ratio: "16:9",
  resolution: "2k",
  quality: "medium",
});

console.log(response.data[0].url);

Practical Workflow Takeaways

Start with auto aspect ratio and default settings during rapid prompt exploration, where matching the subject to the prompt matters most. Once a prompt proves reliable, pin aspect_ratio for UI fit and step up quality to medium and resolution to 2k for assets where small textures, typography, or architectural edges need to hold up under closer inspection.

Comments

Leave a Reply

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