Table of documentation contents

/v1/batch

With batch you can upload a lot of data objects in bulk. This saves time compared to a lot of single request.

Batch data objects

For sending data objects to Weaviate in bulk.

Method and URL

POST /v1/batch/objects

Parameters

The body requires the following field:

nametyperequireddescription
objectslist of data objectsyesa list of data objects, which correspond to the data object body

Example request

  import weaviate

client = weaviate.Client("http://localhost:8080")

first_object_props = {
    "name": "Jane Doe",
    "writesFor": [{
        "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
    }]
}
second_object_props = {
    "name": "John Doe",
    "writesFor": [{
        "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
    }]
}

## METHOD 1: manually call .create_objects() or .flush() to send objects
client.batch.add_data_object(first_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
client.batch.add_data_object(second_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4304")
client.batch.create_objects() # or client.batch.flush(), which also sends references

## Or let a context manager call .flush() automatically 
with client.batch as batch:
    client.batch.add_data_object(first_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
    client.batch.add_data_object(second_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4304")
    # no need to create / call flush, this goes automatically

## METHOD 2: automatically send batch when the batch is full 
client.batch(batch_size=2)
client.batch.add_data_object(first_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
client.batch.add_data_object(second_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4304")
# note that, at the end, you need to create / flush any objects that are left in a batch that is not full and thus not sent yet

## Or with a context manager and __call__ method:
with client.batch(batch_size=2) as batch:
    client.batch.add_data_object(first_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
    client.batch.add_data_object(second_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4304")
    # no need to create / call flush if there are objects left in an unsent batch, this is automatically flushed

## METHOD 3: Automatically send batch when full, but configure this to 'dynamic':
client.batch(batch_size=2, dynamic=True)
client.batch.add_data_object(first_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
client.batch.add_data_object(second_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4304")

## Or:
client.batch.batch_size = 3
client.batch.dynamic = True
client.batch.add_data_object(first_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
client.batch.add_data_object(second_object_props, 'Author', "36ddd591-2dee-4e7e-a3cc-eb86d30a4304")

## For more info, visit https://weaviate-python-client.readthedocs.io/en/v3.0.0/weaviate.batch.html#weaviate.batch.Batch

For detailed information and instructions of batching in Python, click here.

Tips for batching objects with the Python Client

  • There is no limit to how many objects/references one could add to a batch before committing/creating it. However a too large batch can lead to a TimeOut error, which means that Weaviate could not process and create all the objects from the batch in the specified time (the timeout configuration can be set like this or this). Note that setting a timeout configuration higher that 60s would require some changes to the docker-compose.yml/helm chart file.
  • The batch class in the Python Client can be used in three ways:
    • Case 1: Everything should be done by the user, i.e. the user should add the objects/object-references and create them whenever the user wants. To create one of the data type use these methods of this class: create_objects, create_references and flush. This case has the Batch instance’s batch_size set to None (see docs for the configure or __call__ method). Can be used in a context manager, see below.
    • Case 2: Batch auto-creates when full. This can be achieved by setting the Batch instance’s batch_size set to a positive integer (see docs for the configure or __call__ method). The batch_size in this case corresponds to the sum of added objects and references. This case does not require the user to create the batch/s, but it can be done. Also to create non-full batches (last batches) that do not meet the requirement to be auto-created use the flush method. Can be used in a context manager, see below.
    • Case 3: Similar to Case II but uses dynamic batching, i.e. auto-creates either objects or references when one of them reached the recommended_num_objects or recommended_num_references respectively. See docs for the configure or __call__ method for how to enable it.
    • Context-manager support: Can be use with the with statement. When it exists the context-manager it calls the flush method for you. Can be combined with configure or __call__ method, in order to set it to the desired Case.

Batch references

For batching cross-references between data objects in bulk.

Method and URL

POST /v1/batch/references

Parameters

The body of the data object for a new object is a list of objects containing:

nametyperequireddescription
frombeaconyesThe beacon, in the form of weaviate://{host}/{Classname}/{id}/{cref_property_name}
tobeaconyesThe beacon, in the form of weaviate://{host}/{id}

Example request

  import weaviate

client = weaviate.Client("http://localhost:8080")

batch = weaviate.ReferenceBatchRequest()

# Format:
# client.batch.add_reference(<from_entity_uuid>, <from_class_name>, <from_property_name>, <to_entity_uuid>)

## METHOD 1: manually call .flush() to send objects
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "6bb06a43-e7f0-393e-9ecf-3c0f4e129064")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4304", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
client.batch.create_references() # or client.batch.flush(), which also sends objects

## METHOD 2: let a context manager call .flush() automatically 
with client.batch as batch:
    client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "6bb06a43-e7f0-393e-9ecf-3c0f4e129064")
    client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
    client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4304", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
    # no need to create or call flush, this goes automatically

## METHOD 3: automatically send batch when the batch is full 
client.batch(batch_size=3)
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "6bb06a43-e7f0-393e-9ecf-3c0f4e129064")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4304", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
# note that, at the end, you need to create or flush any objects that are left in a batch that is not full and thus not sent yet

## Or with a context manager and __call__ method:
with client.batch(batch_size=2) as batch:
    client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "6bb06a43-e7f0-393e-9ecf-3c0f4e129064")
    client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
    client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4304", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
    # no need to create or call flush if there are objects left in an unsent batch, this is automatically flushed

## METHOD 4: Automatically send batch when full, but configure this to 'dynamic':
client.batch(batch_size=2, dynamic=True)
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "6bb06a43-e7f0-393e-9ecf-3c0f4e129064")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4304", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")

## Or:
client.batch.batch_size = 3
client.batch.dynamic = True
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "6bb06a43-e7f0-393e-9ecf-3c0f4e129064")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")
client.batch.add_reference("36ddd591-2dee-4e7e-a3cc-eb86d30a4304", "Author", "wroteArticles", "b72912b9-e5d7-304e-a654-66dc63c55b32")

## For more info, visit https://weaviate-python-client.readthedocs.io/en/v3.0.0/weaviate.batch.html#weaviate.batch.Batch

For detailed information and instructions of batching in Python, click here.

Error handling

When sending a batch request to your Weaviate instance, it could be the case that an error occurs. This can be caused by several reasons, for example that the connection to Weaviate is lost or that there is a mistake in a single data object that you are trying to add.

Only errors that are caused by sending the whole batch are shown when sending a batch. Errors on individual batch items will not be shown on creating and sending a batch request. Thus, sending a batch and getting no errors does not guarantee that each batch item is added/created. Sending a batch can lead to a successful batch creation but unsuccessful per batch item creation. An example of an error on an individual data object that might be unnoticed by sending a batch request without checking the individual results is: Adding an object to the batch that is in conflict with the schema (for example a non existing class name).

The following Python code can be used to handle errors on individual data objects in the batch.

import weaviate

client = weaviate.Client("http://localhost:8080")

object_to_add = {
    "name": "Jane Doe",
    "writesFor": [{
        "beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
    }]
}

client.batch.add_data_object(object_to_add, "Author", "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
results = client.batch.create_objects() # client.batch.flush() does not return something, but client.batch.create_objects() and client.batch.create_references() does

if results is not None:
    for result in results:
        if 'result' in result and 'errors' in result['result'] and  'error' in result['result']['errors']:
            for message in result['result']['errors']['error']:
                print(message['message'])

This can also be applied to adding references in batch. Note that sending batches, especially references, skips some validation on object and reference level. Adding this validation on single data objects like above makes it less likely for errors to pass without discovering.

More Resources

If you can’t find the answer to your question here, please look at the:

  1. Frequently Asked Questions. Or,
  2. Knowledge base of old issues. Or,
  3. For questions: Stackoverflow. Or,
  4. For issues: Github. Or,
  5. Ask your question in the Slack channel: Slack.
Tags
  • RESTful API
  • references
  • batching