Skip to content

Creating DocVec with the Object that has Optional Field #1468

@movchan74

Description

@movchan74

I am experiencing difficulty creating a DocVec of an Image object with an optional "features" field. When attempting to create a DocVec of Images, where each Image contains an optional Features object, I receive the error message: "ValueError: None is not a <class 'main.Features'>".

To provide some context, I am using the following code:

import numpy as np
from docarray import BaseDoc, DocList, DocVec
from docarray.typing import NdArray, ImageUrl
from typing import Optional

class Features(BaseDoc):
    tensor: NdArray

class Image(BaseDoc):
    url: ImageUrl
    features: Optional[Features]

docs = DocVec[Image](
    [Image(url='http://url.com/foo.png') for _ in range(10)]
)

I have attempted to resolve the issue by using NdArray directly.

class Image(BaseDoc):
    url: ImageUrl
    features: Optional[NdArray]

While this code works, it is not ideal as I would prefer to use the Features class for the features field. I also attempted to create the Features class with an optional tensor field, which resolved the issue, but does not seem like the correct solution.

class Features(BaseDoc):
    tensor: Optional[NdArray]

My question is whether this behavior is expected or if I am doing something incorrectly.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions