API / check-respect-moderation-on-imagine-video

API

Check respect_moderation on Imagine video

Read the moderation flag on a finished Imagine video response before you download or publish the clip, because a filtered result can report done with an empty URL. Official Video Generation Response Details document respect_moderation on the SDK response and on the REST video object: the field is true when the clip respects moderation rules; when it is false, the video URL field is empty. Create a key and load credits at console.x.ai, then always branch on this flag after polling reaches done.

What you need

An XAI_API_KEY with Console balance, a completed video job (status is done), and application logic that refuses to treat an empty URL as success. Use this check on every path that consumes response.url or video.url, including text-to-video, image-to-video, reference-to-video, edits, and extensions. Pair it with Poll Imagine video manually with start and get when you own the poll loop.

Branch on the flag

  1. Export the key locally and keep it out of chat logs and public repos:
export XAI_API_KEY="your_api_key"
  1. Prefer the SDK pattern from the docs when generate() returns a response object:
import os
import xai_sdk

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

response = client.video.generate(
    prompt="A futuristic city skyline at dusk",
    model="grok-imagine-video-1.5",
    duration=5,
    aspect_ratio="16:9",
    resolution="720p",
)

if response.respect_moderation:
    print(response.url)
    print(f"Duration: {response.duration} seconds")
else:
    print("Video filtered by moderation")
  1. On REST, after status is done, inspect both fields before download:
RESULT=$(curl -s "https://api.x.ai/v1/videos/$REQUEST_ID" \
  -H "Authorization: Bearer $XAI_API_KEY")
echo "$RESULT" | jq '{
  status,
  respect_moderation: .video.respect_moderation,
  url: .video.url,
  duration: .video.duration
}'

# Only download when respect_moderation is true and url is non-null
URL=$(echo "$RESULT" | jq -r 'select(.video.respect_moderation == true) | .video.url // empty')
if [ -n "$URL" ]; then
  curl -sL "$URL" -o out.mp4
else
  echo "No downloadable URL (filtered or missing)"
fi
  1. Log request_id alongside the moderation outcome so you can revise the prompt or inputs without guessing which job failed soft.
  2. Distinguish moderation filtering (done + respect_moderation: false) from hard failures (status: failed with an error object) covered in Handle Imagine video generation errors.

After the check

When the flag is false, rewrite the prompt or swap the source still/video and submit a new request — do not retry the same body blindly. When the flag is true, download promptly because temporary vidgen.x.ai URLs expire, then optionally persist with Persist an Imagine output with a public URL. For progress during the wait, see Read Imagine video progress while polling.

Pitfalls

Treating status: done as success without reading respect_moderation leads to null-URL crashes downstream. Retrying an identical filtered prompt burns credits without changing the outcome. Confusing a moderation-empty URL with an expired temporary URL sends you down the wrong fix path. Mixing Console API spend with SuperGrok weekly Imagine quotas on grok.com confuses two meters. Pasting keys into tickets or committing them to git forces a rotate.