Python SDK

Python SDK enables you to connect to the MindsDB server from Python using HTTP API. Read along to see how to install, test, and use the MindsDB Python SDK.

How to Install

Simple Installation

To install the MindsDB Python SDK, run the below command:

pip install mindsdb_sdk

Here is the expected output:

Advanced Installation

Instead of using the pip install mindsdb_sdk command, you can install it by cloning the Python SDK repository. Then you should create a virtual environment, install all dependencies from the requirements.txt file, and run tests as instructed below.

To test all the components, go to the project directory (mindsdb_sdk) and run the below command:

env PYTHONPATH=./ pytest

To generate the API documentation, run the below commands:

pip install sphinx
cd docs
make html

The documentation is generated in the docs/build/html folder.

How to Use

Connecting to MindsDB

You can connect to your local MindsDB server, MindsDB Cloud server, or MindsDB Pro server.

  • Local MindsDB

  • MindsDB Cloud

  • MindsDB Pro

Here is how to connect to your local MindsDB server:

import mindsdb_sdk

# connects to the default port (47334) on localhost 
server = mindsdb_sdk.connect()

# connects to the specified host and port
server = mindsdb_sdk.connect('')

Working with Databases

Use the get_database() method to get an existing database:

mysql_demo_db = server.get_database('mysql_demo_db')

Or use the create_database() method to connect data to MindsDB:

mysql_demo_db = server.create_database(
    engine = "mysql",
    name = "mysql_demo_db",
    connection_args = {
      "user": "user",
      "password": "MindsDBUser123!",
      "host": "",
      "port": "3306",
      "database": "public"

Use the list_databases() method to list all databases:


Use the query() method to submit a query to a database:

query = mysql_demo_db.query('SELECT * FROM my_table;')

Use the drop_database() method to remove a database:


Working with Tables

Each table is stored in a database. Hence, we use the mysql_demo_db variable to run some of the commands below.

Use the get_table() method to fetch a table from the mysql_demo_db database:

my_table = mysql_demo_db.get_table('my_table')

Or use the create_table() method to create a new table:

# option 1
my_table = mysql_demo_db.create_table('my_table', 'SELECT * FROM some_table WHERE key=value')

# option 2
my_table = mysql_demo_db.create_table('my_table', base_table)

Use the list_tables() method to list all database tables:


Use the fetch() method to query for table’s data:


The filter() method filters the data based on the provided condition, and the limit() method limits the output rows.

Working with Projects

MindsDB uses projects to keep artifacts, such as models or views, separate according to what predictive task they solve. You can learn more about MindsDB projects here.

Use the get_project() method to get the default mindsdb project:

project = server.get_project()

Use the get_project() method to get other project:

project = server.get_project('project_name')

Use the create_project() method to create a new project:

project = server.create_project('project_name')

Use the list_projects() method to lists all available projects:


Use the query() method to submit a query to a project:

query = project.query('SELECT * FROM my_table;')

Use the drop_project() method to remove a project:


Working with Models

Each model is stored in a project. Hence, we use the project variable to run some of the commands below.

Use the get_model() method to get an existing model:

my_model = project.get_model('my_model')

Or use the create_model() method to create and train a new model:

my_model = project.create_model (
    name = 'my_model',
    predict = 'target',
    query = my_table

Use the list_models() method to list all models contained in the project:


Use the get_status() method to check the training status of the model:


Use the predict() method to make predictions:


Above command returns predictions for the first 10 rows of the my_table table.

Use the describe() method to get information about a model:


Use the refresh() method to refresh model’s data from the MindsDB server:


Use the drop_model() method to remove a model:


Working with Model Versions

Use the finetune() method to finetune a model with specific data:


Use the retrain() method to retrain a model:


Use the list_versions() method to view all available version of a model:


Use the get_version() method to use a specific version of a model:


Use the set_active() method to set a specific version of a model as active:


Use the drop_model_version() method to remove a specific version of a model:

project.drop_model_version('my_model', 3)

Please note that the model version should be deactivated before it is removed.

Working with Views

Each view is stored in a project. Hence, we use the project variable to run the commands below.

Use the get_view() method to get an existing view:

my_view = project.get_view('my_view')

Or use the create_view() method to create a view:

my_view = project.create_view(
    mysql_demo_db.query('SELECT * FROM my_table LIMIT 100')

Use the list_views() method to list all views in a project:


Use the drop_view() method to remove a view:


Working with Jobs

Each job is stored in a project. Hence, we use the project variable to run the commands below.

Use the get_job() method to get an existing job:

my_job = project.get_job('my_job')

Or use the create_job() method to create a job:

my_job = project.create_job(
    'select * from models',
    repeat_str = '1 hour'

Use the list_jobs() method to list all jobs in a project:


Use the refresh() method to retrieve job data from the MindsDB server:


Use the get_history() method to get history of job execution:


Use the drop_job() method to remove a job:



Here is a sample code to create and train a model and make predictions:

# get the default project
project = server.get_project()

# prepare the training data
example_db = server.get_database('example_db')
rentals_table = example_db.get_table('demo_data.home_rentals')
print('First 3 rows of home_rentals:')

# get all models
model_names = [ for i in  project.list_models()]

# create a model if it doesn't exist yet
if 'home_rentals_model' in model_names:
  print('home_rentals_model already exists. Returning existing model')
  home_rentals_model = project.get_model('home_rentals_model')

  print('Creating home_rentals_model model')

  home_rentals_model = project.create_model(
      name = 'home_rentals_model',
      predict = 'rental_price',
      query = rentals_table
  print('Created home_rentals_model successfully:')

  print('Waiting for model training to complete...please be patient')
  for i in range(400):
    print('.', end='')

  if home_rentals_model.get_status() not in ('generating', 'training'):
    print('\nFinished training home_rentals_model:')

# get model's details      

if home_rentals_model.get_status() == 'error':
  print('Something went wrong while training:')

# make predictions
rentals_table_limit = rentals_table.limit(3)

print('Making prediction on the first 3 rows of home_rentals table')
ret = home_rentals_model.predict(rentals_table_limit)

print(ret[['number_of_rooms', 'number_of_bathrooms', 'rental_price']])

You can find the MindsDB Python SDK on the PyPI webpage here.

For more examples, visit the Google Colab Notebook here.