Xai images generations api aspect_ratio grok-imagine-image-quality site:docs.x.ai

Written by

in

Framing and Fidelity in the xAI Image Generation API

Building reliable visual pipelines around generative media requires tight control over framing, detail fidelity, and execution flow. In the xAI image APIs—centered on grok-imagine-image-2.0 via /v1/images/generations and /v1/images/edits—developers can explicitly govern output geometry with aspect_ratio, pair it with resolution (1k or 2k), and tune rendering rigor with quality (low, medium, or auto).

Rather than treating image generation as a one-shot black box, these controls let you match canvas proportions to the target display surface while keeping compute and cost aligned with how much visual detail the application actually needs.


Controlling Framing with aspect_ratio

The aspect_ratio parameter sets the width-to-height proportion of the generated output. Omitting it defaults to auto, allowing the service to choose a ratio suited to the prompt. When building UI components, marketing assets, or mobile-first feeds, leaving framing to chance often causes awkward cropping or wasted space—so pinning an explicit ratio is usually preferred.

Ratio Typical Use Case
1:1 Social media posts and square thumbnails
16:9 / 9:16 Widescreen layouts, mobile screens, and vertical stories
4:3 / 3:4 Slide decks, structured panels, and portraits
3:2 / 2:3 Classic photography and editorial frames
2:1 / 1:2 / 5:2 Wide banners and page headers
19.5:9 / 9:19.5 Modern smartphone displays (such as iPhone form factors)
20:9 / 9:20 Modern smartphone displays (such as Android form factors)
21:9 Cinematic widescreen scenes
auto Model selects the ratio that best fits the prompt

In multi-image edits via /v1/images/edits (which accept up to five source images), the output aspect ratio defaults to matching the first input image unless overridden by aspect_ratio. By contrast, when using the conversational image_generation tool inside chat or Responses workflows, the model selects the aspect ratio dynamically unless you specify the desired ratio in your natural-language instruction.


Tuning Detail with quality and resolution

While aspect_ratio shapes the frame, quality and resolution determine how crisp and thorough the rendered result is.

  • Supported quality values: The optional quality parameter accepts low, medium, and auto (supported on grok-imagine-image-2.0).
  • How auto behaves: When quality is omitted or set to auto, the service selects the quality automatically—currently using low for standard image generations and medium for image edits.
  • Pinning quality and resolution: Images are billed according to the quality level at which they are served. Passing low or medium locks in a specific quality tier, and pairing medium with 2k resolution (over the default 1k) is recommended when fine textures, small objects, or intricate details must remain clearly recognizable.

Batching, Persistence, and Workflow Patterns

Beyond framing and quality, production integrations benefit from matching the request mode to the workload:

  • Batch size (n): You can request between 1 and 10 images in a single call. On the REST API and OpenAI-compatible SDKs, n defaults to 1. In the xAI Python SDK, use sample() for a single image and sample_batch(n=...) when generating multiple images. For distinct prompts that do not share a batch call, concurrent execution via AsyncClient with asyncio.gather avoids sequential bottlenecks.
  • Output format and storage: By default (response_format: "url"), responses return temporary hosted URLs, whereas "b64_json" returns base64-encoded data for direct embedding or saving. Because generated URLs are temporary, request base64 or persist assets to durable storage or the Files API when you need long-term retention.
  • Deferred execution: Setting deferred: true on /v1/images/generations or /v1/images/edits returns a request_id immediately so you can poll GET /v1/images/{request_id} without holding an open connection. Deferred requests use response_format: "url" and retain results for 24 hours.

Practical Example

import xai_sdk

client = xai_sdk.Client()

response = client.image.sample(
    prompt="Mountain landscape at sunrise, crisp granite ridges and warm morning light",
    model="grok-imagine-image-2.0",
    aspect_ratio="16:9",
    resolution="2k",
    quality="medium",
)

if response.respect_moderation:
    print(response.url)

A reliable rule of thumb for production pipelines is to generate early candidates at 1k and low (or auto) quality to validate composition and aspect_ratio, then promote approved prompts—or targeted /v1/images/edits passes—to medium quality and 2k resolution for final delivery.

Comments

Leave a Reply

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