Weaviate Free code examples in C#, Python, Java and JavaScript
Open-source vector database with built-in vectorisation modules.
WHAT THIS PAGE COVERS
Letting the database call the embedding model so you never handle vectors.
text2vec-weaviate
weaviate-client
Model ids change. If a sample returns a 404 on the model, that is the first thing to check against the official docs.
The official client reached 1.0 in 2026. Note it does NOT mirror the Python and JavaScript names — connection is Connect.Cloud, and a collection handle is Collections.Use.
dotnet add package Weaviate.Client --version 1.0.0
setx WEAVIATE_URL "https://your-cluster.weaviate.cloud"
setx WEAVIATE_API_KEY "your-key"
Connect.Cloud, not a constructor and not ConnectToWeaviateCloud — that is the Python and JS spelling. The client is built once and shared; the collection handles off it are thread-safe.
using Weaviate.Client;
var weaviateUrl = Environment.GetEnvironmentVariable("WEAVIATE_URL")
?? throw new InvalidOperationException("WEAVIATE_URL is not set.");
var weaviateApiKey = Environment.GetEnvironmentVariable("WEAVIATE_API_KEY")
?? throw new InvalidOperationException("WEAVIATE_API_KEY is not set.");
var client = await Connect.Cloud(weaviateUrl, weaviateApiKey);
Configure.Vector names the vector and attaches the module. text2vec-weaviate is declared once here, and from then on every write and every query is plain text.
var documents = await client.Collections.Create(new CollectionCreateParams
{
Name = "Document",
// text2vec-weaviate: the database calls the embedding model, so
// you never hold a vector.
VectorConfig = Configure.Vector("default", v => v.Text2VecWeaviate()),
});
No vectors anywhere in this block. Collections.Use returns a handle to an existing collection; NearText takes the string straight through because the collection knows its own model.
var documents = client.Collections.Use("Document");
await documents.Data.InsertMany(
[
new { text = "Error code E-4471 means the pump stalled.",
source = "handbook.pdf", page = 4 },
new { text = "Quarterly maintenance is due every thirteen weeks.",
source = "handbook.pdf", page = 9 },
]);
var response = await documents.Query.NearText(
"the pump has stopped working",
limit: 3,
returnMetadata: MetadataOptions.Distance);
foreach (var item in response.Objects)
{
Console.WriteLine(item.Properties["text"]);
}
Weaviate Cloud reads a URL and a key. A local instance via docker needs neither, and connect_to_local() is the equivalent call.
pip install -U weaviate-client
export WEAVIATE_URL="https://your-cluster.weaviate.cloud"
export WEAVIATE_API_KEY="your-key"
The client holds a connection and should be closed. Use it as a context manager in a script, or close it in a shutdown hook in a service.
import os
import weaviate
from weaviate.classes.init import Auth
client = weaviate.connect_to_weaviate_cloud(
cluster_url=os.environ["WEAVIATE_URL"],
auth_credentials=Auth.api_key(os.environ["WEAVIATE_API_KEY"]),
)
print(client.is_ready())
# A local container instead:
# client = weaviate.connect_to_local()
text2vec-weaviate is the vectoriser. Attaching it here is what makes every write and every query take TEXT — you never compute, store or compare a vector yourself.
from weaviate.classes.config import Configure
documents = client.collections.create(
name="Document",
# The text2vec-weaviate module. NOT vectorizer_config= —
# that keyword belongs to the old client.
vector_config=Configure.Vectors.text2vec_weaviate(),
)
Objects go in as plain dictionaries. Weaviate calls the embedding model itself, in batches, which is the whole argument for this database over a bring-your-own-vector one.
documents = client.collections.get("Document")
documents.data.ingest([
{
"text": "Error code E-4471 means the pump stalled.",
"source": "handbook.pdf",
"page": 4,
},
{
"text": "Quarterly maintenance is due every thirteen weeks.",
"source": "handbook.pdf",
"page": 9,
},
])
near_text takes a string because the collection knows its own model. A filter runs alongside the vector search rather than over its output, so narrowing does not empty the page.
from weaviate.classes.query import Filter, MetadataQuery
response = documents.query.near_text(
query="the pump has stopped working",
limit=3,
filters=Filter.by_property("source").equal("handbook.pdf"),
return_metadata=MetadataQuery(distance=True),
)
for item in response.objects:
print(round(item.metadata.distance, 4), item.properties["text"])
client.close()
THE ARTIFACT IS client6, not client. The older io.weaviate:client artifact is the v3 REST client with a different API — picking it by habit gets you a package that no longer matches the docs.
# Gradle
implementation("io.weaviate:client6:6.3.1")
export WEAVIATE_URL="https://your-cluster.weaviate.cloud"
export WEAVIATE_API_KEY="your-key"
The client is AutoCloseable. In a Spring application that means a bean with a destroy method, not a new client per request.
import io.weaviate.client6.WeaviateClient;
public class Search {
public static void main(String[] args) throws Exception {
// A local container. WeaviateClient.connectToWeaviateCloud()
// is the hosted equivalent — check the reference manual for
// its exact signature rather than guessing it.
try (WeaviateClient client = WeaviateClient.connectToLocal()) {
System.out.println(client.collections.listAll());
}
}
}
text2vec-weaviate goes on the collection. After this, writes and queries are both plain strings — no vector ever crosses the wire from your side.
import io.weaviate.client6.v1.api.collections.Vectors;
// The text2vec-weaviate module, named once and never again.
client.collections.create("Document", collection -> collection
.vectors(Vectors.text2vecWeaviate()));
var documents = client.collections.use("Document");
nearText takes the string straight through. The collection knows which model to call, so there is one model per collection and no way to mix two.
import java.util.List;
import java.util.Map;
documents.data.insertMany(List.of(
Map.of("text", "Error code E-4471 means the pump stalled.",
"source", "handbook.pdf", "page", 4),
Map.of("text", "Quarterly maintenance is due every thirteen weeks.",
"source", "handbook.pdf", "page", 9)));
var response = documents.query.nearText(
"the pump has stopped working",
query -> query.limit(3));
response.objects().forEach(object ->
System.out.println(object.properties().get("text")));
Same package name as Python, different ecosystem. Every call is async, including the connection.
npm install weaviate-client
export WEAVIATE_URL="https://your-cluster.weaviate.cloud"
export WEAVIATE_API_KEY="your-key"
connectToWeaviateCloud returns a promise. Close it on shutdown — the client holds a gRPC channel as well as HTTP.
import weaviate from "weaviate-client";
const client = await weaviate.connectToWeaviateCloud(
process.env.WEAVIATE_URL,
{ authCredentials: new weaviate.ApiKey(process.env.WEAVIATE_API_KEY) },
);
console.log(await client.isReady());
// A local container instead:
// const client = await weaviate.connectToLocal();
The vectoriser is declared once, on the collection. From then on text goes in and text comes out — there is no place for a vector of the wrong width to enter.
import { configure } from "weaviate-client";
await client.collections.create({
name: "Document",
// text2vec-weaviate: the database calls the embedding model itself.
vectorizers: configure.vectors.text2VecWeaviate(),
});
insertMany batches the objects and the embedding calls behind them. Check the returned errors — a partial failure is reported, not thrown.
const documents = client.collections.get("Document");
const result = await documents.data.insertMany([
{
text: "Error code E-4471 means the pump stalled.",
source: "handbook.pdf",
page: 4,
},
{
text: "Quarterly maintenance is due every thirteen weeks.",
source: "handbook.pdf",
page: 9,
},
]);
// Partial failures come back here rather than as an exception.
if (result.hasErrors) console.error(result.errors);
nearText takes a string. The filter is evaluated with the search, so asking for three results from a narrow slice still returns three if they exist.
import { Filters } from "weaviate-client";
const response = await documents.query.nearText(
"the pump has stopped working",
{
limit: 3,
filters: documents.filter.byProperty("source").equal("handbook.pdf"),
returnMetadata: ["distance"],
},
);
for (const item of response.objects) {
console.log(item.metadata?.distance?.toFixed(4), item.properties.text);
}
await client.close();
QUESTIONS
Which languages are the Weaviate examples in?
C#, Python, Java and JavaScript. Every sample runs as pasted and reads its credentials from an environment variable — no key is ever written into the code.
Which package do I install for Weaviate?
The samples use weaviate-client. The install step on each language tab gives the exact command.
Which Weaviate model do these examples use?
text2vec-weaviate, checked against the official documentation on 26 September 2026. Model ids change often; if a call returns a model-not-found error, check the current list in the official docs.
Where is the official Weaviate documentation?
https://weaviate.io/developers/weaviate — it is linked at the bottom of this page, and it is the authority whenever it disagrees with a sample here.
ALSO IN EMBEDDINGS & SEARCH
The authority
Official Weaviate documentation
Model names, pricing and rate limits change faster than any page here can. When this page and the official docs disagree, the official docs are right.
https://weaviate.io/developers/weaviate