Skip to content

[BUG] Serialization bug with pydantic.BaseModel and multiple StructuredDataset #6574

@ebran

Description

@ebran

Flyte & Flytekit version

Flyte 1.15.1
Flytekit 1.16.3

Describe the bug

Flyte fails to serialize a pydantic.BaseModel with multiple StructuredDataset fields, but dataclass works.

import flytekit as fl
from pydantic import BaseModel
from pydantic.dataclasses import dataclass

# This becomes an empty dict (does not work) after serialization by Flyte when used as output in a task.
class BaseModelTwo(BaseModel):
    foo: fl.StructuredDataset
    bar: fl.StructuredDataset

# This is serialized (works) by Flyte when used as output in a task.
@dataclass
class DataClassTwo:
    foo: fl.StructuredDataset
    bar: fl.StructuredDataset

Expected behavior

Both pydantic.Basemodel and dataclass should work to annotate the output.

Additional context to reproduce

One can test with these tasks.

import flytekit as fl
import numpy as np
import pandas as pd

# Does not work (empty dict as output)
@fl.task
def return_basemodel_two() -> BaseModelTwo:
    return BaseModelTwo(
        foo=fl.StructuredDataset(dataframe=pd.DataFrame(np.random.rand(3, 3))),
        bar=fl.StructuredDataset(dataframe=pd.DataFrame(np.random.rand(3, 3))),
    )


# Works (returns populated dataclass as expected) 
@fl.task
def return_dataclass_two() -> DataClassTwo:
    return DataClassTwo(
        foo=fl.StructuredDataset(dataframe=pd.DataFrame(np.random.rand(3, 3))),
        bar=fl.StructuredDataset(dataframe=pd.DataFrame(np.random.rand(3, 3))),
    )

Screenshots

No response

Are you sure this issue hasn't been raised already?

  • Yes

Have you read the Code of Conduct?

  • Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinguntriagedThis issues has not yet been looked at by the Maintainers

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions