TheAlgorithms/Python · error · TypeError

Input data have different datatype... dataset : {dataset.dty

Error message

Input data have different datatype... dataset : {dataset.dtype}, value_array : {value_array.dtype}

What it means

Raised as a TypeError by similarity_search when dataset and value_array have different NumPy dtypes (e.g. float32 vs int32). The implementation requires identical dtypes so distance arithmetic is consistent; this is a deliberate strictness beyond what NumPy broadcasting would allow.

Source

Thrown at machine_learning/similarity_search.py:123

        raise ValueError(msg)

    try:
        if dataset.shape[1] != value_array.shape[1]:
            msg = (
                "Wrong input data's shape... "
                f"dataset : {dataset.shape[1]}, value_array : {value_array.shape[1]}"
            )
            raise ValueError(msg)
    except IndexError:
        if dataset.ndim != value_array.ndim:
            raise TypeError("Wrong shape")

    if dataset.dtype != value_array.dtype:
        msg = (
            "Input data have different datatype... "
            f"dataset : {dataset.dtype}, value_array : {value_array.dtype}"
        )
        raise TypeError(msg)

    answer = []

    for value in value_array:
        dist = euclidean(value, dataset[0])
        vector = dataset[0].tolist()

        for dataset_value in dataset[1:]:
            temp_dist = euclidean(value, dataset_value)

            if dist > temp_dist:
                dist = temp_dist
                vector = dataset_value.tolist()

        answer.append([vector, dist])

    return answer

View on GitHub (pinned to f5988cc097)

Solutions

  1. Cast both to a common dtype before calling: np.asarray(x, dtype=np.float64) on dataset and value_array.
  2. If dtype fidelity matters (float32 for memory), cast the queries to the dataset's dtype rather than the reverse.
  3. Standardize array creation in your pipeline so both sides come from the same loader.

Example fix

# before
result = similarity_search(dataset.astype(np.int32), queries_float)

# after
result = similarity_search(
    np.asarray(dataset, dtype=np.float64),
    np.asarray(queries_float, dtype=np.float64),
)
Defensive patterns

Strategy: validation

Validate before calling

common_dtype = np.float64
dataset = np.asarray(dataset, dtype=common_dtype)
value_array = np.asarray(value_array, dtype=common_dtype)
result = similarity_search(dataset, value_array)

Type guard

def same_dtype(a: np.ndarray, b: np.ndarray) -> bool:
    return a.dtype == b.dtype

Prevention

When it happens

Trigger: Calling similarity_search where dataset.dtype != value_array.dtype, such as an integer dataset from np.arange with float queries from np.random.rand, or float32 model vectors with float64 queries.

Common situations: Mixing arrays from different sources (CSV load gives float64, saved .npy gives float32), or arrays created via literal Python ints on one side and floats on the other.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/14d2fd6516f12575. Report an issue: GitHub.