Skip to content
  • Python

RelationalAI SDK for Python

This guide presents the main features of the RelationalAI SDK for Python, which can be used to interact with RelationalAI’s Relational Knowledge Graph Management System (RKGMS).

python logo

The rai-sdk-python package is open source and is available in this Github repository:


It includes self-contained examples of the main API functionality. Contributions and pull requests are welcome.

Note: This guide applies to rai-sdk-python, the latest iteration of the RelationalAI SDK for Python. The relationalai-sdk package is deprecated.

The last section describes how the different RAI Console notebook cell types can be implemented with the RelationalAI SDK for Python.

See also Python SDK Through Visual Studio Code if you are using a Microsoft VSCode environment to work with the RelationalAI SDK for Python.


You can check the rai-sdk-python repository for the latest version requirements to interact with the RKGMS using the RelationalAI SDK for Python.


The RelationalAI SDK for Python is a stand-alone client library and can be installed using the pip Python package manager as shown below:

# using the pip package manager
pip install rai-sdk

Additional ways of installing the library can be found in the rai-sdk-python repository.


The RelationalAI SDK for Python can access your RAI Server credentials using a configuration file. See SDK Configuration for more details. If you get “local issuer certificate” errors, you may need to install local certificates for your Python.

The Python API function takes the configuration file and the profile name as optional arguments. For instance, you can write the following configuration in a .py file or directly from the Python command-line interface:

from railib import config
cfg ="~/.rai/config", profile = "default")

To load a different configuration, you can replace "default" with a different profile name.

Creating a Context

Most API operations use a context object, constructed with railib.api.Context. To apply the default profile in your ~/.rai/config file, you can use:

from railib import api, config
cfg =
# to specify a non-default profile instead:
# cfg ='myprofile')
ctx = api.Context(**cfg)

As an alternative to loading a configuration and using **cfg, you can also specify the different fields directly using keyword arguments in api.Context. See help(api.Context) for details.

You can test your configuration and context by running:

import json
rsp = api.list_databases(ctx)
print(json.dumps(rsp, indent=2))

This should return a list with database info, assuming your keys have the corresponding permissions. See Listing Databases below.

Another way of testing your setup is by running one of the examples from the repository. You can do that directly from a terminal:

$ cd examples
$ python3 ./

The remaining code examples in this document assume that you have a valid context in the ctx Python variable and the following imports:

from railib import api, config, show
import json
from urllib.request import HTTPError

Moreover, all of them can be run from the Python command-line interface or a .py file.

Managing Users

A client with the right permissions can create, disable, and list the users under the account.

Creating a User

api.create_user(ctx, userid, roles)

Here, userid is a string, identifying the user, and roles is a list of railib.api.Role, with default None and which assigns the user role.

Current valid roles are created with api.Role(<rolestring>). For a list of roles:

rsp = [r.value for r in api.Role]
print(json.dumps(rsp, indent=2))
["user", "admin"]

Deleting a User

You can delete a user through:

api.delete_user(ctx, id)

In this case, id is a string reflecting a given user’s ID.

Disabling and Enabling a User

You can disable a user through:

api.disable_user(ctx, id)

Again, id is a string representing a given user’s ID. You can reenable the user as follows:

api.enable_user(ctx, id)

Listing Users

rsp = api.list_users(ctx)
print(json.dumps(rsp, indent=2))

Retrieving User Details

rsp = api.get_user(ctx, user)
print(json.dumps(rsp, indent=2))

Here, user is a string ID, for example, "auth0|XXXXXXXXXXXXXXXXXX".

Managing OAuth Clients

OAuth clients can be managed with the following functions, provided you have the corresponding permissions:

api.create_oauth_client(ctx, name, permissions)

name is a string identifying the client. permissions is a list of railib.api.Permission, with default None and which assigns no permissions.

For a list of permissions, see:

rsp = [p.value for p in api.Permission]
print(json.dumps(rsp, indent=2))
  "create:compute", "delete:compute", "list:compute", "read:compute",
  "list:database", "update:database", "delete:database",
  "run:transaction", "read:transaction", "read:credits_usage",
  "create:oauth_client", "read:oauth_client", "list:oauth_client",
  "update:oauth_client", "delete:oauth_client",
  "create:user", "list:user", "read:user", "update:user",
  "list:role", "read:role",
  "list:permission", "create:accesskey", "list:accesskey"

This is how to get a list of OAuth clients:


This is how to get details for a specific OAuth client, identified by the string id:

api.get_oauth_client(ctx, id)

Here’s how to delete the OAuth client identified by the string id:

api.delete_oauth_client(ctx, id)

Each OAuth client has its own set of permissions, which determine the operations it can execute. Depending on the permissions, some operations, such as listing other users or creating or deleting compute engines, may fail. Refer to the RAI Console Managing Users guide for further details.

Managing Engines

To query and update RelationalAI databases, you will need a running engine. The following API calls create and manage them.

Creating an Engine

You can create a new engine as follows:

    rsp = api.create_engine(ctx, engine_name, size)
    print(json.dumps(rsp, indent=2))
except HTTPError as e:

API requests return a JSON value, represented in Python as a dict, which can be converted to a string with json.dumps and then printed, as above. Here is a sample result for api.create_engine:

  "compute": {
    "account_name": "#########",
    "id": "#########",
    "name": "python_sdk_engine",
    "size": "XS",
    "region": "#########",
    "state": "REQUESTED",
    "created_by": "#########",
    "requested_on": "2022-08-10T11:22:30.699Z"

Valid sizes are given by the api.EngineSize enum:

  • XS (extra small).
  • S (small).
  • M (medium).
  • L (large).
  • XL (extra large).

Note: It may take some time before your engine is in the “PROVISIONED” state, where it is ready for queries. It will be in the “PROVISIONING” state before that.

To list all the engines that are currently provisioned, you can use api.list_engines:

rsp = api.list_engines(ctx, "PROVISIONED")
print(json.dumps(rsp, indent=2))

If there is an error with the request, a urllib.request.HTTPError exception will be thrown.

Most of the API examples below assume you have a running engine (in “PROVISIONED” state) in the engine variable, and a test database in database:

# replace with your values for testing:
database = "mydatabase"
engine = "MYENGINE"

Deleting an Engine

Here’s how to delete an engine:

    rsp = api.delete_engine(ctx, engine)
except HTTPError as e:

Note that since RelationalAI decouples computation from storage, deleting an engine does not delete any cloud databases. See Managing Engines for more details.

Getting Info for an Engine

You can retrieve the details for a specific compute engine with api.get_engine:

api.get_engine(ctx, engine)

This will return an empty list if the engine does not exist.

Managing Databases

Creating a Database

You can create a database with api.create_database as follows:

database = "mydatabase"
    rsp = api.create_database(ctx, database)
    print(json.dumps(rsp, indent=2))
except HTTPError as e:

The result from a successful create_database call will look like this:

  "database": {
    "account_name": "#########",
    "name": "mydatabase",
    "id": "#########",
    "state": "CREATED",
    "region": "#########",
    "created_on": "2022-08-10T11:50:46.078Z",
    "created_by": "#########"

A failed call will return a status message such as:

  "status": "Conflict",
  "message": "database already exists"

Cloning a Database

You can use the api.create_database call to clone a database by specifying a source argument:

    rsp = api.create_database(ctx, "MYDBCLONE", source=database)
    print(json.dumps(rsp, indent=2))
except HTTPError as e:

With this API call, you can clone a database from database to "MYDBCLONE", creating an identical copy. Any subsequent changes to either database will not affect the other. Cloning a database fails if the source database does not exist.

Retrieving Database Details

rsp = api.get_database(ctx, databasename)
print(json.dumps(rsp, indent=2))

The response is a JSON object. If the database does not exist, [] will be returned.

Note that this call does not require a running engine.

Listing Databases

Using api.list_databases will list the databases available to the account:

rsp = api.list_databases(ctx)
# rsp = api.list_databases(ctx, state)
print(json.dumps(rsp, indent=2))

The optional variable state (default: None) can be used to filter databases by state; for example, “CREATED”, “CREATING”, or “CREATION_FAILED”.

Deleting Databases

You can delete a database with api.delete_database, if the config has the right permissions.

The database is identified by its name (as used in create_database):

    rsp = api.delete_database(ctx, database_name)
    print(json.dumps(rsp, indent=2))
except HTTPError as e:

If successful, the response will be of the form:

  "name": "#########",
  "message": "deleted successfully"

Deleting a database cannot be undone.

Rel Model Sources

Rel model sources are collections of Rel code that can be added, updated, or deleted from a particular database. Since they update a database, you need a running engine to perform operations on sources.

Installing Rel Model Sources

The api.install_model function installs a Rel model source in a given database. The last argument is a Python dictionary, mapping names to models, so that more than one named model can be installed.

For example, this is how to add a Rel model source code file to a database:

source_string = """ def countries = {"United States of America"; "Germany"; "Japan"; "Greece"}
def oceans = {"Arctic"; "Atlantic"; "Indian"; "Pacific"; "Southern"} """
api.install_model(ctx, database, engine, {"mysource" : source_string})

If the database already contains an installed source with the same given name, then it is replaced by the new source.

If you need to install from a file, you can read it into a string first. For example:

from os import path
msources = {}
with open(fname) as fp:
    msources[path.basename(fname)] =
api.install_model(ctx, database, engine, msources)

The argument fname is a string.

Note that you can also install models in a specific folder by adding the directory in the API function call:

api.install_model(ctx, database, engine, {"mymodels/mysource" : source_string})

Deleting a Rel Model Source

You can delete a source from a database using the following code:

api.delete_model(ctx, database, engine, sourcename)

Note that the argument sourcename is a string.

Listing Installed Rel Model Sources

You can list the sources in a database using the following code:

api.list_models(ctx, database, engine)

This returns a JSON array of names.

To see the contents of a named source, you can use:

api.get_model(ctx, database, engine, sourcename)

Note that the argument sourcename is a string.

Querying a Database

The high-level API call for running a single query/transaction against the database is exec. The function call blocks until the transaction is completed or there are several timeouts indicating that the system may be inaccessible. It specifies a Rel source, which can be empty, and a set of input relations:

    ctx: api.Context,
    database: str,
    engine: str,
    command: str,
    inputs: dict = None,
    readonly: bool = True

For example:

rsp = api.exec(ctx, database, engine,  "def output = {1; 2; 3}")
    "id": "#########",
    "response_format_version": "2.0.3",
    "state": "COMPLETED"
      "relationId": "/:output/Int64",
      "types": [
    "relation_count": 1
    "v1": [

By default, readonly is true.

Queries meant to update base relations with insert and delete must use readonly=false.

For example, here is an API call to load some CSV-like data and store them in the base relation my_base_relation:

data = """
api.exec(ctx, database, engine,
def config:schema:name="string"
def config:schema:lastname="string"
def config:schema:id="int"
def config:syntax:header_row=1
def config:data = mydata
def delete[:my_base_relation] = my_base_relation
def insert[:my_base_relation] = load_csv[config]
inputs = {"mydata":data},

The RelationalAI SDK for Python also supports asynchronous transactions, through exec_async. In summary, when you issue a query to the database, the return output contains a transaction ID that can subsequently be used to retrieve the actual query results.

exec_async is defined as exec, but in this case the running processes are not blocked:

rsp_async= api.exec_async(
    database, engine,
    "def output = {1;2;3}"

Then, you can poll the transaction until it has completed or aborted. Finally, you can fetch the results:

if rsp_async.transaction['state'] == "COMPLETED":

Similarly, you can get the results, metadata, and problems for a given transaction ID using the following functions:

results = api.get_transaction_results(ctx, rsp_async.transaction['id'])
metadata = api.get_transaction_metadata(ctx, rsp_async.transaction['id'])
problems = api.get_transaction_problems(ctx, rsp_async.transaction['id'])

The query size is limited to 64MB. An HTTPError exception will be thrown if the request exceeds this API limit.

Getting Multiple Relations Back

As in the RAI Console, if you want to return multiple relations, you can define subrelations of output. For example:

rsp = api.exec(ctx, database, engine,
    "def a = 1;2 def b = 3;4 def output:one = a def output:two = b")
    "v1": [
    "v1": [

Result Structure

The response is a Python dictionary with the following keys:

metadataMetadata information about the results key.
problemsInformation about any existing problems in the database — which are not necessarily caused by the query.
resultsQuery output information.
transactionInformation about transaction status, including identifier.

The results key is a vector with the following fields:

relationIDThis is a key for the relation, for example, "v1". It refers to the column name in the Arrow table that contains the data, where "v" stands for variable, since a relation’s tuples contain several variables.
tableThis contains the results of the query in a JSON-array format.

Each query is a complete transaction, executed in the context of the provided database.

The metadata key is a JSON string with the following fields:

relationIDThis is a key for the relation, for example, "/:output/:two/Int64". This key describes the keys of the relation together with the type of data.
typesThis is a JSON-array that contains the key names of the relation and their data type.

Finally, the problems key is also a JSON string with the following fields:

error_codeThe type of error that happened, for example, "PARSE_ERROR".
is_errorWhether an error occurred or there was some other problem.
is_exceptionWhether an exception occurred or there was some other problem.
messageA short description of the problem.
pathA file path for the cases when such a path was used.
reportA long description of the problem.
typeThe type of problem, for example, "ClientProblem".

Specifying Inputs

The exec API call takes an optional inputs dictionary that can be used to map relation names to string constants for the duration of the query, analogous to the file upload feature of RAI Console notebooks. For example:

api.exec(ctx, database, engine, "def output = foo", inputs = {"foo" : "asdf"})

This returns the string "asdf" back.

Functions that transform a file and write the results to a base relation can be written in this way. The calls api.load_csv and api.load_json are special cases of this. See, for example, the sample code using load_csv in Querying a Database.

Printing Responses

The module can be used to print API responses. For example:

rsp = api.exec(ctx, database, engine, "def output = {1;2;3}")

Loading Data: load_csv and load_json

As a convenience, the Python API includes load_csv and load_json Python functions. These are not strictly necessary, since the load utilities in Rel itself can be used in a non-read-only exec call that uses the inputs option. See the Specifying Inputs section for further details.

The call api.load_csv loads data and inserts the result into the base relation named by the relation argument:

api.load_csv(ctx: api.Context, database: str, engine: str, relation: str,
    data, syntax: dict = {})
    # `syntax`:
    #   * header: a map from col number to name (base 1)
    #   * header_row: row number of header, 0 means no header (default: 1)
    #   * delim: default: ,
    #   * quotechar: default: "
    #   * escapechar: default: \
    # Schema: a map from col name to rel type name, eg:
    #   {'a': "int", 'b': "string"}

Similarly, load_json loads the data string as JSON and inserts it into the base relation named by the relation argument:

api.load_json(ctx: api.Context, database: str, engine: str, relation: str, data)


api.load_json(ctx, database, engine, "myjson",  """{"a" : "b"}""")

Note: In both cases, the relation base relation is not cleared, allowing for multipart, incremental loads. To clear it, you can issue a non-read-only query of the form:

def delete[:relation] = relation

Listing Base Relations

api.list_edbs(ctx, database, engine)

This will list the base relations in the given database. The result is a JSON list of objects.

Transaction Cancellation

You can cancel an ongoing transaction by calling the following function:

api.cancel_transaction(ctx, id)

The argument id is a string that represents the transaction ID; for instance, rsp[0]["id"] from a previous exec API call.

Notebook Cell Types

As explained in Working With RAI Notebooks there are four notebook cell types: Query, Install, Update, and Markdown. This section shows how the three cell types — Query, Install, and Update — map to the RelationalAI SDK for the Python API calls.

Query Cells

The Query cell in a RAI Console notebook corresponds to an exec (or exec_async) function call, as shown below:

rsp = api.exec(ctx, database, engine, "def output = {1; 2; 3}")

Note that no rules/definitions are persisted in this case.

Install Cells

If derived relation definitions need to be persisted, you can use the install_model API call. That is, the Install cell in a RAI Console notebook corresponds to the api.install_model function, as shown below:

rsp = api.install_model(ctx, database, engine,
    {"arity1_k_def" : "def k = {2;3;5}"}

You can check that the relation above was persisted using the exec API call:

rsp = api.exec(ctx, database, engine, "def output = k")

Update Cells

The Update cell in a RAI Console notebook corresponds to an api.exec call with the flag readonly=False.

For example:

rsp = api.exec(ctx, database, engine,
    """def insert:employee = {(1, \"Han Solo\");(2, \"Bart Simpson\")}""",

You can modify the employee base relation:

rsp = api.exec(ctx, database, engine,
    """def insert:employee = {(3, \"King\");(4, \"Queen\")}""",

You can now query the modified employee base relation:

rsp = api.exec(ctx, database, engine, "def output=employee")
    "v1": [
    "v2": [
      "Han Solo",
      "Bart Simpson",

To update base relations, always use the readonly=False option in the exec call.

Was this doc helpful?