Make async APIs a first-class API in AI SDK
In some cases, provider APIs do not respond with the result to a user's prompt, but with a task ID or a callback URL which has to be polled until the result is ready and the method like generateVideo() can be resolved with the result. As an alternative to polling, most if not all provider APIs support a webhook URL to be passed. We have to support both ways and default to the long polling behavior we currently implement across some of our provider models. Support for Webhook URLs can depend on models within the same provider. Instead of passing a webhookUrl at call time, a user should also be able to pass an async callback function which resolves with a webhook URL, letting the provider decide whether to generate it or not depending on model configuration.
Users will be interested into the long poll mechanism, like using their own fetch method, tracking the number of poll requests, configure the poll interval (e.g. make it exponential/dynamic).
Let's discuss what API we want to implement. We are open to input!
Current architecture for context
Current Architecture (for context)
User calls: generateVideo({ model, prompt })
↓
Core function: model.doGenerate(options) ← returns Promise<Result>
↓
Provider impl: postJsonToApi(...) ← submit job
while (!done) { ← internal polling loop
await delay(interval)
status = await getFromApi()
}
return result ← resolves only when complete
The provider owns the entire lifecycle. The SDK core has zero visibility or control.Summary of current polling implementations across providers
11 files use the submit-then-poll pattern, where postJsonToApi() (or similar) does not directly return the response, but instead returns a URL or task ID that needs to be polled until the actual response is ready. They fall into three categories.
Image Generation (2)
@ai-sdk/black-forest-labs
- File:
packages/black-forest-labs/src/black-forest-labs-image-model.ts - Submit:
postJsonToApi→polling_url+id - Poll:
pollForImageUrl()—forloop with max attempts,getFromApito polling URL - Lines: 261–333
@ai-sdk/luma
- File:
packages/luma/src/luma-image-model.ts - Submit:
postJsonToApi→ generationid - Poll:
pollForImageUrl()—forloop with max attempts,getFromApito/generations/{id} - Lines: 153–198
Video Generation (5)
@ai-sdk/replicate
- File:
packages/replicate/src/replicate-video-model.ts - Submit:
postJsonToApi→ predictionid+urls.get - Poll:
whileloop indoGenerate,getFromApitourls.get - Lines: 193–254
@ai-sdk/fal (video)
- File:
packages/fal/src/fal-video-model.ts - Submit:
postJsonToApi→response_url - Poll:
while (true)indoGenerate,getFromApito response URL - Lines: 156–249
@ai-sdk/klingai
- File:
packages/klingai/src/klingai-video-model.ts - Submit:
postJsonToApi→task_id - Poll:
while (true)indoGenerate,getFromApito task status endpoint - Lines: 276–348
@ai-sdk/google (video)
- File:
packages/google/src/google-generative-ai-video-model.ts - Submit:
postJsonToApi→ operationname - Poll:
while (!done)indoGenerate,getFromApito operation URL - Lines: 185–251
@ai-sdk/google-vertex (video)
- File:
packages/google-vertex/src/google-vertex-video-model.ts - Submit:
postJsonToApi→ operationname - Poll:
while (!done)indoGenerate,postJsonToApitofetchPredictOperation - Lines: 181–251
Transcription (4)
@ai-sdk/assemblyai
- File:
packages/assemblyai/src/assemblyai-transcription-model.ts - Submit:
postJsonToApi→ transcriptid - Poll:
waitForCompletion()—while (true), directfetchto/v2/transcript/{id} - Lines: 268–329
@ai-sdk/revai
- File:
packages/revai/src/revai-transcription-model.ts - Submit:
postFormDataToApi→ jobid - Poll:
while (status !== 'transcribed')indoGenerate,getFromApito/jobs/{id} - Lines: 322–390
@ai-sdk/gladia
- File:
packages/gladia/src/gladia-transcription-model.ts - Submit:
postJsonToApi→result_url - Poll:
while (true)indoGenerate,getFromApito result URL - Lines: 525–589
@ai-sdk/fal (transcription)
- File:
packages/fal/src/fal-transcription-model.ts - Submit:
postJsonToApi→request_id - Poll:
while (true)indoGenerate,getFromApito request status URL - Lines: 136–232
Common Shape
All follow roughly the same pattern:
- Submit via
postJsonToApi(orpostFormDataToApi) → get back a task ID or polling URL - Poll in a loop (
while/for) callinggetFromApi(orfetch) with adelay()between iterations - Check status — looking for terminal states like
Ready,completed,succeed,done - Return the final result (image URL, video URL, transcript)
- Timeout protection via max attempts or elapsed time
Default Poll Intervals and Timeouts
| Package | Poll Interval | Timeout |
|---|---|---|
black-forest-labs |
500ms | 60s |
luma |
500ms | 120 attempts (~60s) |
replicate |
2000ms | 5 min |
fal (video) |
2000ms | 5 min |
klingai |
5000ms | 10 min |
google / google-vertex |
10000ms | 10 min |
assemblyai |
3000ms | none (polls indefinitely) |
revai |
1000ms | 60s |
gladia |
1000ms | 60s |
fal (transcription) |
1000ms | 60s |
Related
- https://github.com/vercel/ai/pull/11159
- https://github.com/vercel/ai/pull/10281, https://github.com/vercel/ai/pull/11913 - introduced most of the models with internal long polling
Source: vercel/ai