cocoindex-io/cocoindex · error · TypeError

NDArray for Vector must use a concrete numpy dtype, got `Any

Error message

NDArray for Vector must use a concrete numpy dtype, got `Any`.

What it means

DtypeRegistry.validate_dtype_and_get_kind rejects an `Any` dtype before consulting the registry: a Vector backed by NDArray must declare a concrete numpy scalar dtype so CocoIndex can map it to a Float32/Float64/Int64 kind. `typing.Any` as the dtype raises TypeError.

Source

Thrown at python/cocoindex/_internal/datatype.py:84

class DtypeRegistry:
    """
    Registry for NumPy dtypes used in CocoIndex.
    Maps NumPy dtypes to their CocoIndex type kind.
    """

    _DTYPE_TO_KIND: dict[Any, str] = {
        np.float32: "Float32",
        np.float64: "Float64",
        np.int64: "Int64",
    }

    @classmethod
    def validate_dtype_and_get_kind(cls, dtype: Any) -> str:
        """
        Validate that the given dtype is supported, and get its CocoIndex kind by dtype.
        """
        if dtype is Any:
            raise TypeError(
                "NDArray for Vector must use a concrete numpy dtype, got `Any`."
            )
        kind = cls._DTYPE_TO_KIND.get(dtype)
        if kind is None:
            raise ValueError(
                f"Unsupported NumPy dtype in NDArray: {dtype}. "
                f"Supported dtypes: {cls._DTYPE_TO_KIND.keys()}"
            )
        return kind


class AnyType(NamedTuple):
    """
    When the type annotation is missing or matches any type.
    """


class SequenceType(NamedTuple):

View on GitHub (pinned to e84aa99b32)

Solutions

  1. Specify a concrete dtype: npt.NDArray[np.float32]
  2. Use np.float64 or np.int64 if that matches the data and the target index
  3. Normalize bare NDArray aliases to a parametrized form before handing the type to cocoindex

Example fix

// before
embedding: npt.NDArray[Any]

// after
embedding: npt.NDArray[np.float32]
Defensive patterns

Strategy: type-guard

Validate before calling

import typing
from typing import Any as _Any
def dtype_is_concrete(annotation) -> bool:
    args = typing.get_args(annotation)
    return bool(args) and args[-1] is not _Any

Try / catch

try:
    kind = DtypeRegistry.validate_dtype_and_get_kind(dtype)
except TypeError:
    dtype = np.float32  # default to concrete dtype

Prevention

When it happens

Trigger: Annotating a vector/embedding field as np.ndarray[Any, Any] or npt.NDArray[Any] (or Vector with dtype left as Any) so analyze_type_info ends with dtype=Any and validation runs against Any.

Common situations: Leaving the element type unparametrized because the array is created dynamically; using `Any` to silence a type checker; auto-generated annotations missing dtype info.

Related errors


AI-assisted analysis of cocoindex-io/cocoindex@e84aa99b32 (2026-09-08). Data as JSON: /api/errors/e758552d0c199329. Report an issue: GitHub.