Skip to content

Commit d7e67e1

Browse files
authored
Add regression test for JsonModel vector field with list[float] (#786)
Confirms that issue #656 is fixed - JsonModel can now declare vector fields using list[float] type annotation without AttributeError. Related to #656
1 parent 9d9c0ce commit d7e67e1

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

tests/test_json_model.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
QueryNotSupportedError,
2525
RedisModel,
2626
RedisModelError,
27+
VectorFieldOptions,
2728
)
2829
from aredis_om.model.model import ExpressionProxy
2930

@@ -2024,3 +2025,49 @@ class Meta:
20242025
assert retrieved.pk == 42
20252026
assert retrieved.x == 42
20262027
assert retrieved.name == "test"
2028+
2029+
2030+
2031+
@py_test_mark_asyncio
2032+
async def test_jsonmodel_vector_field_with_list(key_prefix, redis):
2033+
"""Test that JsonModel allows list[float] fields with vector_options.
2034+
2035+
Regression test for GitHub issue #656: JsonModel with list[float] vector
2036+
field threw AttributeError: type object 'float' has no attribute '__origin__'.
2037+
"""
2038+
vector_options = VectorFieldOptions.flat(
2039+
type=VectorFieldOptions.TYPE.FLOAT32,
2040+
dimension=4,
2041+
distance_metric=VectorFieldOptions.DISTANCE_METRIC.COSINE,
2042+
)
2043+
2044+
class Article(EmbeddedJsonModel):
2045+
title: str
2046+
2047+
class Group(JsonModel, index=True):
2048+
articles: List[Article]
2049+
tender_text: str = Field(index=False)
2050+
tender_embedding: list[float] = Field(
2051+
index=True,
2052+
vector_options=vector_options
2053+
)
2054+
2055+
class Meta:
2056+
global_key_prefix = key_prefix
2057+
database = redis
2058+
2059+
await Migrator().run()
2060+
2061+
# Create and save a document with a vector
2062+
doc = Group(
2063+
articles=[Article(title="Test Article")],
2064+
tender_text="Sample text",
2065+
tender_embedding=[0.1, 0.2, 0.3, 0.4]
2066+
)
2067+
await doc.save()
2068+
2069+
# Retrieve and verify
2070+
retrieved = await Group.get(doc.pk)
2071+
assert retrieved.tender_text == "Sample text"
2072+
assert len(retrieved.articles) == 1
2073+
assert retrieved.articles[0].title == "Test Article"

0 commit comments

Comments
 (0)