Skip to main content

Create objects

LICENSE Weaviate on Stackoverflow badge Weaviate issues on GitHub badge Weaviate version badge Weaviate total Docker pulls badge Go Report Card

Overview

This page shows you how to create individual objects in Weaviate.

To create multiple objects, it's best to use batch operations.

Requirements

To create an object, specify:

  • The object data to be added, and
  • The target class.
Multi-tenancy

For classes where multi-tenancy is enabled, you will also need to specify the tenant name when creating objects. See Manage data: multi-tenancy operations for details on how.

Object data vs class definition

Compared to the class definition, the object data:

  • Can omit defined properties.
  • Can include new properties.
info

Any new properties will be added to the class definition during object creation with auto-schema.

Example

This example shows how to create an object in the JeopardyQuestion class, with:

  • An existing property (question),
  • A new property (somePropNotInTheSchema), and
  • Omits an existing property (answer).
uuid = client.data_object.create({
'question': 'This vector DB is OSS & supports automatic property type inference on import',
# 'answer': 'Weaviate', # schema properties can be omitted
'somePropNotInTheSchema': 123, # will be automatically added as a number property
}, 'JeopardyQuestion')

print(uuid) # the return value is the object's UUID

Optional parameters

Optionally, you can specify an id or a vector.

id

You can optionally specify an ID in UUID format. Otherwise, Weaviate will generate a random UUID.

vector

You can optionally specify a vector to represent each object. Otherwise, Weaviate will follow the relevant vectorizer setting.

Example

uuid = client.data_object.create(
data_object={
'question': 'This vector DB is OSS and supports automatic property type inference on import',
'answer': 'Weaviate',
},
class_name='JeopardyQuestion',
uuid='12345678-e64f-5d94-90db-c8cfa3fc1234',
vector=[0.12345] * 1536
)

print(uuid) # the return value is the object's UUID

Preventing duplicates

In Weaviate, the only unique field is the id. If you try to create an object with an existing id, Weaviate will throw an error. (In batch imports, Weaviate will overwrite the object if one exists.)

To prevent duplicates, we suggest:

  • Using a deterministic ID generation method, and
  • Checking if an object exists before creating it.

Generate deterministic ID

You can do so with the generate_uuid5 function in the Python client, or the generateUuid5 function in the TypeScript client.

Example

from weaviate.util import generate_uuid5  # Generate a deterministic ID

data_object = {
'question': 'This vector DB is OSS and supports automatic property type inference on import',
'answer': 'Weaviate',
}
uuid = client.data_object.create(
data_object=data_object,
class_name='JeopardyQuestion',
uuid=generate_uuid5(data_object),
)

Check if an object exists

You can check if an object exists given its id.

IDs unique to each class and tenants

Classes and tenants each work like namespaces. So, it is possible to have the same id in different classes and/or tenants.

Validating before creation

You can validate an object against the class definition before creating it.

This will confirm:

  • Whether the creation of an object will succeed, and
  • That it doesn't have any extra properties vs. the schema.
result = client.data_object.validate(
data_object={
'question': 'This vector DB is open-source and supports auto-schema',
'answer': 'Weaviate',
'thisPropShouldNotEndUpInTheSchema': -1,
},
class_name='JeopardyQuestion',
uuid='12345678-1234-1234-1234-123456789012',
)

# "invalid object: no such prop with name 'thisPropShouldNotEndUpInTheSchema' found..."
print(json.dumps(result, indent=2))

More Resources

For additional information, try these sources.

  1. Frequently Asked Questions
  2. Weaviate Community Forum
  3. Knowledge base of old issues
  4. Stackoverflow
  5. Weaviate slack channel