Skip to content

Conversation

@jstol
Copy link
Contributor

@jstol jstol commented May 15, 2025

Currently, using Nested[list[SubObject] to automatically extract properties from data models isn't supported – only using Nested[SubObject] within a TypedDict data model type results in automatic marshalling of the property, even though both object and object[] types can be used for nested properties within a collection. This PR adds support for auto-extracting nested object[] properties in addition to nested object properties via the Nested generic wrapper type.

Example
from typing import TypedDict

import weaviate
import weaviate.classes.config as wc
from weaviate.classes.generics import Nested

client = weaviate.connect_to_local()

class Child(TypedDict):
    age: int
    name: str

class Parent(TypedDict):
    age: int
    name: str
    children: Nested[list[Child]]

collection = client.collections.create(
    name="Parent",
    properties=[
        wc.Property(name="age", data_type=wc.DataType.INT),
        wc.Property(name="name", data_type=wc.DataType.TEXT),
        wc.Property(
            name="children", 
            data_type=wc.DataType.OBJECT_ARRAY,
            nested_properties=[
                wc.Property(name="age", data_type=wc.DataType.INT),
                wc.Property(name="name", data_type=wc.DataType.TEXT),
            ]
        )
    ],
    vectorizer_config=wc.Configure.Vectorizer.none(),
    data_model_properties=Parent,
)
    
collection.data.insert({
    "age": 39,
    "name": "Bob Smith",
    "children": [{
        "age": 8,
        "name": "Joe Smith",
    }, {
        "age": 10,
        "name": "Jane Smith",
    }]
})

res = collection.query.fetch_objects()
print(res.objects[0].properties)
Current State

Prior to this change, running the above snippet resulted in an error. (This was due to the list[SubObject] type being passed to typing.get_type_hints within the extraction logic, rather than the underlying SubObject type.)

>>> TypeError: list[Child] is not a module, class, method, or function.
New State

With this change, the inserted data is now fetched and marshalled back to the TypedDict data model.

{'age': 39, 'name': 'Bob Smith', 'children': [{'age': 8, 'name': 'Joe Smith'}, {'age': 10, 'name': 'Jane Smith'}]}

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

@weaviate-git-bot
Copy link

To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Slack channel?

@jstol
Copy link
Contributor Author

jstol commented May 15, 2025

To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Slack channel?

I agree with the CLA.

@dirkkul dirkkul merged commit 4c3b939 into weaviate:main May 16, 2025
54 of 56 checks passed
@dirkkul
Copy link
Collaborator

dirkkul commented May 16, 2025

Thank you very much :)

@jstol
Copy link
Contributor Author

jstol commented May 16, 2025

@dirkkul thanks for the speedy approval and merge! 🚀

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.

3 participants