weaviate.schema

Module used to manipulate schemas.

class weaviate.schema.Schema(connection: Connection)

Bases: object

Schema class used to interact and manipulate schemas or classes.

property

A Property object to create new schema property/ies.

Type:

weaviate.schema.properties.Property

Initialize a Schema class instance.

Parameters:

connection (weaviate.connect.Connection) – Connection object to an active and running Weaviate instance.

add_class_tenants(class_name: str, tenants: List[Tenant]) None

Add class’s tenants in Weaviate.

Parameters:
  • class_name (str) – The class for which we add tenants.

  • tenants (List[Tenant]) – List of Tenants.

Examples

>>> tenants = [ Tenant(name="Tenant1"), Tenant(name="Tenant2") ]
>>> client.schema.add_class_tenants("class_name", tenants)
Raises:
  • TypeError – If ‘tenants’ has not the correct type.

  • requests.ConnectionError – If the network connection to Weaviate fails.

  • weaviate.UnexpectedStatusCodeException – If Weaviate reports a non-OK status.

contains(schema: dict | str | None = None) bool

Check if Weaviate already contains a schema.

Parameters:

schema (dict or str, optional) – Schema as a Python dict, or the path to a JSON file or the URL of a JSON file. If a schema is given it is checked if this specific schema is already loaded. It will test only this schema. If the given schema is a subset of the loaded schema it will still return true, by default None.

Examples

>>> schema = client.schema.get()
>>> client.schema.contains(schema)
True
>>> schema = client.schema.get()
>>> schema['classes'].append(
    {
        "class": "Animal",
        "description": "An Animal",
        "properties": [
            {
                "name": "type",
                "dataType": ["text"],
                "description": "The animal type",
            }
        ]
    }
)
>>> client.schema.contains(schema)
False
Returns:

True if a schema is present, False otherwise.

Return type:

bool

create(schema: dict | str) None

Create the schema of the Weaviate instance, with all classes at once.

Parameters:

schema (dict or str) – Schema as a Python dict, or the path to a JSON file, or the URL of a JSON file.

Examples

>>> article_class = {
...     "class": "Article",
...     "description": "An article written by an Author",
...     "properties": [
...         {
...             "name": "title",
...             "dataType": ["text"],
...             "description": "The title the article",
...         },
...         {
...             "name": "hasAuthors",
...             "dataType": ["Author"],
...             "description": "Authors this article has",
...         }
...     ]
... }
>>> author_class = {
...     "class": "Author",
...     "description": "An Author class to store the author information",
...     "properties": [
...         {
...             "name": "name",
...             "dataType": ["text"],
...             "description": "The name of the author",
...         },
...         {
...             "name": "wroteArticles",
...             "dataType": ["Article"],
...             "description": "The articles of the author",
...         }
...     ]
... }
>>> client.schema.create({"classes": [article_class, author_class]})

If you have your schema saved in the ‘./schema/my_schema.json’ you can create it directly from the file.

>>> client.schema.create('./schema/my_schema.json')
Raises:
  • TypeError – If the ‘schema’ is neither a string nor a dict.

  • ValueError – If ‘schema’ can not be converted into a Weaviate schema.

  • requests.ConnectionError – If the network connection to Weaviate fails.

  • weaviate.UnexpectedStatusCodeException – If Weaviate reports a non-OK status.

  • weaviate.SchemaValidationException – If the ‘schema’ could not be validated against the standard format.

create_class(schema_class: dict | str) None

Create a single class as part of the schema in Weaviate.

Parameters:

schema_class (dict or str) – Class as a Python dict, or the path to a JSON file, or the URL of a JSON file.

Examples

>>> author_class_schema = {
...     "class": "Author",
...     "description": "An Author class to store the author information",
...     "properties": [
...         {
...             "name": "name",
...             "dataType": ["text"],
...             "description": "The name of the author",
...         },
...         {
...             "name": "wroteArticles",
...             "dataType": ["Article"],
...             "description": "The articles of the author",
...         }
...     ]
... }
>>> client.schema.create_class(author_class_schema)

If you have your class schema saved in the ‘./schema/my_schema.json’ you can create it directly from the file.

>>> client.schema.create_class('./schema/my_schema.json')
Raises:
  • TypeError – If the ‘schema_class’ is neither a string nor a dict.

  • ValueError – If ‘schema_class’ can not be converted into a Weaviate schema.

  • requests.ConnectionError – If the network connection to Weaviate fails.

  • weaviate.UnexpectedStatusCodeException – If Weaviate reports a non-OK status.

  • weaviate.SchemaValidationException – If the ‘schema_class’ could not be validated against the standard format.

delete_all() None

Remove the entire schema from the Weaviate instance and all data associated with it.

Examples

>>> client.schema.delete_all()
delete_class(class_name: str) None

Delete a schema class from Weaviate. This deletes all associated data.

Parameters:

class_name (str) – The class that should be deleted from Weaviate.

Examples

>>> client.schema.delete_class('Author')
Raises:
  • TypeError – If ‘class_name’ argument not of type str.

  • requests.ConnectionError – If the network connection to Weaviate fails.

  • weaviate.UnexpectedStatusCodeException – If Weaviate reports a non-OK status.

exists(class_name: str) bool

Check if class exists in Weaviate.

Parameters:

class_name (str) – The class whose existence is being checked.

Examples

>>> client.schema.exists(class_name="Exists")
True
>>> client.schema.exists(class_name="DoesNotExists")
False
Returns:

True if the class exists, False otherwise.

Return type:

bool

get(class_name: str | None = None) dict

Get the schema from Weaviate.

Parameters:

class_name (str, optional) – The class for which to return the schema. If NOT provided the whole schema is returned, otherwise only the schema of this class is returned. By default None.

Returns:

A dict containing the schema. The schema may be empty. To see if a schema has already been loaded, use the contains method.

Return type:

dict

Examples

No schema present in client

>>> client.schema.get()
{'classes': []}

Schema present in client

>>> client.schema.get()
{
    "classes": [
        {
        "class": "Animal",
        "description": "An Animal",
        "invertedIndexConfig": {
            "cleanupIntervalSeconds": 60
        },
        "properties": [
            {
            "dataType": ["text"],
            "description": "The animal type",
            "name": "type"
            }
        ],
        "vectorIndexConfig": {
            "cleanupIntervalSeconds": 300,
            "maxConnections": 64,
            "efConstruction": 128,
            "vectorCacheMaxObjects": 500000
        },
        "vectorIndexType": "hnsw",
        "vectorizer": "text2vec-contextionary",
        "replicationConfig": {
            "factor": 1
        }
        }
    ]
}
>>> client.schema.get('Animal')
{
    "class": "Animal",
    "description": "An Animal",
    "invertedIndexConfig": {
        "cleanupIntervalSeconds": 60
    },
    "properties": [
        {
        "dataType": ["text"],
        "description": "The animal type",
        "name": "type"
        }
    ],
    "vectorIndexConfig": {
        "cleanupIntervalSeconds": 300,
        "maxConnections": 64,
        "efConstruction": 128,
        "vectorCacheMaxObjects": 500000
    },
    "vectorIndexType": "hnsw",
    "vectorizer": "text2vec-contextionary",
    "replicationConfig": {
        "factor": 1
    }
}
Raises:
get_class_shards(class_name: str) list

Get the status of all shards in an index.

Parameters:

class_name (str) – The class for which to return the status of all shards in an index.

Returns:

The list of shards configuration.

Return type:

list

Examples

Schema contains a single class: Article

>>> client.schema.get_class_shards('Article')
[{'name': '2rPgsA2yngW3', 'status': 'READY'}]
Raises:
get_class_tenants(class_name: str) List[Tenant]

Get class’s tenants in Weaviate.

Parameters:

class_name (str) – The class for which we get tenants.

Examples

>>> client.schema.get_class_tenants("class_name")
Raises:
remove_class_tenants(class_name: str, tenants: List[str]) None

Remove class’s tenants in Weaviate.

Parameters:
  • class_name (str) – The class for which we remove tenants.

  • tenants (List[str]) – List of tenant names to remove from the given class.

Examples

>>> client.schema.remove_class_tenants("class_name", ["Tenant1", "Tenant2"])
Raises:
  • TypeError – If ‘tenants’ has not the correct type.

  • requests.ConnectionError – If the network connection to Weaviate fails.

  • weaviate.UnexpectedStatusCodeException – If Weaviate reports a non-OK status.

update_class_shard(class_name: str, status: str, shard_name: str | None = None) list

Get the status of all shards in an index.

Parameters:
  • class_name (str) – The class for which to update the status of all shards in an index.

  • status (str) – The new status of the shard. The available options are: ‘READY’ and ‘READONLY’.

  • shard_name (str or None, optional) – The shard name for which to update the status of the class of the shard. If None then all the shards are going to be updated to the ‘status’. By default None.

Returns:

The updated statuses.

Return type:

list

Examples

Schema contains a single class: Article

>>> client.schema.get_class_shards('Article')
[{'name': 'node1', 'status': 'READY'}, {'name': 'node2', 'status': 'READY'}]

For a specific shard:

>>> client.schema.update_class_shard('Article', 'READONLY', 'node2')
{'status': 'READONLY'}
>>> client.schema.get_class_shards('Article')
[{'name': 'node1', 'status': 'READY'}, {'name': 'node2', 'status': 'READONLY'}]

For all shards of the class:

>>> client.schema.update_class_shard('Article', 'READONLY')
[{'status': 'READONLY'},{'status': 'READONLY'}]
>>> client.schema.get_class_shards('Article')
[{'name': 'node1', 'status': 'READONLY'}, {'name': 'node2', 'status': 'READONLY'}]
Raises:
update_class_tenants(class_name: str, tenants: List[Tenant]) None

Update class tenants.

Use this when you want to move tenants from one activity state to another.

Parameters:
  • class_name (str) – The class for which we update tenants.

  • tenants (List[Tenant]) – List of Tenants.

Examples

>>> client.schema.add_class_tenants(
        "class_name",
        [
            Tenant(activity_status=TenantActivityStatus.HOT, name="Tenant1")),
            Tenant(activity_status=TenantActivityStatus.COLD, name="Tenant2"))
            Tenant(name="Tenant3")
        ]
    )
>>> client.schema.update_class_tenants(
        "class_name",
        [
            Tenant(activity_status=TenantActivityStatus.COLD, name="Tenant1")),
            Tenant(activity_status=TenantActivityStatus.HOT, name="Tenant2"))
        ]
    )
>>> client.schema.get_class_tenants("class_name")
[
    Tenant(activity_status=TenantActivityStatus.COLD, name="Tenant1")),
    Tenant(activity_status=TenantActivityStatus.HOT, name="Tenant2")),
    Tenant(activity_status=TenantActivityStatus.HOT, name="Tenant3"))
]
Raises:
update_config(class_name: str, config: dict) None

Update a schema configuration for a specific class.

Parameters:
  • class_name (str) – The class for which to update the schema configuration.

  • config (dict) – The configurations to update (MUST follow schema format).

Example

In the example below we have a Weaviate instance with a class ‘Test’.

>>> client.schema.get('Test')
{
    'class': 'Test',
    ...
    'vectorIndexConfig': {
        'ef': -1,
        ...
    },
    ...
}
>>> client.schema.update_config(
...     class_name='Test',
...     config={
...         'vectorIndexConfig': {
...             'ef': 100,
...         }
...     }
... )
>>> client.schema.get('Test')
{
    'class': 'Test',
    ...
    'vectorIndexConfig': {
        'ef': 100,
        ...
    },
    ...
}

NOTE: When updating schema configuration, the ‘config’ MUST be sub-set of the schema, starting at the top level. In the example above we update ‘ef’ value, and for this we included the ‘vectorIndexConfig’ top level too.

Raises:

weaviate.schema.properties

weaviate.schema.validate_schema