Migrate from v3 to v4
The current Python client version is v4.5.7
The v4
Weaviate Python client API is very different from the v3
API. This guide will help you understand the major changes and how to migrate your code at a high level.
Installation
To go from v3
to v4
, you must
Upgrade the client library:
pip install -U weaviate-client # For beta versions: `pip install --pre -U "weaviate-client==4.*"`
Upgrade Weaviate to a compatible version
- Weaviate
1.23.7
is required forv4.4.1
. Generally, we recommend you use the latest versions of Weaviate and the client.
- Weaviate
Make sure a port for gRPC is open to Weaviate.
- The default port is 50051.
docker-compose.yml example
If you are running Weaviate with Docker, you can map the default port (
50051
) by adding the following to yourdocker-compose.yml
file:ports:
- 8080:8080
- 50051:50051
Instantiation
The v4
client is instantiated through the WeaviateClient
object, which is the main entry point for all API operations.
You can directly instantiate the client, but in most cases you can use helper functions starting with _connect_to
, such as connect_to_local
, connect_to_wcs
.
- WCS
- Local
- Embedded
- Custom
import weaviate
import os
client = weaviate.connect_to_wcs(
cluster_url=os.getenv("WCS_DEMO_URL"), # Replace with your WCS URL
auth_credentials=weaviate.auth.AuthApiKey(os.getenv("WCS_DEMO_RO_KEY")), # Replace with your WCS key
headers={'X-OpenAI-Api-key': os.getenv("OPENAI_APIKEY")} # Replace with your OpenAI API key
)
To configure connection timeout values, see Timeout values.
import weaviate
client = weaviate.connect_to_local() # Connect with default parameters
import weaviate
client = weaviate.connect_to_embedded() # Connect with default parameters
import weaviate
client = weaviate.connect_to_custom(
http_host="localhost",
http_port="8080",
http_secure=False,
grpc_host="localhost",
grpc_port="50051",
grpc_secure=False,
headers={
"X-OpenAI-Api-Key": os.getenv("OPENAI_APIKEY") # Or any other inference API keys
}
)
Major changes
The v4
client API is very different from the v3
API. Major user-facing changes in the v4
client include:
- Extensive use of helper classes
- Interaction with collections
- Removal of builder patterns
Helper classes
The v4
client introduces an extensive set of helper classes to interact with Weaviate. These classes are used to provide strong typing, and to make the client more user-friendly such as through IDE autocompletion.
Take a look at the examples below.
- Create a collection
- NearText query
import weaviate
import weaviate.classes.config as wvcc
client = weaviate.connect_to_local()
try:
# Note that you can use `client.collections.create_from_dict()` to create a collection from a v3-client-style JSON object
collection = client.collections.create(
name="TestArticle",
vectorizer_config=wvcc.Configure.Vectorizer.text2vec_cohere(),
generative_config=wvcc.Configure.Generative.cohere(),
properties=[
wvcc.Property(
name="title",
data_type=wvcc.DataType.TEXT
)
]
)
finally:
client.close()
import weaviate
import weaviate.classes as wvc
from weaviate.collections.classes.grpc import Move
import os
client = weaviate.connect_to_local()
try:
publications = client.collections.get("Publication")
response = publications.query.near_text(
query="fashion",
distance=0.6,
move_to=Move(force=0.85, concepts="haute couture"),
move_away=Move(force=0.45, concepts="finance"),
return_metadata=wvc.query.MetadataQuery(distance=True),
limit=2
)
for o in response.objects:
print(o.properties)
print(o.metadata)
finally:
client.close()
In both of these examples, you can see how the helper classes and methods abstract away the need for manual JSONs or strings.
Interaction with collections
Interacting with the client
object for CRUD and search operations have been replaced with the use of collection objects.
This conveniently removes the need to specify the collection for each operation, and reduces potential for errors.
- Python (v4)
- Python (v3)
jeopardy = client.collections.get("JeopardyQuestion")
data_object = jeopardy.query.fetch_object_by_id("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
print(data_object.properties)
data_object = client.data_object.get_by_id(
"00ff6900-e64f-5d94-90db-c8cfa3fc851b",
class_name="JeopardyQuestion",
)
print(json.dumps(data_object, indent=2))
Note here that the collection object can be re-used throughout the codebase.
Collection creation from JSON
You can still create a collection from a JSON definition. This may be a useful way to migrate your existing data, for example. You could fetch an existing definition and then use it to create a new collection.
import weaviate
client = weaviate.connect_to_local()
try:
collection_definition = {
"class": "TestArticle",
"properties": [
{
"name": "title",
"dataType": ["text"],
},
{
"name": "body",
"dataType": ["text"],
},
],
}
client.collections.create_from_dict(collection_definition)
finally:
client.close()
Removal of builder patterns
The builder patterns for constructing queries have been removed, as they could be confusing and potentially lead to invalid queries.
In v4
, queries are constructed using specific methods and its parameters.
- Python (v4)
- Python (v3)
from weaviate.classes.query import MetadataQuery
jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.near_text(
query="animals in movies",
limit=2,
return_metadata=MetadataQuery(distance=True)
)
for o in response.objects:
print(o.properties)
print(o.metadata.distance)
response = (
client.query
.get("JeopardyQuestion", ["question", "answer"])
.with_near_text({
"concepts": ["animals in movies"]
})
.with_limit(2)
.with_additional(["distance"])
.do()
)
print(json.dumps(response, indent=2))
This makes it easier to understand and use. Additionally, some parameters typed (e.g. MetadataQuery
) which makes it easier to use and reduces errors.
How to migrate your code
The migration will likely involve significant changes to your codebase. Review the Python client library documentation to get started, including instantiation details and various submodules.
Then, take a look at the how-to guides for Managing data and Queries.
In particular, check out the pages for:
- Client instantiation,
- Manage collections,
- Batch import
- Cross-reference
- Basic search
- Similarity search
- Filters
Questions and feedback
If you have any questions or feedback, let us know in our user forum.