Skip to content

Commit 61f60ac

Browse files
committed
Add block parameter to prediction creation methods
1 parent ac47fb4 commit 61f60ac

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ declare module "replicate" {
210210
deployment_name: string,
211211
options: {
212212
input: object;
213+
/** @deprecated */
213214
stream?: boolean;
214215
webhook?: string;
215216
webhook_events_filter?: WebhookEventType[];
217+
block?: boolean;
216218
}
217219
): Promise<Prediction>;
218220
};
@@ -301,6 +303,7 @@ declare module "replicate" {
301303
stream?: boolean;
302304
webhook?: string;
303305
webhook_events_filter?: WebhookEventType[];
306+
block?: boolean;
304307
} & ({ version: string } | { model: string })
305308
): Promise<Prediction>;
306309
get(prediction_id: string): Promise<Prediction>;

lib/deployments.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const { transformFileInputs } = require("./util");
77
* @param {string} deployment_name - Required. The name of the deployment
88
* @param {object} options
99
* @param {object} options.input - Required. An object with the model inputs
10-
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false
1110
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
1211
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
12+
* @param {boolean} [options.block] - Whether to wait until the prediction is completed before returning. Defaults to false
1313
* @returns {Promise<object>} Resolves with the created prediction data
1414
*/
1515
async function createPrediction(deployment_owner, deployment_name, options) {
16-
const { stream, input, ...data } = options;
16+
const { input, block, ...data } = options;
1717

1818
if (data.webhook) {
1919
try {
@@ -24,10 +24,16 @@ async function createPrediction(deployment_owner, deployment_name, options) {
2424
}
2525
}
2626

27+
const headers = {};
28+
if (block) {
29+
headers["X-Sync"] = "true";
30+
}
31+
2732
const response = await this.request(
2833
`/deployments/${deployment_owner}/${deployment_name}/predictions`,
2934
{
3035
method: "POST",
36+
headers,
3137
data: {
3238
...data,
3339
input: await transformFileInputs(

lib/predictions.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const { transformFileInputs } = require("./util");
99
* @param {object} options.input - Required. An object with the model inputs
1010
* @param {string} [options.webhook] - An HTTPS URL for receiving a webhook when the prediction has new output
1111
* @param {string[]} [options.webhook_events_filter] - You can change which events trigger webhook requests by specifying webhook events (`start`|`output`|`logs`|`completed`)
12-
* @param {boolean} [options.stream] - Whether to stream the prediction output. Defaults to false. Streaming is now enabled by default for all predictions. For more information, see https://replicate.com/changelog/2024-07-15-streams-always-available-stream-parameter-deprecated
12+
* @param {boolean} [options.block] - Whether to wait until the prediction is completed before returning. Defaults to false
1313
* @returns {Promise<object>} Resolves with the created prediction
1414
*/
1515
async function createPrediction(options) {
16-
const { model, version, input, ...data } = options;
16+
const { model, version, input, block, ...data } = options;
1717

1818
if (data.webhook) {
1919
try {
@@ -24,10 +24,16 @@ async function createPrediction(options) {
2424
}
2525
}
2626

27+
const headers = {};
28+
if (block) {
29+
headers["X-Sync"] = "true";
30+
}
31+
2732
let response;
2833
if (version) {
2934
response = await this.request("/predictions", {
3035
method: "POST",
36+
headers,
3137
data: {
3238
...data,
3339
input: await transformFileInputs(
@@ -41,6 +47,7 @@ async function createPrediction(options) {
4147
} else if (model) {
4248
response = await this.request(`/models/${model}/predictions`, {
4349
method: "POST",
50+
headers,
4451
data: {
4552
...data,
4653
input: await transformFileInputs(

0 commit comments

Comments
 (0)