vercel/ai · error

Image generation was blocked due to a content policy violati

Error message

Image generation was blocked due to a content policy violation.

What it means

The xAI image API returns a respect_moderation flag per generated image; when false the image was blocked by content policy. doGenerate throws a plain Error rather than returning a partial result, so a single blocked image in the batch fails the whole generateImage call.

Source

Thrown at packages/xai/src/xai-image-model.ts:166

      body.images = imageUrls.map(url => ({ url, type: 'image_url' }));
    }

    const baseURL = this.config.baseURL ?? 'https://api.x.ai/v1';
    const currentDate = this.config._internal?.currentDate?.() ?? new Date();
    const { value: response, responseHeaders } = await postJsonToApi({
      url: `${baseURL}${endpoint}`,
      headers: combineHeaders(this.config.headers?.(), headers),
      body,
      failedResponseHandler: xaiFailedResponseHandler,
      successfulResponseHandler: createJsonResponseHandler(
        xaiImageResponseSchema,
      ),
      abortSignal,
      fetch: this.config.fetch,
    });

    if (response.data.some(image => image.respect_moderation === false)) {
      throw new Error(
        'Image generation was blocked due to a content policy violation.',
      );
    }

    const hasAllBase64 = response.data.every(image => image.b64_json != null);

    const images = hasAllBase64
      ? response.data.map(image => image.b64_json!)
      : await Promise.all(
          response.data.map(image =>
            this.downloadImage(image.url!, abortSignal),
          ),
        );

    return {
      images,
      warnings,
      response: {

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Revise the prompt (and any input images) to remove content xAI's moderation may flag
  2. Retry — moderation can be borderline; rephrasing usually resolves it
  3. If you need partial results, catch the error and re-run with a smaller/simpler prompt to isolate the offending request

Example fix

// before
const { image } = await generateImage({ model: xai.image('grok-2-image'), prompt: riskyPrompt });
// after
try {
  const { image } = await generateImage({ model: xai.image('grok-2-image'), prompt: sanitizedPrompt });
} catch (e) {
  if ((e as Error).message.includes('content policy')) {
    // rephrase prompt or surface a user-facing moderation message
  }
}
Defensive patterns

Strategy: fallback

Validate before calling

null

Type guard

null

Try / catch

try {
  const { image } = await generateImage({ model, prompt });
} catch (e) {
  if ((e as Error).message.includes('content policy violation')) {
    // show a moderation notice or retry with a sanitized prompt
  }
}

Prevention

When it happens

Trigger: Calling generateImage with an xai image model where any returned image has respect_moderation === false — i.e. the prompt (or an input image for edits) tripped xAI's moderation layer even though the API call itself succeeded.

Common situations: Prompts with borderline/unsafe content; image-to-image edits where the source image is flagged; stricter moderation on newer Grok image models; adult/violence-adjacent creative workloads.

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/c0207c12cf3d9463. Report an issue: GitHub.