Skip to main content
Weaviate Docs (migrated from docs.weaviate.io) Docs

Search documentation

Type to search this documentation.

On this pageOverview

Create objects

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

This example creates an object in the JeopardyQuestion collection.

Python
jeopardy = client.collections.use("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
JavaScript/TypeScript
const jeopardy = client.collections.use('JeopardyQuestion')
Go
w, err := client.Data().Creator().
  WithClassName("JeopardyQuestion").
  WithProperties(map[string]interface{}{
    "question": "This vector DB is OSS and supports automatic property type inference on import",
    // "answer": "Weaviate", // schema properties can be omitted
    "newProperty": 123, // will be automatically added as a number property
  }).
  Do(ctx)
Java
var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(Map.of(    "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)).uuid();System.out.println(uuid); // the return value is the object's UUID
C#
var jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert(    new    {        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    });Console.WriteLine(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.

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

Python
jeopardy = client.collections.use("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
JavaScript/TypeScript
const jeopardy = client.collections.use('JeopardyQuestion')
Go
vector := make([]float32, 1536)for i := 0; i < len(vector); i++ {  vector[i] = 0.12345}w, err := client.Data().Creator().  WithClassName("JeopardyQuestion").  WithProperties(map[string]interface{}{    "question": "This vector DB is OSS and supports automatic property type inference on import",    "answer":   "Weaviate",  }).  WithVector(vector).  Do(ctx)
Java
var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(Map.of("question",    "This vector DB is OSS and supports automatic property type inference on import",    "answer", "Weaviate"),    meta -> meta.vectors(Vectors.of(new float[384])) // Using a zero vector for demonstration).uuid();System.out.println(uuid); // the return value is the object's UUID
C#
var jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert(    new    {        question = "This vector DB is OSS and supports automatic property type inference on import",        answer = "Weaviate",    },    vectors: new float[300] // Using a zero vector for demonstration);Console.WriteLine(uuid); // the return value is the object's UUID

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

Python
reviews = client.collections.use("WineReviewNV")  # This collection must have named vectors configureduuid = 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
JavaScript/TypeScript
const reviews = client.collections.use('WineReviewNV')
Java
var reviews = client.collections.use("WineReviewNV"); // This collection must have named vectors configuredvar uuid = reviews.data.insert(    Map.of("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    meta -> meta.vectors(Vectors.of("title", new float[1536]),        Vectors.of("review_body", new float[1536]),        Vectors.of("title_country", new float[1536]))).uuid();System.out.println(uuid); // the return value is the object's UUID
C#
var reviews = client.Collections.Use("WineReviewNV"); // This collection must have named vectors configuredvar uuid = await reviews.Data.Insert(    new    {        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    vectors: new Vectors    {        { "title", new float[1536] },        { "review_body", new float[1536] },        { "title_country", new float[1536] },    });Console.WriteLine(uuid); // the return value is the object's UUID

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

Python
properties = {    "question": "This vector DB is OSS and supports automatic property type inference on import",    "answer": "Weaviate",}jeopardy = client.collections.use("JeopardyQuestion")uuid = jeopardy.data.insert(    properties=properties,    uuid="12345678-e64f-5d94-90db-c8cfa3fc1234")print(uuid)  # the return value is the object's UUID
JavaScript/TypeScript
const jeopardy = client.collections.use('JeopardyQuestion')
Go
vector := make([]float32, 1536)for i := 0; i < len(vector); i++ {  vector[i] = 0.12345}w, err := client.Data().Creator().  WithClassName("JeopardyQuestion").  WithProperties(map[string]interface{}{    "question": "This vector DB is OSS and supports automatic property type inference on import",    "answer":   "Weaviate",  }).  WithID("12345678-e64f-5d94-90db-c8cfa3fc1234").  Do(ctx)
Java
Map<String, Object> properties = new HashMap<>();properties.put("question",    "This vector DB is OSS and supports automatic property type inference on import");properties.put("answer", "Weaviate");var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(properties,    meta -> meta.uuid("12345678-e64f-5d94-90db-c8cfa3fc1234")).uuid();System.out.println(uuid); // the return value is the object's UUID
C#
var jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert(    new    {        question = "This vector DB is OSS and supports automatic property type inference on import",        answer = "Weaviate",    },    uuid: Guid.Parse("12345678-e64f-5d94-90db-c8cfa3fc1234"));Console.WriteLine(uuid); // the return value is the object's UUID

You can generate an ID based on your data object.

Python
from weaviate.util import generate_uuid5  # Generate a deterministic IDdata_object = {    "question": "This vector DB is OSS and supports automatic property type inference on import",    "answer": "Weaviate",}jeopardy = client.collections.use("JeopardyQuestion")uuid = jeopardy.data.insert(    properties=data_object,    uuid=generate_uuid5(data_object),)
JavaScript/TypeScript
import { generateUuid5 } from 'weaviate-client';
Go
// This feature is under development
Java
// In Java, you can generate a deterministic UUID from a string or bytes.// This helper function uses UUID.nameUUIDFromBytes for this purpose.Map<String, Object> dataObject = new HashMap<>();dataObject.put("question",    "This vector DB is OSS and supports automatic property type inference on import");dataObject.put("answer", "Weaviate");var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(dataObject,    meta -> meta.uuid(generateUuid5(dataObject.toString()).toString())).uuid();
C#
// In C#, you can generate a deterministic UUID from a string or bytes.// This helper function creates a UUID v5 for this purpose.var dataObject = new{    question = "This vector DB is OSS and supports automatic property type inference on import",    answer = "Weaviate",};var dataObjectString = JsonSerializer.Serialize(dataObject);var jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert(    dataObject,    uuid: GenerateUuid5(dataObjectString));
Additional information

To generate deterministic IDs, use one of these methods:

  • generate_uuid5 (Python)
  • generateUuid5 (TypeScript)

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

Python
questions = client.collections.use("JeopardyQuestion")questions.data.insert(    properties=properties,  # A dictionary with the properties of the object    uuid=obj_uuid,  # The UUID of the object    references={"hasCategory": category_uuid},  # e.g. {"hasCategory": "583876f3-e293-5b5b-9839-03f455f14575"})
JavaScript/TypeScript
const category = client.collections.use('JeopardyCategory')const dataObject = {'name': 'Science'}const response = await category.data.insert({  properties: dataObject,  references: {    'hasCategory': categoryId  // e.g. {'hasCategory': '583876f3-e293-5b5b-9839-03f455f14575'}  }})console.log('UUID: ', response)
Java
var questions = client.collections.use("JeopardyQuestion");var result = questions.data.insert(properties, // A map with the properties of the object    opt -> opt        .reference("hasCategory", ObjectReference.uuids(categoryUuid)) // e.g. {"hasCategory":// "583876f3-e293-5b5b-9839-03f455f14575"});
C#

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

Python
publications = client.collections.use("Publication")

publications.data.insert(
    properties={
        "headquartersGeoLocation": {
            "latitude": 52.3932696,
            "longitude": 4.8374263
        }
    },
)
JavaScript/TypeScript
const publication = client.collections.use('Publication')

uuid = await publication.data.insert({
  properties: {
    name: 'Elsevier',
    headquartersGeoLocation: {
      'latitude': 52.3932696,
      'longitude': 4.8374263,
    },
  },
  id: 'df48b9f6-ba48-470c-bf6a-57657cb07390'
})

console.log('UUID: ', uuid)
Go
package main

import (
  "context"
  "fmt"

  "github.com/weaviate/weaviate-go-client/v5/weaviate"
)

func main() {
  cfg := weaviate.Config{
    Host:   "localhost:8080",
    Scheme: "http",
  }
  client, err := weaviate.NewClient(cfg)
  if err != nil {
    panic(err)
  }

  dataSchema := map[string]interface{}{
    "name": "Elsevier",
    "headquartersGeoLocation": map[string]float32{
      "latitude":  52.3932696,
      "longitude": 4.8374263,
    },
  }

  created, err := client.Data().Creator().
    WithClassName("Publication").
    WithID("df48b9f6-ba48-470c-bf6a-57657cb07390").
    WithProperties(dataSchema).
    Do(context.Background())

  if err != nil {
    panic(err)
  }
  fmt.Printf("%v", created)
}
Java
var publications = client.collections.use("Publication");

var uuid = publications.data
    .insert(Map.of("headquartersGeoLocation",
        Map.of("latitude", 52.3932696, "longitude", 4.8374263)))
    .uuid();
C#
var publications = client.Collections.Use("Publication");

var uuid = await publications.Data.Insert(
    new { headquartersGeoLocation = new GeoCoordinate(52.3932696f, 4.8374263f) }
);

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

Python
# Validate is currently not supported with the Weaviate Python client v4
JavaScript/TypeScript
// Validate is currently not supported with the Weaviate TypeScript client v3
Go
err := client.Data().Validator().
  WithClassName("JeopardyQuestion").
  WithProperties(map[string]interface{}{
    "question":                          "This vector DB is OSS and supports automatic property type inference on import",
    "answer":                            "Weaviate",
    "thisPropShouldNotEndUpInTheSchema": -1,
  }).
  WithID("12345678-1234-1234-1234-123456789012").
  Do(ctx)
Java
// Coming soon
C#
// Coming soon

Collections can have multiple named vectors.

The vectors in a collection can have their own configurations. Each vector space can set its own index, its own compression algorithm, and its own vectorizer. This means you can use different vectorization models, and apply different distance metrics, to the same object.

To work with named vectors, adjust your queries to specify a target vector for vector search or hybrid search queries.

Have a question or feedback? Here's how to reach us.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu