I'm using pgvector, fastapi and sqlmodel to insert vectors to the database.
from pgvector.sqlalchemy import Vector
## model
class Record(SQLModel, table=True):
id: UUID = Field(default_factory=uuid4, primary_key=True)
text: str = Field(default=None)
vector: List[float] = Field(default=None, sa_column=Vector(1536))
## controllers
def get_record(session: Session, id: UUID):
return session.get(Record, id)
def create_or_update_record(session: Session, record: RecordCreate):
db_record = session.exec(
select(Record).where(Record.id == record.id)
).first()
# rest of the function
But when I need to query the table (for updating a row for example), I get the following error:
File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/fastapi.py", line 84, in _sentry_call
return old_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/app/app/api/controllers/records.py", line 119, in create_or_update_record_endpoint
return create_or_update_record(session, record)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/app/app/api/controllers/records.py", line 53, in create_or_update_record
db_record = session.exec(
^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/sqlmodel/orm/session.py", line 60, in exec
results = super().execute(
^^^^^^^^^^^^^^^^
rest of the error ...
File "/usr/local/lib/python3.11/site-packages/sqlalchemy/sql/type_api.py", line 702, in _cached_result_processor
d[coltype] = rp = d["impl"].result_processor(dialect, coltype)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 525, in result_processor
raise exc.InvalidRequestError(
sqlalchemy.exc.InvalidRequestError: Unknown PG numeric type: 24664
I had the issue with inserting vectors as well, but I fixed it by changing from:
def create_record(session: Session, record: RecordCreate):
new_record = Record(**record.dict())
session.add(new_record)
session.commit()
session.refresh(new_record)
return new_record
to:
def create_record(session: Session, record: RecordCreate):
new_record = Record(**record.dict())
session.add(new_record)
session.commit()
# session.refresh(new_record)
return dict(new_record)
Any idea how can I fix this?
Changing the definition of vector
column in the model from:
vector: List[float] = Field(default=None, sa_column=Vector(1536))
to:
vector: List[float] = Field(default=None, sa_column=Column(Vector(1536)))
fixed the issue.