weaviate.util

Helper functions!

class weaviate.util.BaseEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

class weaviate.util.MetaEnum(cls, bases, classdict, *, boundary=None, _simple=False, **kwds)[source]

Bases: EnumType

weaviate.util.check_batch_result(results: List[Dict[str, Any]] | None) None[source]

Check batch results for errors.

Parameters

resultsdict

The Weaviate batch creation return value.

weaviate.util.file_decoder_b64(encoded_file: str) bytes[source]

Decode file from a Weaviate format image.

Parameters

encoded_filestr

The encoded file.

Returns

bytes

Decoded file as a binary string. Use this in your file handling code to convert it into a specific file type of choice. E.g., PIL for images.

weaviate.util.file_encoder_b64(file_or_file_path: str | Path | BufferedReader) str[source]

Encode a file in a Weaviate understandable format from an io.BufferedReader binary read file or by providing the file path as either a string of a pathlib.Path object

If you pass an io.BufferedReader object, it is your responsibility to close it after encoding.

Parameters

file_or_file_pathstr, pathlib.Path io.BufferedReader

The binary read file or the path to the file.

Returns

str

Encoded file.

Raises

ValueError

If the argument is str and does not point to an existing file.

TypeError

If the argument is of a wrong data type.

weaviate.util.generate_local_beacon(to_uuid: str | UUID, class_name: str | None = None) dict[source]

Generates a beacon with the given uuid and class name (only for Weaviate >= 1.14.0).

Parameters

to_uuidstr or uuid.UUID

The UUID for which to create a local beacon.

class_nameOptional[str], optional

The class name of the to_uuid object. Used with Weaviate >= 1.14.0. For Weaviate < 1.14.0 use None value.

Returns

dict

The local beacon.

Raises

TypeError

If ‘to_uuid’ is not of type str.

ValueError

If the ‘to_uuid’ is not valid.

weaviate.util.generate_uuid5(identifier: Any, namespace: Any = '') str[source]

Generate an UUIDv5, may be used to consistently generate the same UUID for a specific identifier and namespace.

Parameters

identifierAny

The identifier/object that should be used as basis for the UUID.

namespaceAny, optional

Allows to namespace the identifier, by default “”

Returns

str

The UUID as a string.

weaviate.util.get_domain_from_weaviate_url(url: str) str[source]

Get the domain from a weaviate URL.

Parameters

urlstr

The weaviate URL. Of this form: ‘weaviate://localhost/objects/28f3f61b-b524-45e0-9bbe-2c1550bf73d2’

Returns

str

The domain.

weaviate.util.get_valid_uuid(uuid: str | UUID) str[source]

Validate and extract the UUID.

Parameters

uuidstr or uuid.UUID

The UUID to be validated and extracted. Should be in the form of an UUID or in form of an URL (weaviate ‘beacon’ or ‘href’). E.g. ‘http://localhost:8080/v1/objects/fc7eb129-f138-457f-b727-1b29db191a67’ or ‘weaviate://localhost/28f3f61b-b524-45e0-9bbe-2c1550bf73d2’ or ‘fc7eb129-f138-457f-b727-1b29db191a67’

Returns

str

The extracted UUID.

Raises

TypeError

If ‘uuid’ is not of type str.

ValueError

If ‘uuid’ is not valid or cannot be extracted.

weaviate.util.get_vector(vector: Sequence) List[float][source]

Get weaviate compatible format of the embedding vector.

Parameters

vector: Sequence

The embedding of an object. Used only for class objects that do not have a vectorization module. Supported types are list, numpy.ndarray, torch.Tensor, tf.Tensor, pd.Series and pl.Series.

Returns

list

The embedding as a list.

Raises

TypeError

If ‘vector’ is not of a supported type.

weaviate.util.image_decoder_b64(encoded_image: str) bytes[source]

Decode image from a Weaviate format image.

Parameters

encoded_imagestr

The encoded image.

Returns

bytes

Decoded image as a binary string.

weaviate.util.image_encoder_b64(image_or_image_path: str | BufferedReader) str[source]

Encode a image in a Weaviate understandable format from a binary read file or by providing the image path.

Parameters

image_or_image_pathstr, io.BufferedReader

The binary read file or the path to the file.

Returns

str

Encoded image.

Raises

ValueError

If the argument is str and does not point to an existing file.

TypeError

If the argument is of a wrong data type.

weaviate.util.is_object_url(url: str) bool[source]

Validates an url like ‘http://localhost:8080/v1/objects/1c9cd584-88fe-5010-83d0-017cb3fcb446’ or ‘/v1/objects/1c9cd584-88fe-5010-83d0-017cb3fcb446’ references a object. It only validates the path format and UUID, not the host or the protocol.

Parameters

urlstr

The URL to be validated.

Returns

bool

True if the ‘url’ is a valid path to an object. False otherwise.

weaviate.util.is_weaviate_client_too_old(current_version_str: str, latest_version_str: str) bool[source]

Check if the user should be gently nudged to upgrade their Weaviate client version.

Parameters

current_version_strstr

The version of the Weaviate client that is being used (e.g. “v1.18.2” or “1.18.0”)

latest_version_strstr

The latest version of the Weaviate client to compare against (e.g. “v1.18.2” or “1.18.0”)

Returns

bool : True if the user should be nudged to upgrade. False if the user is using a valid version or if the version could not be parsed.

weaviate.util.is_weaviate_domain(url: str) bool[source]
weaviate.util.is_weaviate_object_url(url: str) bool[source]

Checks if the input follows a normal Weaviate ‘beacon’ like this: ‘weaviate://localhost/ClassName/28f3f61b-b524-45e0-9bbe-2c1550bf73d2’

Parameters

urlstr

The URL to be validated.

Returns

bool

True if the ‘url’ is a Weaviate object URL. False otherwise.

weaviate.util.is_weaviate_too_old(current_version_str: str) bool[source]

Check if the user should be gently nudged to upgrade their Weaviate server version.

Parameters

current_version_strstr

The version of the Weaviate server that the client is connected to. (e.g. “v1.18.2” or “1.18.0”)

Returns

bool : True if the user should be nudged to upgrade.

weaviate.util.parse_version_string(ver_str: str) tuple[source]

Parse a version string into a float.

Parameters

ver_strstr

The version string to parse. (e.g. “v1.18.2” or “1.18.0”)

Returns

tuple :

The parsed version as a tuple with len(2). (e.g. (1, 18)) Note: Ignores the patch version.

weaviate.util.strip_newlines(s: str) str[source]