Why Use the Async Queue
The synchronous endpoints (/ai/images/generate, /ai/videos/generate) block until the result is ready. For video generation, this can be 1-10+ minutes. The async queue returns immediately with generation IDs so you can:
- Run many generations in parallel (up to 4 per request, no limit on total queued)
- Avoid long-lived HTTP connections
- Build progress-tracking UIs
- Query completed generations and their output URLs at any time
Workflow
succeeded, failed, or cancelled), so you can retrieve results at any time.
A Server-Sent Events stream at GET /api/events can also deliver completion notifications with the output file URL. See Completion Events.
Enqueue
Required Parameters
| Parameter | Type | Description |
|---|---|---|
model | string | Must be an image or video model. Text-only models are rejected. |
Additional Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
num_generations | integer | 1 | How many generations to enqueue per request. Range: 1-4. Call the endpoint multiple times to queue more. |
target_namespace | string | your username | Namespace to store results and bill to. |
prompt, multi_prompt, input_image, input_video, aspect_ratio, duration, seed, generate_audio, response_format) are passed through to the model. Consult the model’s request_schema via GET /api/ai/models/:id for supported parameters and their constraints.
Response
Validation
The API validates at enqueue time that:- The model exists and has image or video output capability
num_generationsis 1-4- The user is authenticated with sufficient credits
failed status with an error_message. To validate parameters and get immediate error feedback, use /ai/images/generate or /ai/videos/generate instead.
List Generations
queued or processing) are returned. Pass an explicit status filter to include terminal states.
Query Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
namespace | string | no | Namespace to query. Defaults to the authenticated user. |
model | string | no | Filter by model name. |
status | string | no | Filter by status: queued, processing, succeeded, failed, or cancelled. When omitted, only active generations are returned. |
media_type | string | no | Filter by image or video. |
repo | string | no | Filter by target repository name. |
folder | string | no | Filter by target directory. |
Response
| Field | Always Present | Description |
|---|---|---|
count | yes | Number of generations in the response |
generation_id | yes | Unique ID for this generation |
model_name | yes | Model name |
prompt | yes | Text prompt (from original request parameters) |
media_type | yes | "image" or "video" |
status | yes | "queued", "processing", "succeeded", "failed", or "cancelled" |
result_url | yes | Output file URL when succeeded, otherwise null |
error_message | yes | Error details when failed, otherwise null |
enqueued_at | yes | Unix timestamp when the job was enqueued |
started_at | yes | Unix timestamp when processing began, or null |
completed_at | yes | Unix timestamp when the job reached a terminal state, or null |
seed | if submitted | Random seed |
aspect_ratio | if submitted | Aspect ratio |
duration | if submitted | Video duration |
seed, aspect_ratio, duration, input_image) are included in the response alongside the fields above.
Polling Strategy
- Image generation (FLUX, etc.): typically completes in 5-30 seconds. Poll every 2-5 seconds.
- Video generation (Kling, etc.): typically takes 1-5 minutes. Poll every 10-30 seconds.
status is a terminal value (succeeded, failed, or cancelled), or until count reaches 0 when using the default active-only filter.
Get Generation
result_url when the generation has succeeded and error_message when it has failed.
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
generation_id | string (UUID) | yes | The generation ID returned by the enqueue endpoint. |
Response (in progress)
Response (succeeded)
Response (failed)
Response (not found)
Returns 404 when the generation ID does not exist:Cancel Generation
Path Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
generation_id | string (UUID) | yes | The generation ID to cancel. |
Response (success)
Response (not found)
Returns 404 when the generation ID does not exist:queued or processing). Cancelling a generation that has already reached a terminal state (succeeded, failed, or cancelled) has no effect.
Completion Events
media_generation_completed events when generations reach a terminal state.
Connect
Content-Type: text/event-stream. The server sends : keep-alive\n\n every 15 seconds when idle.
Events are broadcast to currently-connected subscribers with no buffering. Anything that fires before you connect is lost. Subscribe before calling POST /ai/queue to avoid missing events.
Event: media_generation_completed
Fires once per generation on terminal state. Success wire format:Fields
| Field | succeeded | failed | Description |
|---|---|---|---|
generation_id | yes | yes | Matches the ID returned by POST /ai/queue |
status | "succeeded" | "failed" | Only these two values appear |
media_type | yes | yes | "image" or "video" |
model | yes | no | Model name |
url | yes | no | Presigned URL to the output file. Expires after a limited time. |
error | no | yes | Human-readable failure reason |
Other events on this stream
GET /api/events is a user-scoped stream that may carry unrelated event types (e.g. deployment events). Filter on the event: line and ignore anything other than media_generation_completed.
Example
Terminal states without events
media_generation_completed does not fire for cancelled generations (you called DELETE /ai/queue/:id). You can still retrieve the final status of any generation via GET /ai/queue/:id.
Examples
Batch image generation with polling
Async video generation
Poll a single generation by ID
End-to-end: enqueue, wait for SSE, download
Python
Errors
| Condition | Error |
|---|---|
num_generations out of range | "num_generations must be an integer between 1 and 4" |
| Model not found | "Model not found: <name>" |
| Text-only model | ":unsupported_media_type" |
| 404 on GET/DELETE | Generation ID does not exist |