Skip to content

Commit d031695

Browse files
authored
Document the FileOutput and wait parameters (#326)
This commit takes a first pass at documenting the `FileOutput` and `wait` parameters introduced in 1.0.0 as well as calling out various potential gotchas with the API around URLs and data-uris based on feedback from the issues.
1 parent 8b75b39 commit d031695

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

README.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ console.log(output.url()); // 'https://replicate.delivery/pbxt/GtQb3Sgve42ZZyVnt
6464
console.log(output.blob()); // Blob
6565
```
6666

67+
> [!NOTE]
68+
> A model that outputs file data returns a `FileOutput` object by default. This is an implementation
69+
> of `ReadableStream` that returns the file contents. It has a `.blob()` method for accessing a
70+
> `Blob` representation and a `.url()` method that will return the underlying data-source.
71+
>
72+
> **This data source can be either a remote URL or a data-uri with base64 encoded data. Check
73+
> out the documentation on [creating a prediction](https://replicate.com/docs/topics/predictions/create-a-prediction)
74+
> for more information.**
75+
6776
You can also run a model in the background:
6877

6978
```js
@@ -277,6 +286,7 @@ const replicate = new Replicate(options);
277286
| `options.baseUrl` | string | Defaults to https://api.replicate.com/v1 |
278287
| `options.fetch` | function | Fetch function to use. Defaults to `globalThis.fetch` |
279288
| `options.fileEncodingStrategy` | string | Determines the file encoding strategy to use. Possible values: `"default"`, `"upload"`, or `"data-uri"`. Defaults to `"default"` |
289+
| `options.useFileOutput` | boolean | Determines if the `replicate.run()` method should convert file output into `FileOutput` objects |
280290

281291

282292
The client makes requests to Replicate's API using
@@ -333,16 +343,23 @@ const output = await replicate.run(identifier, options, progress);
333343
| ------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
334344
| `identifier` | string | **Required**. The model version identifier in the format `{owner}/{name}:{version}`, for example `stability-ai/sdxl:8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f` |
335345
| `options.input` | object | **Required**. An object with the model inputs. |
336-
| `options.wait` | object | Options for waiting for the prediction to finish |
337-
| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 |
346+
| `options.wait` | object | Options for waiting for the prediction to finish |
347+
| `options.wait.type` | `"poll" \| "block"` | `"block"` holds the request open, `"poll"` makes repeated requests to fetch the prediction. Defaults to `"block"` |
348+
| `options.wait.interval` | number | Polling interval in milliseconds. Defaults to 500 |
349+
| `options.wait.timeout` | number | In `"block"` mode determines how long the request will be held open until falling back to polling. Defaults to 60 |
338350
| `options.webhook` | string | An HTTPS URL for receiving a webhook when the prediction has new output |
339351
| `options.webhook_events_filter` | string[] | An array of events which should trigger [webhooks](https://replicate.com/docs/webhooks). Allowable values are `start`, `output`, `logs`, and `completed` |
340352
| `options.signal` | object | An [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to cancel the prediction |
341353
| `progress` | function | Callback function that receives the prediction object as it's updated. The function is called when the prediction is created, each time it's updated while polling for completion, and when it's completed. |
342354

343355
Throws `Error` if the prediction failed.
344356

345-
Returns `Promise<object>` which resolves with the output of running the model.
357+
Returns `Promise<unknown>` which resolves with the output of running the model.
358+
359+
> [!NOTE]
360+
> Currently the TypeScript return type of `replicate.run()` is `Promise<object>` this is
361+
> misleading as a model can return array types as well as primative types like strings,
362+
> numbers and booleans.
346363
347364
Example:
348365

@@ -364,6 +381,26 @@ const onProgress = (prediction) => {
364381
const output = await replicate.run(model, { input }, onProgress)
365382
```
366383

384+
#### Sync vs. Async API (`"poll"` vs. `"block"`)
385+
386+
The `replicate.run()` API takes advantage of the [Replicate sync API](https://replicate.com/docs/topics/predictions/create-a-prediction)
387+
which is optimized for low latency requests to file models like `black-forest-labs/flux-dev` and
388+
`black-forest-labs/flux-schnell`. When creating a prediction this will hold a connection open to the
389+
server and return a `FileObject` containing the generated file as quickly as possible.
390+
391+
> [!NOTE]
392+
> In this mode the `url()` method on the `FileObject` may refer to either a remote URL or
393+
> base64 encoded data-uri. The latter is an optimization we make on certain models to deliver
394+
> the files faster to the client.
395+
>
396+
> If you need the prediction URLs for whatever reason you can opt out of the sync mode by
397+
> passing `wait: { "type": "poll" }` to the `run()` method.
398+
>
399+
> ```js
400+
> const output = await replicate.run(model, { input, wait: { type: "poll" } });
401+
> output.url() // URL<https://...>
402+
> ```
403+
367404
### `replicate.stream`
368405
369406
Run a model and stream its output. Unlike [`replicate.prediction.create`](#replicatepredictionscreate), this method returns only the prediction output rather than the entire prediction object.
@@ -1192,6 +1229,41 @@ The `replicate.request()` method is used by the other methods
11921229
to interact with the Replicate API.
11931230
You can call this method directly to make other requests to the API.
11941231

1232+
### `FileOutput`
1233+
1234+
`FileOutput` is a `ReadableStream` instance that represents a model file output. It can be used to stream file data to disk or as a `Response` body to an HTTP request.
1235+
1236+
```javascript
1237+
const [output] = await replicate.run("black-forest-labs/flux-schnell", {
1238+
input: { prompt: "astronaut riding a rocket like a horse" }
1239+
});
1240+
1241+
// To access the file URL (or data-uri):
1242+
console.log(output.url()); //=> "http://example.com"
1243+
1244+
// To write the file to disk:
1245+
fs.writeFile("my-image.png", output);
1246+
1247+
// To stream the file back to a browser:
1248+
return new Response(output);
1249+
1250+
// To read the file in chunks:
1251+
for await (const chunk of output) {
1252+
console.log(chunk); // UInt8Array
1253+
}
1254+
```
1255+
1256+
You can opt out of FileOutput by passing `useFileOutput: false` to the `Replicate` constructor:
1257+
1258+
```javascript
1259+
const replicate = new Replicate({ useFileOutput: false });
1260+
```
1261+
1262+
| method | returns | description |
1263+
| -------------------- | ------ | ------------------------------------------------------------ |
1264+
| `url()` | string | A `URL` object representing the HTTP URL or data-uri |
1265+
| `blob()` | object | A `Blob` instance containing the binary file |
1266+
11951267
## Troubleshooting
11961268

11971269
### Predictions hanging in Next.js

0 commit comments

Comments
 (0)