Description
While performing vector search, an asyncpg.exceptions.DataError is raised indicating that the query_embedding parameter is being passed as a string, whereas a list of floats (list[float]) is expected.
Error
...
File "/usr/local/lib/python3.13/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 513, in _handle_exception self._adapt_connection._handle_exception(error) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ File "/usr/local/lib/python3.13/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 797, in _handle_exception raise translated_error from error sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $4: '[-0.007590361870825291,-0.0340789593756... (could not convert string to float: '[-0.007590361870825291,-0.03407895937561989,-0.020293917506933212,-0.01723848097026348,0.023463653400540352,-0.018728477880358696,0.0024153145495802164,0.09091737121343613,0.06552311778068542,-0.013850408606231213,0.008757740259170532,0.049861956387758255,-0.03244360163807869,0.002084719948...
Expected behavior
query_embedding should be passed to PostgreSQL as an list of floats (list[float]) and work correctly in:
self.model.question_embedding.cosine_distance(query_embedding)
Actual behavior
Inside the query, query_embedding is somehow converted into a string:
"[-0.00759, -0.03407, ...]"
which causes a type conversion failure in asyncpg.
Important details
query_embedding is confirmed to be list[float] in Python before the query execution.
Stack:
SQLAlchemy (async)
asyncpg
PostgreSQL (likely using pgvector)
Code example
q_conditions = base_conditions + [
self.model.is_question.is_(True),
self.model.question_embedding.cosine_distance(query_embedding) < question_similarity_threshold
]
query = (
select(
*returning_fields,
)
.where(*q_conditions)
)
cursor = await self.session.execute(query)
Description
While performing vector search, an asyncpg.exceptions.DataError is raised indicating that the query_embedding parameter is being passed as a string, whereas a list of floats (list[float]) is expected.
Error
...
File "/usr/local/lib/python3.13/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 513, in _handle_exception self._adapt_connection._handle_exception(error) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ File "/usr/local/lib/python3.13/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 797, in _handle_exception raise translated_error from error sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.DataError'>: invalid input for query argument $4: '[-0.007590361870825291,-0.0340789593756... (could not convert string to float: '[-0.007590361870825291,-0.03407895937561989,-0.020293917506933212,-0.01723848097026348,0.023463653400540352,-0.018728477880358696,0.0024153145495802164,0.09091737121343613,0.06552311778068542,-0.013850408606231213,0.008757740259170532,0.049861956387758255,-0.03244360163807869,0.002084719948...
Expected behavior
query_embedding should be passed to PostgreSQL as an list of floats (list[float]) and work correctly in:
self.model.question_embedding.cosine_distance(query_embedding)
Actual behavior
Inside the query, query_embedding is somehow converted into a string:
"[-0.00759, -0.03407, ...]"
which causes a type conversion failure in asyncpg.
Important details
query_embedding is confirmed to be list[float] in Python before the query execution.
Stack:
SQLAlchemy (async)
asyncpg
PostgreSQL (likely using pgvector)
Code example
q_conditions = base_conditions + [
self.model.is_question.is_(True),
self.model.question_embedding.cosine_distance(query_embedding) < question_similarity_threshold
]
query = (
select(
*returning_fields,
)
.where(*q_conditions)
)
cursor = await self.session.execute(query)