melodic-mechanic-59879
03/12/2025, 2:18 PMjolly-florist-66728
03/12/2025, 2:18 PMmelodic-mechanic-59879
03/12/2025, 2:19 PMmelodic-mechanic-59879
03/12/2025, 2:20 PMjolly-florist-66728
03/12/2025, 2:20 PMMyDataset
type:
from flytekit import task, workflow, TypeEngine, FlyteContext
from flytekit.models.literals import Literal, Scalar, Blob, BlobMetadata
from flytekit.models.types import LiteralType, SimpleType
from flytekit.extend import TypeTransformer
class MyDataset:
def __init__(self, data):
self.data = data
class MyDatasetTransformer(TypeTransformer[MyDataset]):
def __init__(self):
super().__init__(name="MyDataset", t=MyDataset)
def get_literal_type(self, t: type) -> LiteralType:
return LiteralType(simple=SimpleType.STRUCT)
def to_literal(self, ctx: FlyteContext, python_val: MyDataset, python_type: type, expected: LiteralType) -> Literal:
# Serialize the data to a file and return a Blob literal
local_file = ctx.file_access.get_random_local_path() + ".txt"
with open(local_file, "w") as f:
f.write(python_val.data)
remote_file = ctx.file_access.get_random_remote_path()
ctx.file_access.upload(local_file, remote_file)
return Literal(scalar=Scalar(blob=Blob(uri=remote_file, metadata=BlobMetadata(type=self.get_literal_type(python_type)))))
def to_python_value(self, ctx: FlyteContext, lv: Literal, expected_python_type: type) -> MyDataset:
# Deserialize the data from a file
local_file = ctx.file_access.get_random_local_path() + ".txt"
ctx.file_access.download(lv.scalar.blob.uri, local_file)
with open(local_file, "r") as f:
data = f.read()
return MyDataset(data)
# Register the custom transformer
TypeEngine.register(MyDatasetTransformer())
@task
def process_data(data: MyDataset) -> str:
return f"Processed data: {data.data}"
@workflow
def my_workflow(data: str) -> str:
dataset = MyDataset(data)
return process_data(data=dataset)
This code defines a custom type transformer for MyDataset
and registers it with Flytekit. The process_data
task and my_workflow
workflow demonstrate how to use this transformer.
Would you like more details on any specific part?
Sources:
- Custom types 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.