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.

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.

Create an object with geoCoordinates

Limitations

Currently, geo-coordinate filtering is limited to the nearest 800 results from the source location, which will be further reduced by any other filter conditions and search parameters.

If you plan on a densely populated dataset, consider using another strategy such as geo-hashing into a text datatype, and filtering further, such as with a ContainsAny filter.

If you want to supply a geoCoordinates property, you need to specify the latitude and longitude as floating point decimal degrees:

publications = client.collections.get("Publication")

publications.data.insert(
properties={
"headquartersGeoLocation": {
"latitude": 52.3932696,
"longitude": 4.8374263
}
},
)

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 a target vector for vector or hybrid queries.

Questions and feedback

If you have any questions or feedback, let us know in our user forum.