You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@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.
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.
import{replicate}from'@ai-sdk/replicate';import{generateImage}from'ai';awaitgenerateImage({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:
Description
@ai-sdk/replicateimage generation intermittently throwsAI_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-devduring 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):…and validated against a schema that requires
outputto be present (L206-L208):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.getset). Per Replicate's docs, the client should pollurls.getuntilstatusissucceeded, then useoutput.The video model in the same package already implements this (
replicate-video-model.ts: keeps fullprediction, polls whilestarting/processing, usesreplicatePredictionSchemawithoutput.nullish()).The image model should behave the same way.
Actual behavior
replicate-image-model.tsposts withPrefer: 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 messageInvalid JSON responseis misleading; the JSON is valid, the schema is just too narrow.Top-level error thrown (what calling code catches):
Underlying cause (
AI_TypeValidationError):Reproduction
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:replicateImageResponseSchemato acceptoutput.nullish(), plusstatus,error, andurls.getpredictionfrom the POST response (not{ output })urls.getwhilestatusisstartingorprocessingfinalPrediction.outputoncesucceededReference implementation:
packages/replicate/src/replicate-video-model.ts(thewhile (status === 'starting' || status === 'processing')poll loop indoGenerate).Related
@ai-sdk/replicate@2.0.36and onmainas of June 2026AI SDK Version
ai: 6.0.168@ai-sdk/replicate: 2.0.29 (also verified unfixed in 2.0.36)Code of Conduct