Skip to content

Inserting binary vectors using SQLalchemy #84

@JungeAlexander

Description

@JungeAlexander

Hello,

Thanks for the work on this, @ankane and team! It's amazing to see more vector types and distance metrics being added continuously :) I am trying to insert a binary vector using SQLalchemy and my code looks like this:

from pgvector.sqlalchemy import BIT
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Entity(DeclarativeBase):
    [...]
    embedding: Mapped[np.array] = mapped_column(BIT(2048))

I am then trying to insert embedding as a list[int] of 0s and 1s only. However, this raises the following error:

RepositoryError: An exception occurred: (psycopg.errors.DatatypeMismatch) column "embedding" is of type bit but expression is of type smallint[]
                                                             ^
HINT:  You will need to rewrite or cast the expression.

A similar approach works fine using a list[float] when inserting into an embedding of type pgvector.sqlalchemy.Vector.

My question is: which Python type do I need to cast embedding to before insertion?

Thanks for the help in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions