Hi, team. I got a flyte workflow that returns a Na...
# ask-the-community
z
Hi, team. I got a flyte workflow that returns a NamedTuple
Copy code
return ModelOutput(
        model_version=model_version,
        bst_rounds=training_output.bst_rounds,
        classifier=training_output.classifier
    )
And the it is defined as
Copy code
ModelOutput = NamedTuple(
    "ModelOutput",
    model_version=ModelVersion,
    bst_rounds=int,
    classifier=Classifier
)
It got error when packaging
Copy code
Transformer for type <class 'tuple'> is restricted currently
However, it used to work when I set the ModelOutput as below, my changes is just add a new filed to the existing tuple. Anyone knows this issue? Thanks in advance
Copy code
ModelOutput = NamedTuple(
    "ModelOutput",
    model_version=ModelVersion,
    bst_rounds=int
)
FYI, there is another structure in our code that gets to work
Copy code
TrainingOutput = NamedTuple(
    "TrainingOutput",
    classifier=Classifier,
    model_pickle=FlyteFile[TypeVar("pkl")],
    model_bst=FlyteFile[TypeVar("bst")],
    feature_importance=PNGImageFile,
    bst_rounds=int
)
s
This is weird. Could you share your code snippet? Will try reproducing the error.
v
I am also facing similar issue in our project :
"Exception when executing Tuples are not a supported type for individual values in Flyte - got a tuple
z
hi, @Samhita Alla here it is
Copy code
@workflow
def train_register_model(
        config: Config,
        all_data: DataSlicer,
        stage: str,
        last_best_rounds: int,
        validate_on_service_type: bool) -> ModelOutput:
    """This workflow trains an xgb classifier and create a new model
    version on Merlin.

    Creation of a Merlin model version implies the registration of the
    model and log the artifacts/metadata on Mlflow.
    """
    training_output: TrainingOutput = train_classifier(
        config=config, all_data=all_data, stage=stage, last_best_rounds=last_best_rounds
    )
    offline_metrics: Dict = compute_offline_metric(
        model=training_output.classifier,
        all_data=all_data,
        stage=stage,
        config=config,
        on_service_type=validate_on_service_type,
    )
    metadata: Metadata = create_metadata(
        config=config, all_data=all_data, metrics=offline_metrics, stage=stage
    )
    model_version: ModelVersion = create_pyfunc_model_version(
        config=config,
        model_pickle=training_output.model_pickle,
        model_bst=training_output.model_bst,
        feature_importance=training_output.feature_importance,
        metadata=metadata,
    )
    return ModelOutput(
        model_version=model_version,
        bst_rounds=training_output.bst_rounds,
        classifier=training_output.classifier
    )

ModelOutput = NamedTuple(
    "ModelOutput",
    model_version=ModelVersion,
    bst_rounds=int,
    classifier=Classifier
)
s
@Zhang Yue, what's
classifier
here? What's its type?
@Vipul Goswami, tuples aren't supported but NamedTuple is.
v
@Samhita Alla thanks for the reply, i tried with NamedTuple once but will try again
s
Okay. Let me know if you come across any error with NamedTuple.
z
hi, @Samhita Alla classifier is a class
Copy code
class Classifier(ABC):
    @abstractmethod
    def fit(self, *params) -> None:
        pass
s
wf_outputs = typing.NamedTuple("OP2", greet1=str, greet2=str, classifier=Classifier)
works for me. What's
ModelVersion
?
z
you mean it is able to package is it?
s
I ran the
pyflyte run
command and yes, it's working for me. What flytekit version have you installed?
The code I ran:
Copy code
import typing
from flytekit import task, workflow

hello_output = typing.NamedTuple("OP", greet=str)

@task
def say_hello() -> hello_output:
    return hello_output("hello world")

class Classifier:
    def __init__(self) -> None:
        pass

wf_outputs = typing.NamedTuple("OP2", greet1=str, greet2=str, classifier=Classifier)

@workflow
def my_wf(classifier: Classifier = Classifier()) -> wf_outputs:
    return wf_outputs(say_hello().greet, say_hello().greet, classifier)