<@U06RKAMK0UC> Can I return a dataclass with a dat...
# ask-ai
w
@ancient-wolf-19325 Can I return a dataclass with a datetime field from a Flyte workflow
j
Yes, you can return a dataclass with a
datetime
field from a Flyte workflow. Flyte supports dataclasses as task and workflow inputs/outputs, and
datetime.datetime
is a supported type for serialization within dataclasses. However, you should not use
datetime.date
(without time) as a field type, as it is not fully supported and may cause errors—use
datetime.datetime
instead for date/time fields in dataclasses within Flyte workflows. All dataclass fields must be type-annotated for Flyte to serialize/deserialize them correctly. For example:
Copy code
from dataclasses import dataclass
from datetime import datetime
from flytekit import task, workflow

@dataclass
class MyData:
    timestamp: datetime

@task
def make_data() -> MyData:
    return MyData(timestamp=datetime.now())

@workflow
def my_wf() -> MyData:
    return make_data()
See Flyte dataclass docs and datetime support issue for details. Would you like more detail or examples on this topic? Sources: - Flyte dataclass docs - datetime support issue 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.
w
@ancient-wolf-19325 Can put a protobuf in the dataclass as well
j
No, you cannot directly use a protobuf message as a field in a dataclass for Flyte task/workflow inputs or outputs. Flyte's type system does not natively support serializing/deserializing arbitrary protobuf messages within dataclasses. To use protobufs, you must implement a custom TypeTransformer or serialize the protobuf to a supported type (e.g., bytes or JSON) before including it in the dataclass. This limitation is discussed in the Flyte documentation and related issues. See Flyte dataclass docs and custom typing issue. Would you like more detail or code examples for custom serialization? Sources: - custom typing issue - Flyte dataclass docs 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.