[Python] How to pass vector from Python to Duckdb for vector similarity search

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Duckdb with the VSS extension to store document embeddings, it works normally however when I try to do a similarity search to a vector passed from Python, I am getting a TypeError. The query looks like this:

    query = text('select document from embedding_duckdb where collection_name = :collection_name order by array_cosine_similarity(embedding, :embedding::FLOAT[1024])) limit :num_docs')
    result = self.session.execute(query, {'collection_name': collection, 'embedding': array_type(response["embedding"]), 'num_docs': num_docs})
    pages = [row[0] for row in result.fetchall()]


    here is TypeError I get:

    E TypeError: array_type(): incompatible function arguments. The following argument types are supported:
    E 1. (type: duckdb.duckdb.typing.DuckDBPyType, size: int, *, connection: duckdb.DuckDBPyConnection = None) -> duckdb.duckdb.typing.DuckDBPyType
    E
    E Invoked with: [0.8049294352531433, -0.5165860652923584, -0.25891393423080444, -0.8668404221534729, 0.050446540117263794, -0.060985639691352844, -0.2768419682979584, -0.3037632703781128, 0.7962498664855957, -0.30102455615997314, 0.13201330602169037, -0.35436326265335083, -0.2618299722671509, -0.030353419482707977, -0.009162794798612595, -0.37244415283203125, -0.505980372428894, -0.2313883751630783, -0.4354000389575958, 0.7349468469619751, 0.18996360898017883, 0.47891461849212646, -0.009091421961784363, ...


    I tried other ways of casting the vector, for example using the cast function.

    AFAICT I am doing exactly as the docs instruct. Don't know what else to try.

    Continue reading...

Compartilhe esta Página