Light Logo
Go SDK (v0.47.5)Guides

Predictions

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

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

Get a Prediction by ID

prediction, err := client.Predictions.Get(context.TODO(), "your-prediction-id")

Cancel a Prediction by ID

err := client.Predictions.Cancel(context.TODO(), "your-prediction-id")

Create a Prediction

Pass the prediction input as a wriftai.PredictionInput, a map of input names to values. Any value that encoding/json can marshal is allowed, and a plain map[string]any also works.

With the latest version of a model

prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1",
		Input: wriftai.PredictionInput{
			"prompt": "Summarize quantum computing.",
		},
	},
	false, // do not wait for completion
	nil,   // no custom wait options
)

With a specific version of a model

prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1:2",
		Input: wriftai.PredictionInput{
			"prompt": "Summarize quantum computing.",
		},
	},
	false, // do not wait for completion
	nil,   // no custom wait options
)

With raw JSON values

Values that are already encoded can be passed as json.RawMessage. They are sent as-is and can be mixed with regular Go values.

prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1",
		Input: wriftai.PredictionInput{
			"prompt":  "Summarize quantum computing.",
			"options": json.RawMessage(`{"temperature":0.7}`),
		},
	},
	false, // do not wait for completion
	nil,   // no custom wait options
)

With a webhook for prediction updates

webhookSecret := "top-secret"

prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1",
		Input: wriftai.PredictionInput{
			"prompt": "Summarize quantum computing.",
		},
		Webhook: &wriftai.Webhook{
			URL:    "https://example.com/webhooks/wriftai",
			Secret: &webhookSecret, // This is optional
		},
	},
	false, // do not wait for completion
	nil,   // no custom wait options
)

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.

validate := true
prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1",
		Input: wriftai.PredictionInput{
			"prompt": "Summarize quantum computing.",
		},
		ValidateInput: &validate,
	},
	false, // do not wait for completion
	nil,   // no custom wait options
)

Create and wait for completion

prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1",
		Input: wriftai.PredictionInput{
			"prompt": "Summarize quantum computing.",
		},
	},
	true, // wait for completion
	nil,  // no custom wait options
)

Create and wait with custom options

onPoll := func(pred *wriftai.PredictionWithDetails) error {
	// your custom logic
	return nil
}
options := wriftai.WaitOptions{PollInterval: time.Second, OnPoll: onPoll}

prediction, err := client.Predictions.Create(
	context.TODO(),
	wriftai.CreatePredictionParams{
		Model: "deepseek-ai/deepseek-r1",
		Input: wriftai.PredictionInput{
			"prompt": "Summarize quantum computing.",
		},
	},
	true,     // wait for completion
	&options, // custom wait options
)

Wait for an existing prediction to complete

prediction, err := client.Predictions.Wait(
	context.TODO(),
	"your-prediction-id",
	nil, // no custom wait options
)

List Predictions

predictions, err := client.Predictions.List(
	context.TODO(),
	nil, // default pagination options
)

Predictions filtered by status

statuses := []wriftai.Status{wriftai.StatusFailed}
predictions, err := client.Predictions.List(
	context.TODO(),
	&wriftai.PredictionPaginationOptions{
		Statuses: &statuses,
	},
)