Light Logo
JavaScript SDK (v0.56.0)Guides

Predictions

A guide on how to work with predictions using the WriftAI JavaScript client

This section demonstrates common prediction operations you can perform with the WriftAI JavaScript client. These examples are not exhaustive — check the client reference for all options.

Get a Prediction by ID

const prediction = await wriftai.predictions.get('your-prediction-id');

Cancel a Prediction by ID

await wriftai.predictions.cancel('your-prediction-id');

Create a Prediction

With the latest version of a model

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
});

With a specific version of a model

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1:2',
  input: {
    prompt: 'Summarize quantum computing.',
  },
});

With a webhook for prediction updates

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
  webhook: {
    url: 'https://example.com/webhooks/wriftai',
    secret: 'top-secret', // This is optional
  },
});

With file inputs

File inputs are automatically uploaded when you create a prediction. Wrap a Uint8Array, Blob, or File in FileData, and no need to call wriftai.files.upload() separately.

Files can be nested anywhere in input, including inside arrays, objects, or combinations of both, and you can include as many as you need. They'll all be uploaded concurrently.

From a browser file input (<input type="file">):

import { FileData } from 'wriftai';

const prediction = await wriftai.predictions.create({
  model: 'pyannote/speaker-diarization-community-1',
  input: { audio: new FileData(input.files[0]) },
});

From constructed data:

import { FileData } from 'wriftai';

const blob = new Blob(recordedChunks, { type: 'audio/wav' });
const prediction = await wriftai.predictions.create({
  model: 'pyannote/speaker-diarization-community-1',
  input: { audio: new FileData(blob) },
});

From a fetch response:

import { FileData } from 'wriftai';

const res = await fetch('https://example.com/audio.wav');
const prediction = await wriftai.predictions.create({
  model: 'pyannote/speaker-diarization-community-1',
  input: { audio: new FileData(await res.blob(), 'audio/wav') },
});

From a file path (requires node:fs):

import { readFile } from 'node:fs/promises';
import { FileData } from 'wriftai';

const prediction = await wriftai.predictions.create({
  model: 'pyannote/speaker-diarization-community-1',
  input: { audio: new FileData(await readFile('./audio.wav'), 'audio/wav') },
});

For large files, use openAsBlob to stream from disk without loading the entire file into memory (requires node:fs):

import { openAsBlob } from 'node:fs';
import { FileData } from 'wriftai';

const prediction = await wriftai.predictions.create({
  model: 'pyannote/speaker-diarization-community-1',
  input: {
    audio: new FileData(await openAsBlob('./long-recording.wav'), 'audio/wav'),
  },
});

For more details on working with files, see the Files guide.

With input validation enabled

Enable early input validation against the model’s input schema before a prediction is created. This catches invalid inputs upfront and prevents unnecessary model execution and cost.

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
  validateInput: true,
});

Create and wait for completion

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
  wait: true,
});

This returns a finished prediction. The server holds the connection open for its default wait time, and if the prediction hasn't finished by then, the client continues polling until it reaches a terminal state.

Create and wait with a custom duration

Set how long the server holds the initial request open before the client falls back to polling:

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
  wait: { duration: 30 },
});

This still returns a finished prediction. duration only changes how long the initial request stays open before polling starts. Use a lower value if your environment has a short HTTP timeout, or a higher one (up to the server's maximum) to get the result as soon as it's ready with fewer polling requests.

Create and wait without polling

Set poll to false to return as soon as the server's wait ends, even if the prediction is still running:

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
  wait: { duration: 6, poll: false },
});

The returned prediction may not be in a terminal state, so check its status.

Create and wait with progress updates

const prediction = await wriftai.predictions.create({
  model: 'deepseek-ai/deepseek-r1',
  input: {
    prompt: 'Summarize quantum computing.',
  },
  wait: {
    pollInterval: 2000,
    onPoll: async (prediction) => {
      // your custom logic
    },
  },
});

pollInterval and onPoll only take effect while the client is polling. They do not affect the initial wait.

Wait for an existing prediction to complete

const prediction = await wriftai.predictions.wait('your-prediction-id');

This is also what create(..., true) uses to poll when the prediction hasn't finished after the server's wait, so calling it directly is useful when you want to skip straight to polling, or you already have a prediction ID from somewhere else.

It takes the same pollInterval and onPoll options shown above via WaitOptions instead of CreateWaitOptions:

const prediction = await wriftai.predictions.wait('your-prediction-id', {
  pollInterval: 2000,
  onPoll: async (prediction) => {
    // your custom logic
  },
});

List Predictions

const predictions = await wriftai.predictions.list();

Filter by status

import { Status } from 'wriftai';

const predictions = await wriftai.predictions.list({
  statuses: [Status.failed, Status.cancelled],
});