weaviate.util

Helper functions!

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

Bases: Enum

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

Bases: EnumType

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

Check batch results for errors.

Parameters:

results (dict) – The Weaviate batch creation return value.

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

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

Parameters:
  • to_uuid (str or uuid.UUID) – The UUID for which to create a local beacon.

  • class_name (Optional[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:

The local beacon.

Return type:

dict

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

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

Parameters:
  • identifier (Any) – The identifier/object that should be used as basis for the UUID.

  • namespace (Any, optional) – Allows to namespace the identifier, by default “”

Returns:

The UUID as a string.

Return type:

str

weaviate.util.get_domain_from_weaviate_url(url: str) str

Get the domain from a weaviate URL.

Parameters:

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

Returns:

The domain.

Return type:

str

weaviate.util.get_valid_uuid(uuid: str | UUID) str

Validate and extract the UUID.

Parameters:

uuid (str 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:

The extracted UUID.

Return type:

str

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

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 and tf.Tensor.

Returns:

The embedding as a list.

Return type:

list

Raises:

TypeError – If ‘vector’ is not of a supported type.

weaviate.util.image_decoder_b64(encoded_image: str) bytes

Decode image from a Weaviate format image.

Parameters:

encoded_image (str) – The encoded image.

Returns:

Decoded image as a binary string.

Return type:

bytes

weaviate.util.image_encoder_b64(image_or_image_path: str | BufferedReader) str

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

Parameters:

image_or_image_path (str, io.BufferedReader) – The binary read file or the path to the file.

Returns:

Encoded image.

Return type:

str

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

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:

url (str) – The URL to be validated.

Returns:

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

Return type:

bool

weaviate.util.is_weaviate_domain(url: str) bool
weaviate.util.is_weaviate_object_url(url: str) bool

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

Parameters:

url (str) – The URL to be validated.

Returns:

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

Return type:

bool

weaviate.util.is_weaviate_too_old(current_version_str: str) bool

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

Parameters:

current_version_str (str) – 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

Parse a version string into a float.

Parameters:

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

Returns:

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

Return type:

tuple

weaviate.util.strip_newlines(s: str) str