{"record":{"id":"586cac4c44e27d5f","repo":"pola-rs/polars","slug":"arr-dot-query-vector-must-be-one-dimensional","errorCode":null,"errorMessage":"arr.dot query vector must be one-dimensional","messagePattern":"arr\\.dot query vector must be one-dimensional","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/expr/array.py","lineNumber":356,"sourceCode":"        >>> query = [2.0, 3.0]\n        >>> df.select(pl.col(\"a\").arr.dot(query))\n        shape: (2, 1)\n        ┌──────┐\n        │ a    │\n        │ ---  │\n        │ f64  │\n        ╞══════╡\n        │ 8.0  │\n        │ 18.0 │\n        └──────┘\n        \"\"\"\n        if isinstance(other, Sequence) and not isinstance(other, (str, bytes)):\n            other = list(other)\n            other = F.lit(other).list.to_array(len(other))\n        elif _check_for_numpy(other) and isinstance(other, np.ndarray):\n            if other.ndim != 1:\n                msg = \"arr.dot query vector must be one-dimensional\"\n                raise ValueError(msg)\n            other = F.lit(other).implode().list.to_array(other.size)\n\n        other_pyexpr = parse_into_expression(other)\n        return wrap_expr(self._pyexpr.arr_dot(other_pyexpr))\n\n    def std(self, ddof: int = 1) -> Expr:\n        \"\"\"\n        Compute the std of the values of the sub-arrays.\n\n        .. engine-support:: in-memory, streaming, distributed\n\n        Examples\n        --------\n        >>> df = pl.DataFrame(\n        ...     data={\"a\": [[1, 2], [4, 3]]},\n        ...     schema={\"a\": pl.Array(pl.Int64, 2)},\n        ... )\n        >>> df.select(pl.col(\"a\").arr.std())","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/expr/array.py#L338-L374","documentation":"Expr.arr.dot computes a dot product between each sub-array and a query vector. When the vector is a numpy array, polars requires ndim == 1; a 0-d scalar array or a 2-d matrix raises ValueError before any expression is built. Python lists/tuples bypass the check.","triggerScenarios":"pl.col('vecs').arr.dot(np.array([[1, 2], [3, 4]])) (2-d), arr.dot(np.array(3)) (0-d), or a (1, n) shaped row vector straight from an ML pipeline.","commonSituations":"Passing model weight matrices or batched (batch, n) vectors instead of a single n-vector; forgetting .ravel()/.squeeze() after loading weights from .npy files or checkpoints.","solutions":["Flatten the array first: vec = vec.reshape(-1) (or vec.ravel() / vec.squeeze())","Pass a Python list or tuple instead: arr.dot([1, 2, 3])","Check other.ndim == 1 and that the length matches the fixed array width before calling"],"exampleFix":"# before\npl.col('vecs').arr.dot(np.load('w.npy'))  # w.npy is (1, 3): ValueError\n\n# after\npl.col('vecs').arr.dot(np.load('w.npy').reshape(-1))","handlingStrategy":"validation","validationCode":"import numpy as np\n\nif isinstance(vec, np.ndarray) and vec.ndim != 1:\n    vec = vec.reshape(-1)\nexpr = pl.col('vecs').arr.dot(vec)","typeGuard":"import numpy as np\nfrom typing import TypeGuard\n\ndef is_1d_vector(other) -> TypeGuard[np.ndarray]:\n    return isinstance(other, np.ndarray) and other.ndim == 1","tryCatchPattern":null,"preventionTips":["ravel() vectors loaded from .npy files or model checkpoints before use","Prefer passing a plain list when the vector is small and literal"],"tags":["numpy","array","shape-validation","dot-product"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}