How to get started with Weaviate
Quick answer
To get started with
Weaviate, install the weaviate-client Python package and connect to a running Weaviate instance. Use the client to create schemas, add vectorized data, and perform semantic searches with simple Python API calls.PREREQUISITES
Python 3.8+Running Weaviate instance (local Docker or cloud)pip install weaviate-client
Setup
Install the official weaviate-client Python package and ensure you have access to a running Weaviate server, either locally via Docker or a cloud instance.
pip install weaviate-client Step by step
This example connects to a local Weaviate instance, creates a schema, adds an object with a vector, and queries it.
import weaviate
# Connect to local Weaviate instance
client = weaviate.Client("http://localhost:8080")
# Define a simple schema
schema = {
"classes": [
{
"class": "Article",
"description": "A news article",
"properties": [
{
"name": "title",
"dataType": ["text"]
},
{
"name": "content",
"dataType": ["text"]
}
]
}
]
}
# Create schema
client.schema.delete_all() # Clear existing schema
client.schema.create(schema)
# Add an object with vector
obj = {
"title": "Weaviate introduction",
"content": "Weaviate is an open-source vector search engine."
}
# Example vector (usually from an embedding model)
vector = [0.1, 0.2, 0.3, 0.4]
client.data_object.create(obj, "Article", vector=vector)
# Query by vector (search for similar vectors)
near_vector = {"vector": [0.1, 0.2, 0.3, 0.4], "certainty": 0.7}
result = client.query.get("Article", ["title", "content"]).with_near_vector(near_vector).do()
print(result) output
{'data': {'Get': {'Article': [{'title': 'Weaviate introduction', 'content': 'Weaviate is an open-source vector search engine.'}]}}} Common variations
You can connect to cloud-hosted Weaviate instances by changing the URL and adding authentication headers. Async usage is not supported in the official client. Use different embedding models to generate vectors before adding data.
import weaviate
# Connect to cloud Weaviate with API key
client = weaviate.Client(
url="https://your-cloud-instance.weaviate.network",
auth_client_secret=weaviate.AuthApiKey(api_key=os.environ["OPENAI_API_KEY"])
)
# Use client as usual for schema and data operations Troubleshooting
- If you get connection errors, verify your Weaviate server is running and accessible at the specified URL.
- Schema creation errors often mean the schema already exists; use
client.schema.delete_all()to reset. - Ensure your vectors match the dimensionality expected by your Weaviate instance.
Key Takeaways
- Install
weaviate-clientand connect to a running Weaviate server to start. - Define schemas and add vectorized data objects via the Python client API.
- Use
with_near_vectorqueries to perform semantic vector searches. - Cloud instances require API key authentication with
AuthApiKey. - Clear schema conflicts with
client.schema.delete_all()before creating new schemas.