Skip to main content

Create objects

The examples on this page demonstrate how to create individual objects in Weaviate.

Create an object

This example creates an object in the JeopardyQuestion collection.

    jeopardy = client.collections.get("JeopardyQuestion")

uuid = jeopardy.data.insert({
"question": "This vector DB is OSS & supports automatic property type inference on import",
# "answer": "Weaviate", # properties can be omitted
"newProperty": 123, # will be automatically added as a number property
})

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

To create an object, specify the following:

By default, auto-schema creates new collections and adds new properties.

Create an object with a specified vector

When you create an object, you can provide a vector. (For specifying multiple, named vectors, see below.)

    jeopardy = client.collections.get("JeopardyQuestion")
uuid = jeopardy.data.insert(
properties={
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
},
vector=[0.12345] * 1536
)

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

Create an object with named vectors

Added in v1.24

When you create an object, you can specify named vectors (if configured in your collection).

    reviews = client.collections.get("WineReviewNV")  # This collection must have named vectors configured
uuid = reviews.data.insert(
properties={
"title": "A delicious Riesling",
"review_body": "This wine is a delicious Riesling which pairs well with seafood.",
"country": "Germany",
},
# Specify the named vectors, following the collection definition
vector={
"title": [0.12345] * 1536,
"review_body": [0.31313] * 1536,
"title_country": [0.05050] * 1536,
}
)

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

Create an object with a specified ID

When you create an object, you can specify an ID.

info

If no ID is provided, Weaviate will generate a random UUID.

    properties = {
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
}
jeopardy = client.collections.get("JeopardyQuestion")
uuid = jeopardy.data.insert(
properties=properties,
uuid="12345678-e64f-5d94-90db-c8cfa3fc1234"
)

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

Generate deterministic IDs

You can generate an ID based on your data object.

info

Object IDs are not randomly generated. The same value always generates the same ID.
Weaviate throws an error if you provide a duplicate ID. Use deterministic IDs to avoid inserting duplicate objects.

    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",
}

jeopardy = client.collections.get("JeopardyQuestion")
uuid = jeopardy.data.insert(
properties=data_object,
uuid=generate_uuid5(data_object),
)
Additional information
To generate deterministic IDs, use one of these methods:

Create an object with cross-references

You can create an object with cross-references to other objects.

    import weaviate.classes as wvc

questions = client.collections.get("JeopardyQuestion")

questions.data.insert(
properties=properties, # A dictionary with the properties of the object
uuid=obj_uuid, # A UUID for the object
references={"hasCategory": category_uuid}, # e.g. {"hasCategory": "583876f3-e293-5b5b-9839-03f455f14575"}
)
Additional information

See How-to: Cross-references for more on working with cross-references.

Validate objects before creation

Before you create an object, you can validate it against the collection definition.

    # Validate is currently not supported with the Weaviate Python client v4

Multiple vectors

Added in v1.24.0

Weaviate collections support multiple, named vectors.

Collections can have multiple, named vectors. Each vector is independent. Each vector space has its own index, its own compression, and its own vectorizer. This means you can create vectors for properties, use different vectorization models, and apply different metrics to the same object.

You do not have to use multiple vectors in your collections, but if you do, you need to adjust your queries to specify which vector you want to use.