Am I correct in understanding that Flyte does not ...
# ask-the-community
Am I correct in understanding that Flyte does not support custom encoders and decoders for dataclasses_json.
Copy code
class MyDataClass:
     foo: Foo = field(metadata=dict(encoder=encoder, decoder=decoder))
Is the only alternative to define and register a custom
? What's the logic behind requiring something comparatively cumbersome?
I ask because I find myself writing the following pattern a lot, which the above is essentially sugar for.
Copy code
class MyDataClass:
   foo_str: str

   def __post_init__(self): = decoder(self.foo_str)

   def from_foo(cls, foo: Foo):
       return cls(foo_str=encoder(foo))
The field type, in this case
, has to be a valid Flyte type. If that isn't the case, I believe the data gets pickled. @Kevin Su, could you please confirm?
no, we can’t use pickle, because it’s not dataclass or python primitive type. I think we can support custom encoder/decoder here. just need to update the dataclass transformer here. mind create a ticket, and share your encoder/decoder code
@Kevin Su Done I think this is super straightforward
AFAIK the default encoder and decoder (without additional metadata), assumes a serialization to string. If I understand correctly, you can also specify
to specify an alternate intermediate marshmallow type in the schema, which then needs to match up with the return/call signature of your encoder/decoder respectively.
But I haven't tried that
If you're asking what I personally use this for, I like to use it for serializing type and function objects. So for example:
Copy code
def import_from_str(obj_qual_name: str):
    module_str, obj_qual_name = obj_qual_name.rsplit(sep='.', maxsplit=1)
    module = importlib.import_module(module_str)
    return getattr(module, obj_qual_name)

def full_name_from_obj(obj) -> str:
    return f'{inspect.getmodule(obj).__name__}.{obj.__qualname__}'


class ModelSpec:
    model: Type[PreTrainedModel]
    preprocessor_factory: Type[PreprocessorFactory] = field(metadata=TYPE_SERIALIZER)
    training_args: Dict = field(default_factory=dict)
    model_weights: PyTorchCheckpoint
    model_args: Dict = field(default_factory=dict)
    weight_preprocessing: Callable = field(metadata=TYPE_SERIALIZER, default=noop)
Thanks for the clarification, will take a look. contributions are welcome too
Yeah, I may try to contribute if I can find time, since someone snagged the other feature I was looking for 🙂
Thanks! let me know anything I can help