Table of contents
Some models generate files as output, like images, audio, or video. With the release of version 1.0.0 of our client libraries, the way you handle these output files has changed.
When a model generates files, replicate.run()
now returns FileOutput
objects instead of URLs. These objects provide direct access to the file data, simplifying your code and speeding up your applications.
Here's how you can work with FileOutput
objects:
The FileOutput
type mimics a file-like object available on the platform.
Each Python FileOutput
object implements Iterator[bytes]
and AsyncIterator[bytes]
and provides:
read()
: Returns the binary content of the fileurl
: The URL of the underlying data sourceEach JavaScript FileOutput
object is a ReadableStream
that also provides:
blob()
: Reads the binary content of the filepipe()
: Stream the file data to a WriteableStream
instance.url()
: The URL of the underlying data sourceIn Python, FileOutput
implements Iterator[bytes]
and AsyncIterator[bytes]
which means you can pass it to any function that takes one as input:
In JavaScript, FileOutput
implements ReadableStream
which means you can pass it to any function that takes one as input (such as Response
and fs.writeFile
) to stream large files efficiently:
Sometimes you might want to use the file's URL directly - for example, when displaying images in a web application or passing the URL to another service. Each FileOutput
object has a url
property:
Remember: URLs for files will point to replicate.delivery
and will expire after one hour.
If you're updating from a version before 1.0.0:
FileOutput
objectread()
or pipe()
to access file data instead of downloading from URLsurl
property of the FileOutput
objectIf you prefer not to use the blocking API, you can opt for the polling mode. This allows you to handle predictions asynchronously and can be useful if you want to avoid holding a connection open. To use polling mode, pass the relevant argument to the run()
method in your favorite language:
You can also opt out of FileOutput
objects entirely by configuring the client when you create it:
This will make the client return URLs instead of FileOutput
objects, which can be useful if you're migrating from an older version or prefer to handle the files yourself.
For predictions created through the API, output files are automatically deleted after an hour. You must save a copy of any files in the output if you'd like to continue using them. For more details on how to store prediction data, see the webhooks docs.
For predictions created through the web interface, output files are kept indefinitely, unless you delete them manually.
Output files are served by replicate.delivery
and its subdomains.
If you use an allow list of external domains for your assets, add replicate.delivery
and *.replicate.delivery
to it.
For example, if you're building a Next.js app that displays output files from Replicate, update your Next.js config as follows:
Remember to update your code to work with FileOutput
objects if you're using version 1.0.0 or later of our client libraries.