How to beginner · 3 min read

How to add objects to Weaviate

Quick answer
Use the official weaviate-client Python SDK to add objects by creating a client, defining your schema class, and calling client.data_object.create() with your object data and class name. This method inserts the object into Weaviate for vector search and retrieval.

PREREQUISITES

  • Python 3.8+
  • Weaviate instance running (cloud or local)
  • pip install weaviate-client
  • Access to Weaviate URL and API key if authentication is enabled

Setup

Install the official weaviate-client Python package and set environment variables for your Weaviate URL and API key if needed.

bash
pip install weaviate-client

Step by step

This example shows how to connect to a Weaviate instance, define a class schema, and add an object to that class.

python
import os
import weaviate

# Configure Weaviate client
client = weaviate.Client(
    url=os.environ.get('WEAVIATE_URL', 'http://localhost:8080'),
    auth_client_secret=weaviate.AuthApiKey(api_key=os.environ.get('WEAVIATE_API_KEY')) if os.environ.get('WEAVIATE_API_KEY') else None
)

# Define a schema class (if not already created)
schema_class = {
    "class": "Article",
    "description": "A news article",
    "properties": [
        {"name": "title", "dataType": ["text"]},
        {"name": "content", "dataType": ["text"]}
    ]
}

# Create class schema if it doesn't exist
if "Article" not in [cls["class"] for cls in client.schema.get()['classes']]:
    client.schema.create_class(schema_class)

# Object data to add
article_obj = {
    "title": "Weaviate introduction",
    "content": "Weaviate is an open-source vector search engine."
}

# Add object to Weaviate
response = client.data_object.create(
    data_object=article_obj,
    class_name="Article"
)

print(f"Created object with ID: {response['id']}")
output
Created object with ID: <uuid-string>

Common variations

  • Use client.data_object.create_batch() to add multiple objects efficiently.
  • Authenticate with OAuth2 or other methods by configuring auth_client_secret accordingly.
  • Use client.data_object.update() to modify existing objects.

Troubleshooting

  • If you get connection errors, verify your Weaviate URL and network access.
  • Schema creation errors usually mean the class already exists or schema is invalid.
  • Authentication failures require checking your API key or auth method.

Key Takeaways

  • Use the official weaviate-client Python SDK to interact with Weaviate.
  • Always define your schema class before adding objects to ensure proper data structure.
  • Batch creation improves performance when adding many objects.
  • Check authentication and network settings if connection or permission errors occur.
Verified 2026-04
Verify ↗