Skip to content
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

json method not working correctly with DocVec #1490

Closed

Description

@movchan74

Description:

The json method doesn't work correctly when using DocVec. The json output for a field containing a DocVec instance returns null.

Steps to reproduce:

Using the following code with a custom class and DocList:

from docarray import DocVec, BaseDoc, DocList
from docarray.documents.image import ImageDoc

class CustomClass(BaseDoc):
    images: DocList[ImageDoc]
        
images = DocList[ImageDoc]([ImageDoc(url=f'http://url.com/{i}.png') for i in range(10)])
custom_instance = CustomClass(images=images)
custom_instance.json()

Output:

'{"id":"05094c8c7045d988c58791da43f79584","images":[{"id":"c7cb6d04ddda6d5f6299959fcae6241a","url":"http://url.com/0.png","tensor":null,"embedding":null,"bytes_":null},{"id":"1175d1988e1173b5217dd148dca705f7","url":"http://url.com/1.png","tensor":null,"embedding":null,"bytes_":null},{...}]}' 

Using the following code with a custom class and DocVec:

from docarray import DocVec, BaseDoc, DocList
from docarray.documents.image import ImageDoc

class CustomClass(BaseDoc):
    images: DocVec[ImageDoc]
        
images = DocVec[ImageDoc]([ImageDoc(url=f'http://url.com/{i}.png') for i in range(10)])
custom_instance = CustomClass(images=images)
custom_instance.json()

Output:

'{"id":"2f040340fa6ad9dbc10d394609251afa","images":null}'

Expected behavior:

The json method should return the correct output when using DocVec with any custom class, similar to how it works with DocList.

Activity

  1. moved this to Community issues in DocArray backlogon May 4, 2023
  2. moved this from Community issues to Todo/Bug in DocArray backlogon May 4, 2023
  3. JoanFM commented on May 5, 2023

    @JoanFM
    Member

    Note:

    on main, the first part raises a ValueError:

    Traceback (most recent call last):
      File "aux.py", line 9, in <module>
        print(custom_instance.json())
      File "/home/joan/jina/docarray/docarray/base_doc/doc.py", line 221, in json
        **dumps_kwargs,
      File "pydantic/main.py", line 492, in pydantic.main.BaseModel.json
      File "pydantic/main.py", line 868, in _iter
      File "pydantic/main.py", line 794, in pydantic.main.BaseModel._get_value
      File "/home/joan/jina/docarray/docarray/array/doc_list/doc_list.py", line 120, in __init__
        super().__init__(docs)
      File "/home/joan/jina/docarray/docarray/array/doc_list/doc_list.py", line 148, in _validate_docs
        yield self._validate_one_doc(doc)
      File "/home/joan/jina/docarray/docarray/array/doc_list/doc_list.py", line 153, in _validate_one_doc
        raise ValueError(f'{doc} is not a {self.doc_type}')
    ValueError: {'id': '08ddfb573dff754baee479e1b3a894b6', 'url': ImageUrl('http://url.com/0.png', host_type='domain'), 'tensor': None, 'embedding': None, 'bytes_': None} is not a <class 'docarray.documents.image.ImageDoc'>
  4. samsja commented on May 5, 2023

    @samsja
    Member

    Hey @movchan74 so far we don't support to_json on DocVec which is why the nested DocVec cannot be serialized with json. Once we support to_json on DocVec we will make sure that the nested support is working and therefore fix the bug that you see.

  5. JoanFM commented on May 10, 2023

    @JoanFM
    Member

    Closing in favor of #1330

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions