Skip to content

[Bug]: vector types stringify numpy arrays to space-separated literals #31392

Closed

Description

@Ariznawlll

DRAFT 15 (SDK, 2026-10-27) — to be filed

Title: [Bug]: vector bind_processor stringifies numpy arrays with space separators (and any unknown object via bare str())

Description

The SQLAlchemy vector types (VectorType, Vectorf32, Vectorf64 in sqlalchemy_ext/vector_type.py) serialize bind values like this — every one of the three classes has the identical tail:

def process_bind_param(self, value, dialect):
    if value is None:
        return None
    if isinstance(value, list):
        return "[" + ",".join(map(str, value)) + "]"
    if isinstance(value, str):
        return value
    return str(value)          # ← the trap

str(value) is the catch-all for anything that isn't None/list/str. The overwhelmingly common input for vector embeddings is a numpy ndarray — and ndarray's str():

>>> str(np.array([1.5, 2.5, 3.5]))
'[1.5 2.5 3.5]'        # SPACE-separated, not comma
>>> str(np.array([[1.,2.],[3.,4.]]))
'[[1. 2.]\n [3. 4.]]'  # nested with newlines

So INSERT/update via SQLAlchemy ORM with an ndarray embedding ships '[1.5 2.5 3.5]' as the vector literal — MatrixOne's vector parser expects comma-separated terms, yielding (depending on server tolerance) a parse error far from the cause, or worse a silently wrong vector if whitespace ends up tolerated in some path. 2-D arrays produce multi-line garbage. Tensors/torch.Tensor behave the same way.

The same catch-all converts any other unfortunate object to its repr: decimal-comma locales, custom sequence types, pandas Series (str(Series) carries an index in some reprs) — all silently produce non-SQL.

This is not theoretical: the SDK's own docs advertise pandas/numpy-ecosystem embeddings workflows; the natural ORM write path (session.add(Model(embedding=np.array(...))) or session.execute(insert(Model).values(embedding=arr))) lands exactly here.

Reproduction (no server needed)

import numpy as np
from matrixone.sqlalchemy_ext.vector_type import Vectorf32
t = Vectorf32(3)
print(t.process_bind_param(np.array([1.5, 2.5, 3.5]), None))
# '[1.5 2.5 3.5]'    ← space-separated, invalid vector literal
print(t.process_bind_param([1.5, 2.5, 3.5], None))
# '[1.5,2.5,3.5]'    ← correct

(Same result through bind_processor; result_processor is the read side and is tolerant.)

Impact

  • numpy users get hard-to-diagnose insert failures or corrupted vectors on the primary workflow the vector types exist for — the error (SQL syntax/parse from the server) says nothing about numpy or str().
  • The catch-all str(value) masks every future type instead of failing fast; silent wrong-vector writes are the doomsday case for ANN search quality (garbage vectors still insert, searches return nonsense neighbors).

Expected behavior

  1. Handle ndarray explicitly: hasattr(value, "tolist") → value.tolist() then the comma-join path (covers numpy, torch, cupy,jax arrays uniformly). Same guard covers pandas.Series via .values/tolist.
  2. Remove the bare-str() fallback: unknown types should raise TypeError(f"Cannot bind {type(value)} as vector; pass list/tuple/ndarray") — a clear boundary error vastly preferable to shipping a repr to the server.
  3. Guard the list path too: non-numeric elements (e.g. strings) inside the list should raise, not produce '['a','b']'.
  4. Regression: bind ndarray(list/tuple/tensor) asserting the comma-separated literal; bind garbage type asserting the TypeError.

Activity

  1. added
    kind/bug
    on Oct 27, 2026
  2. added this to the MO-202610 milestone on Oct 27, 2026
  3. added theissue type on Oct 27, 2026
  4. added a commit that references this issue on Oct 30, 2026
    bbfe556
  5. XuPeng-SH commented on Oct 30, 2026

    @XuPeng-SH
    Contributor

    QA handoff for merged #31774 (main bbfe55669a1440424914d45a283a1d0e36f722fa): please verify the reported public SDK contract and its failure/recovery controls for vector types stringify numpy arrays to space-separated literals. This isolated QA run inspected the issue and merged PR but did not run tests or a live MatrixOne service at this SHA because the existing resource guard left insufficient headroom. The PR's author-reported validation is separate evidence, not a result of this QA run.

  6. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。NumPy 一维数组经 SDK 向量类型绑定后能正确写入,不支持的对象及非法元素会被拒绝。

    关键证据

    • 官方 main@d39d8a40343ccd40bf665036388df7a40ee867f8;修复 PR fix(python): unify SDK query and analytics lifecycle contracts #31774 合并提交 bbfe55669a1440424914d45a283a1d0e36f722fa 位于祖先链;实际二进制和 SDK 同完整 SHA。本地隔离 1 CN / 1 TN / 1 LogService,Python 3.12、NumPy 2.5.4。
    • 三个独立库中,VectorType、Vectorf32 和 Vectorf64 绑定 NumPy 数组 [1.5,2.5,3.5] 均生成逗号分隔值并实际写入;逐列读取均为三个正确数值。list、tuple 和 NULL 对照也正确。
    • 未知对象、非数值元素和二维数组在三种绑定类型均被拒绝;通过 SDK 的非法实际写入被拒绝,原表仍两行,每轮测试库清理。
    • 回归已加入 MOTR #295,定向 3/3 通过、0 skipped。

    待解决点

  7. Ariznawlll commented on Oct 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    回归位置更正:#295 已关闭并汇总至 MOTR #298;本 issue 的定向用例已补入 #298,之前所述测试结果及未覆盖项不变。#298 尚未合入,完整 MOTR suite 未运行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions