|
24 | 24 | QueryNotSupportedError, |
25 | 25 | RedisModel, |
26 | 26 | RedisModelError, |
| 27 | + VectorFieldOptions, |
27 | 28 | ) |
28 | 29 | from aredis_om.model.model import ExpressionProxy |
29 | 30 |
|
@@ -2024,3 +2025,49 @@ class Meta: |
2024 | 2025 | assert retrieved.pk == 42 |
2025 | 2026 | assert retrieved.x == 42 |
2026 | 2027 | 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