Skip to content

Fix TypeError: 'float' object is not subscriptable for Sqlalchemy core #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

PaxPrz
Copy link

@PaxPrz PaxPrz commented Jun 9, 2025

Summary

When performing a cosine distance vector search using SQLAlchemy with the following code:

query_vector = sa.literal(embedding, type_=VECTOR)
query = sa.select(
    o.embedding_table.c.embedding_vector,
    (o.embedding_table.c.embedding_vector.op('<->')(query_vector)).label("cosine_distance")
).order_by(sa.asc("cosine_distance")).limit(1)

A TypeError is raised:

TypeError: 'float' object is not subscriptable

Details

The issue occurs because SQLAlchemy attempts to process the result using Vector._from_db, which is not appropriate for a scalar float result (i.e., the cosine distance). Vector._from_db expects a structure of numpy array, but receives a list of float from the database, leading to the error.

This line appears to be the source of the issue:

# Inside result_processor or similar internal hook
Vector._from_db(<float_value>)

@ankane
Copy link
Member

ankane commented Jun 9, 2025

See #132.

@ankane ankane closed this Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants