Skip to content

@ai-sdk/replicate image model fails when sync-wait returns status:starting with output:null #16276

Description

@diegonava6

Description

@ai-sdk/replicate image generation intermittently throws AI_APICallError: Invalid JSON response (cause: AI_TypeValidationError) when Replicate returns a prediction that is still in progress.

This happens with model black-forest-labs/flux-dev during cold starts or when inference exceeds the sync-wait window.

Source (where the bug is)

packages/replicate/src/replicate-image-model.ts (v2.0.36).

The POST response is destructured directly into { output } with no status check (L140-L143):

const {
  value: { output },
  responseHeaders,
} = await postJsonToApi({

…and validated against a schema that requires output to be present (L206-L208):

const replicateImageResponseSchema = z.object({
  output: z.union([z.array(z.string()), z.string()]),
});

There is no polling. The video model in the same package already handles this correctly (see Suggested fix).

Expected behavior

When Replicate's sync API (Prefer: wait) times out before the prediction completes, Replicate returns HTTP 201 with a valid prediction object (status: "starting" or "processing", output: null, urls.get set). Per Replicate's docs, the client should poll urls.get until status is succeeded, then use output.

The video model in the same package already implements this (replicate-video-model.ts: keeps full prediction, polls while starting/processing, uses replicatePredictionSchema with output.nullish()).

The image model should behave the same way.

Actual behavior

replicate-image-model.ts posts with Prefer: wait, then immediately validates the response and throws. No polling occurs.

Note the response is actually HTTP 201 (success) — so this is not an HTTP/transport error, it is the success-path JSON schema rejecting output: null. The error message Invalid JSON response is misleading; the JSON is valid, the schema is just too narrow.

Top-level error thrown (what calling code catches):

AI_APICallError: Invalid JSON response
  name:       AI_APICallError
  statusCode: 201
  url:        https://api.replicate.com/v1/models/black-forest-labs/flux-dev/predictions
  cause:      AI_TypeValidationError

Underlying cause (AI_TypeValidationError):

Type validation failed: Value: { ... "output": null, ... "status": "starting" ... }
Error message: [
  {
    "code": "invalid_union",
    "errors": [
      [ { "expected": "array",  "code": "invalid_type", "path": [], "message": "Invalid input: expected array, received null" } ],
      [ { "expected": "string", "code": "invalid_type", "path": [], "message": "Invalid input: expected string, received null" } ]
    ],
    "path": ["output"],
    "message": "Invalid input"
  }
]

Reproduction

import { replicate } from '@ai-sdk/replicate';
import { generateImage } from 'ai';

await generateImage({
  model: replicate.image('black-forest-labs/flux-dev'),
  prompt: 'A watercolor landscape at twilight',
  size: '1024x1024',
});

Intermittent — more likely on cold starts / slow inference. When it fails, the response body (HTTP 201) looks like:

{
  "id": "...",
  "model": "black-forest-labs/flux-dev",
  "status": "starting",
  "output": null,
  "error": null,
  "urls": {
    "get": "https://api.replicate.com/v1/predictions/..."
  }
}

Suggested fix

Mirror the video model pattern in replicate-image-model.ts:

  1. Widen replicateImageResponseSchema to accept output.nullish(), plus status, error, and urls.get
  2. Keep the full prediction from the POST response (not { output })
  3. Poll urls.get while status is starting or processing
  4. Use finalPrediction.output once succeeded

Reference implementation: packages/replicate/src/replicate-video-model.ts (the while (status === 'starting' || status === 'processing') poll loop in doGenerate).

Related

  • Acknowledged gap in Make async APIs a first-class API in AI SDK #12381 (Replicate image gen may fall back to polling, but the SDK only implements polling for video today)
  • Still present on latest published @ai-sdk/replicate@2.0.36 and on main as of June 2026

AI SDK Version

  • ai: 6.0.168
  • @ai-sdk/replicate: 2.0.29 (also verified unfixed in 2.0.36)

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions