{"record":{"id":"c0207c12cf3d9463","repo":"vercel/ai","slug":"image-generation-was-blocked-due-to-a-content-poli","errorCode":null,"errorMessage":"Image generation was blocked due to a content policy violation.","messagePattern":"Image generation was blocked due to a content policy violation\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/xai/src/xai-image-model.ts","lineNumber":166,"sourceCode":"      body.images = imageUrls.map(url => ({ url, type: 'image_url' }));\n    }\n\n    const baseURL = this.config.baseURL ?? 'https://api.x.ai/v1';\n    const currentDate = this.config._internal?.currentDate?.() ?? new Date();\n    const { value: response, responseHeaders } = await postJsonToApi({\n      url: `${baseURL}${endpoint}`,\n      headers: combineHeaders(this.config.headers?.(), headers),\n      body,\n      failedResponseHandler: xaiFailedResponseHandler,\n      successfulResponseHandler: createJsonResponseHandler(\n        xaiImageResponseSchema,\n      ),\n      abortSignal,\n      fetch: this.config.fetch,\n    });\n\n    if (response.data.some(image => image.respect_moderation === false)) {\n      throw new Error(\n        'Image generation was blocked due to a content policy violation.',\n      );\n    }\n\n    const hasAllBase64 = response.data.every(image => image.b64_json != null);\n\n    const images = hasAllBase64\n      ? response.data.map(image => image.b64_json!)\n      : await Promise.all(\n          response.data.map(image =>\n            this.downloadImage(image.url!, abortSignal),\n          ),\n        );\n\n    return {\n      images,\n      warnings,\n      response: {","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/vercel/ai/blob/69428b1f8b037e4d118fb4853428d5c4e620493c/packages/xai/src/xai-image-model.ts#L148-L184","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Revise the prompt (and any input images) to remove content xAI's moderation may flag","Retry — moderation can be borderline; rephrasing usually resolves it","If you need partial results, catch the error and re-run with a smaller/simpler prompt to isolate the offending request"],"exampleFix":"// before\nconst { image } = await generateImage({ model: xai.image('grok-2-image'), prompt: riskyPrompt });\n// after\ntry {\n  const { image } = await generateImage({ model: xai.image('grok-2-image'), prompt: sanitizedPrompt });\n} catch (e) {\n  if ((e as Error).message.includes('content policy')) {\n    // rephrase prompt or surface a user-facing moderation message\n  }\n}","handlingStrategy":"fallback","validationCode":"null","typeGuard":"null","tryCatchPattern":"try {\n  const { image } = await generateImage({ model, prompt });\n} catch (e) {\n  if ((e as Error).message.includes('content policy violation')) {\n    // show a moderation notice or retry with a sanitized prompt\n  }\n}","preventionTips":["Pre-screen prompts for policy-sensitive terms","Sanitize user-supplied prompts before image generation","Treat moderation blocks as user-facing outcomes, not bugs — avoid blanket retries"],"tags":["xai","image-generation","content-policy","moderation"],"backgroundTag":"content-policy-blocked","analyzedSha":"69428b1f8b037e4d118fb4853428d5c4e620493c","analyzedAt":"2026-08-30T12:32:21.016Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}