Getting started

The Python SDK allows you to interact with APIs directly from a python environment.

The following document is a step by step usage example of the Python SDK. If your looking for the full documentation of the software you will find it here.

Don’t already have a account established? Head over to this link, follow the instructions and come back to this page!


You need to have an account at or on an on-premise version installed in your company. Contact us or your IT manager for more information.

You will be working on a specific “instance”. This instance corresponds to the subdomain at the beginning of the url in your address: https://<your instance>

Get the package

pip install previsionio

Set up your client’s SDK client uses a specific master token to authenticate with the instance’s server and allows you to perform various requests. To get your master token, log in the online interface of your instance, navigate to the admin page and copy the token.

You can either set the token and the instance name as environment variables, by specifying PREVISION_URL and PREVISION_MASTER_TOKEN, or at the beginning of your script:

import previsionio as pio

# The client is initialized with your master token and the url of the server
# (or local installation, if applicable)
url = "https://<your instance>"
token = "<your token>"
pio.client.init_client(url, token)

# You can manage the verbosity (only output warnings and errors by default)
    False,           # whether to activate info logging
    debug=False,     # whether to activate detailed debug logging
    event_log=False, # whether to activate detailed event managers debug logging

# You can manage the duration you wish to wait for an asynchronous response
pio.config.default_timeout = 3600

# You can manage the number of retries for each call to the API
pio.config.request_retries = 6

# You can manage the duration of retry for each call to the API
pio.config.request_retry_time = 10

Create a project

First things first, to upload data or train an experiment, you need to create a project.

# create project
project ="project_name",
                          description="project description")


To train an experiment, you need to gather some training data. This data must be uploaded to your instance using either a data source, a file path or a pandas.DataFrame.

Managing datasources & connectors

Datasources and connectors are’s way of keeping a link to a source of data and taking snapshots when needed. The avaible data sources are:

  • SQL
  • FTP
  • SFTP
  • S3
  • GCP

Connectors hold the credentials to connect to the distant data sources. Then you can specify the exact resource to extract from a data source (be it the path to the file to load, the name of the database table to parse, …).

Creating a connector

To create a connector, use the appropriate method of project class. For example, to create a connector to an SQL database, use the create_sql_connector() and pass in your credentials:

connector = project.create_sql_connector('my_sql_connector',

For more information on all the available connectors, check out the Project full documentation.

Creating a data source

After you’ve created a connector, you need to use a datasource to actually refer to and fetch a resource in the distant data source. To create a datasource, you need to link the matching connector and to supply the relevant info, depending on the connector type:

datasource = project.create_datasource(connector,

For more details on the creation of a datasource, check out the Project full documentation of the method create_datasource.

You can then create datasets from this datasource as explained in Uploading Data.

Listing available connectors and data sources

Connectors and datasources already registered on your workspace can be listed using the list_connectors() and list_datasource() method from project class:

connectors = project.list_connectors()
for connector in connectors:

datasources = project.list_datasource()
for datasource in datasources:

Uploading Data

You can upload data from three different sources: a path to a local (csv, zip) file, a pandas.DataFrame or a created data source

# Upload tabular data from a CSV file
data_path = 'path/to/your/data.csv'
dataset = project.create_dataset(name='helloworld', file_name=data_path)

# or use a pandas DataFrame
dataframe = pd.read_csv(data_path)
dataset = project.create_dataset(name='helloworld', dataframe=dataframe)

# or use a created data source
datasource = pio.DataSource.from_id('my_datasource_id')
dataset = project.create_dataset(name='helloworld', datasource=datasource)

# Upload an image folder
image_folder_path = 'path/to/your/'
image_folder = project.create_image_folder(name='helloworld', file_name=image_folder_path)

This will automatically upload the data as a new dataset in your workspace. If you go to the online interface, you will see this new dataset in the list of datasets (in the “Data” tab).

Listing available datasets

To get a list of all the datasets currently available in your workspace, use the list_datasets() method:

# List tabular datasets
datasets = project.list_datasets()
for dataset in datasets:

# List image folders
image_folders = project.list_image_folders()
for folder in image_folders:

Downloading data from your workspace

If you created or uploaded a dataset in your workspace and want to grab it locally, simply use the method:

out_path ="your/local/path")

Regression/Classification/Multi-classification experiments

Configuring the dataset

To start an experiment you need to specify the dataset to be used and its configuration (target column, weight column, id column, …). To get a full documentation check the api reference of the ColumnConfig in Experiment configuration.

column_config = pio.ColumnConfig(target_column='TARGET', id_column='ID')

Configuring the training parameters

If you want, you can also specify some training parameters, such as which models are used, which transformations are applied, and how the models are optimized. To get a full documentation check the api reference of the TrainingConfig in Experiment configuration.

training_config = pio.TrainingConfig(

Starting training

You can now create a new experiment based on:

  • an experiment name
  • a dataset
  • a column config
  • (optional) a metric type
  • (optional) a training config
  • (optional) a holdout dataset (dataset only used for evaluation)
experiment_version = project.fit_classification(

If you want to use image data for your experiment, you need to provide the API with both the tabular dataset and the image folder:

experiment_version = project.fit_image_classification(

To get an exhaustive list of the available metrics go to the api reference Metrics.

Making predictions

To make predictions from a dataset and an experiment, you need to wait until at least one model is trained. This can be achieved in the following way:

# block until there is at least 1 model trained
experiment_version.wait_until(lambda experimentv: len(experimentv.models) > 0)

# check out the experiment status and other info
print('Current (best model) score:', experiment_version.score)


The wait_until method takes a function that takes the experiment as an argument, and can therefore access any info relative to the experiment.

Then you have to options:

  1. you can predict from a dataset of your workspace, which returns a previsionio.ValidationPrediction object. It allows you to keep on working even if the prediction isn’t complete
  2. you can predict from a pd.DataFrame, which returns a pd.DataFrame once the prediction is complete
# predict from a dataset of your workspace
validation_prediction = experiment_version.predict_from_dataset(test_dataset)
# get the result at a pandas.DataFrame
prediction_df = validation_prediction.get_result()

# predict from a pandas.DataFrame
prediction_df = experiment_version.predict(test_dataframe)

Time Series experiments

A time series experiment is very similar to a regression experiment. The main differences rely in the dataset configuration, and the specification of a time window.

Configuring the dataset

Here you need to specify which column in the dataset defines the time steps. Also you can specify the group_columns (columns defining a unique time serie) as well as the apriori_columns (columns containing information known in advanced):

column_config = pio.ColumnConfig(
    group_columns=['Store', 'Product'],

Configuring the training parameters

The training config is the same as for a regression experiment (detailed in Configuring the training parameters).

Starting training

You can now create a new experiment based on:

  • an experiment name
  • a dataset
  • a column config
  • a time window
  • (optional) a metric type
  • (optional) a training config

In particular the time_window parameter defines the period in the past that you have for each prediction, and the period in the future that you want to predict:

# Define your time window:
# example here using 2 weeks in the past to predict the next week
time_window = pio.TimeWindow(

experiment_version = project.fit_timeseries_regression(

To get a full documentation check the api reference Time Series experiments.

Making predictions

The prediction workflow is the same as for a classic experiment (detailed in Making predictions).

Text Similarity experiments

A Text Similarity experiment matches the most similar texts between a dataset containing descriptions (can be seen as a catalog) and a dataset containing queries. It first converts texts to numerical vectors (text embeddings) and then performs a similarity search to retrieve the most similar documents to a query.

Configuring the datasets

To start an experiment you need to specify the datasets to be used and their configuration. Note that a DescriptionsDataset is required while a QueriesDataset is optional during training (used for scoring).

# Required: configuration of the DescriptionsDataset
description_column_config = pio.TextSimilarity.DescriptionsColumnConfig(

# Optional: configuration of the QueriesDataset
queries_column_config = pio.TextSimilarity.QueriesColumnConfig(

To get a full documentation check the api reference of DescriptionsColumnConfig and QueriesColumnConfig.

Configuring the training parameters

If you want, you can also specify some training parameters, such as which embedding models, searching models and preprocessing are used. Here you need to specify one configuration per embedding model you want to use:

# Using TF-IDF as embedding model
models_parameters_1 = pio.ModelsParameters(
    models=[pio.TextSimilarityModels.BruteForce, pio.TextSimilarityModels.ClusterPruning],

# Using Transformer as embedding model
models_parameters_2 = pio.ModelsParameters(
    models=[pio.TextSimilarityModels.BruteForce, pio.TextSimilarityModels.IVFOPQ],

# Using fine-tuned Transformer as embedding model
models_parameters_3 = pio.ModelsParameters(
    models=[pio.TextSimilarityModels.BruteForce, pio.TextSimilarityModels.IVFOPQ],

# Gather everything
models_parameters = [models_parameters_1, models_parameters_2, models_parameters_3]
models_parameters = pio.ListModelsParameters(models_parameters=models_parameters)

To get a full documentation check the api reference of ModelsParameters.


If you want the default configuration of text similarity models, simply use:

models_parameters = pio.ListModelsParameters()

Starting the training

You can then create a new text similarity experiment based on:

  • an experiment name
  • a dataset
  • a description column config
  • (optional) a queries dataset
  • (optional) a queries column config
  • (optional) a metric type
  • (optional) the number of top k results you want per query
  • (optional) a language
  • (optional) a models parameters list
experiment_verion = project.fit_text_similarity(

To get a full documentation check the api reference of previsionio.metrics.TextSimilarity.

Making predictions

The prediction workflow is very similar to a classic experiment (detailed in Making predictions).

The only differences are the specific parameters top_k and queries_dataset_matching_id_description_column which are optional.

To get a full documentation check the api reference of TextSimilarityModel prediction methods.

External Regression/Classification/MultiClassification experiments

Preparing your external models

Before creating an external experiment, you have to define a non-empty list of tuple where each tuple contains 3 items describing an external model as follows:

  1. the name you want to give to the model
  2. the path to the model in onnx format
  3. the path to a yaml file containing metadata about the model
external_models = [
    ('my_first_external_model', 'model_1.onnx', 'model_1.yaml'),
    ('my_second_external_model', 'model_2.onnx', 'model_2.yaml'),

Experiment creation

You can now create a new experiment based on:

  • an experiment name
  • an holdout dataset
  • a target column name
  • a list of tuple containing external models
  • (optional) a metric type
  • (optional) the experiment version description
experiment_version = project.create_external_regression(

To get an exhaustive list of the available metrics go to the api reference Metrics.

Making predictions

The prediction workflow is the same as for classic experiment (detailed in Making predictions).

Deployed experiments’s SDK allows to deploy an experiment’s models. Deployed models are made available for unit and bulk prediction through apis. Then you can follow the usage of a model and the evolution of its input features distribution.

You first need to deploy a main model (and a challenger model) from an existing experiment:

# retrieve the best model of your experiment
experiment_version_best_model = experiment_version.best_model

# deploy the experiment model
experiment_deployment = project.create_experiment_deployment(

Now you can make bulk predictions from your deployed model(s):

# make predictions
deployment_prediction = experiment_deployment.predict_from_dataset(test_dataset)

# retrieve prediction from main model
prediction_df = deployment_prediction.get_result()

# retrieve prediction from challenger model (if any)
prediction_df = deployment_prediction.get_challenger_result()

To get a full documentation check the api reference Experiment Deployment.

You can also make unitary predictions from the main model:

# create an api key for your model

# retrieve the last client id and client secret
creds = experiment_deployment.get_api_keys()[-1]

# initialize the deployed model with its url, your client id and secret
model = pio.DeployedModel(

# make a prediction
prediction, confidence, explain = model.predict(
    predict_data={'feature1': 0, 'feature2': 42},

To get a full documentation check the api reference Deployed model.


Once you trained a model and made predictions from it you might want to export your results on a remote filesystem/database. To do so you will need a registered connector on your project (described in section Creating a connector).

Creating an exporter

The first step is to create an exporter in your project:

exporter = project.create_exporter(
    name = 'my_exporter',
    write_mode = pio.ExporterWriteMode.timestamp,

To get a full documentation check the api reference Exporter.


Once your exporter is operational you can export your datasets or predictions:

# export a dataset stored in your project
export = exporter.export_dataset(

# export a prediction stored in your project
export = exporter.export_prediction(

To get a full documentation check the api reference Export.

Additional util methods

Retrieving an experiment version

Since an experiment version can be somewhat long to train, it can be useful to separate the training, monitoring and prediction phases.

To do that, we need to be able to recreate an experiment object in python from its name:

experiment_version = pio.Supervised.from_id('<a experiment id>')
# Experiment_version now has all the same methods as an experiment_version
# created directly from a file or a dataframe

Stopping and deleting

Once you’re satisfied with model performance, don’t want to wait for the complete training process to be over, or need to free up some resources to start a new training, you can stop the experiment_version simply:


You’ll still be able to make predictions and get info, but the performance won’t improve anymore. Note: there’s no difference in state between a stopped experiment and an experiment that has completed its training completely.

You can decide to completely delete the experiment:

experiment = pio.Experiment.from_id(experiment_version.experiment_id)

However be careful, in that case any detail about the experiment will be removed, and you won’t be able to make predictions from it anymore.