How to beginner · 3 min read

How to create a Weaviate schema

Quick answer
Use the official weaviate-client Python library to create a schema by defining classes and their properties in a JSON-like dictionary, then call client.schema.create_class(). This sets up your vector database schema for storing and querying data.

PREREQUISITES

  • Python 3.8+
  • pip install weaviate-client
  • Running Weaviate instance (local or cloud)
  • Weaviate URL and API key if applicable

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

Define a schema class with properties and create it using the Weaviate client. This example creates a Article class with title and content properties.

python
import os
import weaviate

# Connect to Weaviate instance
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 schema class
article_class = {
    "class": "Article",
    "description": "A class to store articles",
    "properties": [
        {
            "name": "title",
            "dataType": ["text"],
            "description": "Title of the article"
        },
        {
            "name": "content",
            "dataType": ["text"],
            "description": "Content of the article"
        }
    ]
}

# Create the class in Weaviate schema
client.schema.create_class(article_class)

# Verify schema creation
schema = client.schema.get()
print(schema)
output
{'classes': [{'class': 'Article', 'description': 'A class to store articles', 'properties': [{'name': 'title', 'dataType': ['text'], 'description': 'Title of the article'}, {'name': 'content', 'dataType': ['text'], 'description': 'Content of the article'}]}]}

Common variations

You can create multiple classes, use different data types like int, number, date, or geoCoordinates. For async usage, use asyncio with the weaviate-client async support. You can also update or delete schema classes with client.schema.update_class() and client.schema.delete_class().

python
import asyncio
import weaviate

async def create_schema_async():
    client = weaviate.AsyncClient(
        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
    )

    new_class = {
        "class": "Book",
        "description": "A class for books",
        "properties": [
            {"name": "author", "dataType": ["text"]},
            {"name": "publishedYear", "dataType": ["int"]}
        ]
    }

    await client.schema.create_class(new_class)
    schema = await client.schema.get()
    print(schema)

asyncio.run(create_schema_async())
output
{'classes': [{'class': 'Article', ...}, {'class': 'Book', 'description': 'A class for books', 'properties': [{'name': 'author', 'dataType': ['text']}, {'name': 'publishedYear', 'dataType': ['int']}]}]}

Troubleshooting

  • If you get a connection error, verify your Weaviate URL and that the instance is running.
  • If schema creation fails with a 409 conflict, the class already exists; use update_class() or delete it first.
  • Ensure your API key is correct if authentication is enabled.

Key Takeaways

  • Use the official weaviate-client Python library to manage schemas programmatically.
  • Define classes with properties specifying name, dataType, and description.
  • Handle authentication and connection via environment variables for security.
  • Use async client for asynchronous schema operations if needed.
  • Check for existing classes to avoid conflicts during schema creation.
Verified 2026-04
Verify ↗