Skip to content

feat(replicate): add configurable maxWaitTimeInSeconds option for image generation#11296

Merged
lgrammel merged 4 commits intomainfrom
copilot/replicate-prefer-wait-fix
Dec 19, 2025
Merged

feat(replicate): add configurable maxWaitTimeInSeconds option for image generation#11296
lgrammel merged 4 commits intomainfrom
copilot/replicate-prefer-wait-fix

Conversation

Copy link
Contributor

Copilot AI commented Dec 19, 2025

Background

The Replicate provider hardcodes prefer: 'wait' header, capping predictions at 60 seconds. Predictions exceeding this time fail silently. This affects popular models like nano banana pro that regularly exceed 60s.

The flexible wait feature is documented in Replicate's API docs:

Summary

Added maxWaitTimeInSeconds provider option to control sync wait duration:

  • Not specified: Default 60s sync wait (prefer: wait)
  • Positive number: Custom duration (prefer: wait=N)
await replicate.image('model').doGenerate({
  prompt: 'A beautiful sunset',
  providerOptions: {
    replicate: {
      maxWaitTimeInSeconds: 120, // Wait up to 120 seconds
    },
  },
});

Manual Verification

Ran existing and new unit tests covering both header scenarios. Verified header construction logic and that maxWaitTimeInSeconds is excluded from request body.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Future Work

  • Internal polling for non-terminal prediction states when sync mode times out
Original prompt

This section details on the original issue you should resolve

<issue_title>Replicate provider hard‑codes Prefer: wait (60s cap) and fails on long predictions</issue_title>
<issue_description>### Description

The Replicate provider passes prefer: 'wait' to all image generations

headers: combineHeaders(await resolve(this.config.headers), headers, {
prefer: 'wait',
}),

According to Replicate's docs here: https://replicate.com/docs/reference/http/#predictions.create

The request will wait up to 60 seconds for the model to run. If this time is exceeded the prediction will be returned in a "starting" state and need to be retrieved using the predictions.get endpoint.

And on the docs under create a prediction: https://replicate.com/docs/topics/predictions/create-a-prediction#timeout-duration

The default duration for sync mode is 60 seconds, but you can specify a different timeout duration in the header if needed. For example, Prefer: wait=5 will wait for 5 seconds.

If the model doesn’t finish within the specified duration, the request will return the incomplete prediction object with status set to starting or processing. You can then fetch the prediction again via the URL provided in the Location header, or the urls.get field as with Async mode.

So currently, any prediction that is longer than 60s just fails. This happens often with popular models like nano banana pro (in my own usage).

Suggestion: allow configuring the wait duration (e.g., Prefer: wait=n) or allow disabling sync wait entirely (no Prefer header). Alternative: detect non‑terminal prediction status and poll internally (or expose a helper to do so).

AI SDK Version

  • ai: 5.0.102
  • @ai-sdk/replicate version: 1.0.18

Code of Conduct

  • I agree to follow this project's Code of Conduct</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ge generation

Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix hard-coded prefer option for image generation feat(replicate): add configurable maxWaitTimeInSeconds option for image generation Dec 19, 2025
Copilot AI requested a review from lgrammel December 19, 2025 09:16
Copy link
Collaborator

@lgrammel lgrammel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a changeset, update the replicate provider docs, and show me where in the replicate docs flexible waits are defined

…eInSeconds

Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>

// Build the prefer header based on maxWaitTimeInSeconds:
// - undefined/null: use default sync wait (prefer: wait)
// - 0: disable sync mode (no prefer header)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is disabling the sync mode supported in our code? if not we should not support it in the header (ie remove that part here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the maxWaitTimeInSeconds: 0 (async mode) support since the provider code expects output to be immediately available and doesn't handle polling for async predictions. Changed the schema to use z.number().positive().nullish() and simplified the header logic in commit 196b213.

*
* - When not specified: Uses default 60-second sync wait (`prefer: wait`)
* - When set to a positive number: Uses that duration (`prefer: wait=N`)
* - When set to 0: Disables sync mode (no `prefer` header), returning immediately
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async mode might not be supported in the provider; rm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the async mode documentation from the schema comments in commit 196b213.

…'s not implemented

Co-authored-by: lgrammel <205036+lgrammel@users.noreply.github.com>
@lgrammel lgrammel marked this pull request as ready for review December 19, 2025 10:06
@lgrammel lgrammel enabled auto-merge (squash) December 19, 2025 10:07
@lgrammel lgrammel disabled auto-merge December 19, 2025 10:11
@lgrammel lgrammel merged commit d0920f9 into main Dec 19, 2025
20 checks passed
@lgrammel lgrammel deleted the copilot/replicate-prefer-wait-fix branch December 19, 2025 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replicate provider hard‑codes Prefer: wait (60s cap) and fails on long predictions

2 participants