Skip to content

Conversation

@abrookins
Copy link
Collaborator

Summary

Fixes #544 - HashModel incorrectly rejected list[float] fields even when they were vector fields that require this type annotation.

Changes

  1. Modified HashModel.__init_subclass__ to check if a field has vector_options before raising the "cannot index list" error. Vector fields are now allowed to use list[float] type.

  2. Added vector serialization in save() - Vector fields (list[float]) are automatically serialized to packed float32 bytes before storage, since Redis Hash fields can only store scalar values.

  3. Added vector deserialization in get() - Packed bytes are automatically converted back to list[float] when retrieving vector fields.

Example Usage

from aredis_om import HashModel, Field, VectorFieldOptions

vector_options = VectorFieldOptions.flat(
    type=VectorFieldOptions.TYPE.FLOAT32,
    dimension=384,
    distance_metric=VectorFieldOptions.DISTANCE_METRIC.COSINE,
)

class Document(HashModel, index=True):
    name: str
    embedding: list[float] = Field(default=[], vector_options=vector_options)

# Now works correctly:
doc = Document(name="test", embedding=[0.1, 0.2, 0.3, ...])
await doc.save()
retrieved = await Document.get(doc.pk)
# retrieved.embedding is list[float]

Testing

  • Added test test_hashmodel_vector_field_with_list to verify the fix
  • All existing tests pass (63 hash model tests, 2 KNN tests)

HashModel.__init_subclass__ rejected list fields unconditionally, but
vector fields require list[float] type annotation. Now checks for
vector_options before raising the error.

Also adds automatic serialization of vector list[float] to bytes for
Redis Hash storage, and deserialization back to list[float] on retrieval.

Fixes #544
@abrookins abrookins force-pushed the fix/544-hashmodel-vector-list branch from 4d789b3 to 8ef248e Compare January 23, 2026 19:33
@abrookins abrookins merged commit abfcbfa into main Jan 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot create HashModel containing a list to do knn

2 participants