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.

_pickle.PicklingError encountered when attempting to serialize BaseDoc type #1760

Closed

Description

@KenyonY

Initial Checks

  • I have read and followed the docs and still think this is a bug

Description

After using docarray.documents.helper.create_doc to create a BaseDoc type, this type cannot be serialized by Pickle:

from docarray.documents.helper import create_doc

MyDoc = create_doc(
        'MyDoc',
        title=(str, ...),
        title_tensor= (Optional[NdArray[128]], ...),
        )

buffer = pickle.dumps(MyDoc)

Out:

_pickle.PicklingError: Can't pickle <class 'docarray.documents.helper.MyDoc'>: attribute lookup MyDoc on docarray.documents.helper failed

Example Code

No response

Python, Pydantic & OS Version

docarray: 0.37.1  
python: 3.10.12

Affected Components

Activity

  1. JoanFM commented on Aug 23, 2023

    @JoanFM
    Member

    Hello @beidongjiedeguang,

    May I ask what are you trying to do here? Here you are pickling a class, is it the intended behavior?

  2. KenyonY commented on Aug 23, 2023

    @KenyonY
    Author

    Hi @JoanFM ,
    Yes, because this type is dynamically created. If it is not saved, it will need to be recreated the next time DocIndex(such as HnswDocumentIndex) is launched. Therefore, I must save the metadata for creating this class. So if possible, I would like to directly save this type.

  3. JoanFM commented on Aug 24, 2023

    @JoanFM
    Member

    This is the same problem as seen in Pydantic here pydantic/pydantic#4178

  4. JoanFM commented on Aug 24, 2023

    @JoanFM
    Member

    So, a solution is to declare it with module parameter:

    from docarray.documents.helper import create_doc
    import pickle
    
    
    MyDoc = create_doc(
        'MyDoc',
        __module__=__name__,
        title=(str, ...)
    )
    
    buffer = pickle.dumps(MyDoc)

    This works for me

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions