Hi, anyone have a guess as to why `TypeTransformer...
# announcements
z
Hi, anyone have a guess as to why
TypeTransformerFailedError
occurs when using imported variables in workflows, but if the variables are declared in the same python module as the workflow there is no error? Thank you!
s
Hi Zachary, thanks for your question. Do you also receive any other error message alongside the TypeTransformerFailedError
z
Nope. I’m avoiding copying/pasting the stack trace for company privacy reasons
I can try and reproduce it in a dummy format if that will help. More details are that the types failing when imported are annotated with @dataclass and @dataclass_json
pseudocode is this: @task tsk(x: list[MyDataclass]) -> None: result = f(x) print(result) @workflow wf(x: list[MyDataclass]) -> None: tsk(x)
if the input for x is defined in the same module as the task/workflow code there’s no error, but when importing identical code (copy/pasted) from a neighbor module, it produces the error
Copy code
File "<redacted>/lib/python3.9/site-packages/flytekit/core/type_engine.py", line 97, in assert_type
    raise TypeTransformerFailedError(f"Type of Val '{v}' is not an instance of {t}")
flytekit-1.1.0
hmm, well, this works: m2.py
Copy code
from flytekit import task, workflow
from m1 import MyDataclass, inputs


def f(x: list[MyDataclass]) -> MyDataclass:
    return x[0]


@task
def tsk(x: list[MyDataclass]) -> None:
    result = f(x)
    print(result)


@workflow
def wf(x: list[MyDataclass]) -> None:
    tsk(x=x)


wf(x=inputs)
m1.py
Copy code
from dataclasses import dataclass

from dataclasses_json import dataclass_json


@dataclass_json
@dataclass
class MyDataclass:
    a: int


inputs = [MyDataclass(1)]
s
So was there a separate implementation in your previous example? because when just importing class, variables it should work.
z
So was there a separate implementation in your previous example?
iiuc, yes, the real code is different than the example code shown here
figured it out. In the real code the equivalent of
m1.py
is importing dataclasses from a third module (eg.
m3.py
). If a relative import path is used it produces the
TypeTransformerFailedError
. If an absolute path is used, it works
@Shivay Lamba, thank you for the help! Do you think this error is the appropriate error when it’s really do to the way flytekit is interpreting types based on import paths?
👍 1
a
Glad you solved it. There is an alternative solution also to append the module path to sys as follows:
Copy code
import sys
sys.path.append('d/modules/')
Then you don't have to worry about relative or absolute path.
🙏 1
👍 1
z
thank you for this @Alireza! When I register the workflow on our Flyte cluster, the same error returns although it’s now passing locally. I’m guessing it has something to do with the import paths again, but now that they’re all absolute paths, idk what else to change. Any ideas for things to try?
a
@Zachary Carrico Absolutely. One inefficient option is to dump the entire of your code (input and workflow) into one test script. Then run it. Do you think you can do that?
z
ty! I’m assuming that would work, but what would be the long-term solution?
a
If you give that a try. My solution is to use "relative path". This is a very standard option. I attach it here for you to look.
Copy code
@workflow
def new_wf_test():
    new_dynamic_test()
    from core import inputModule # <<<<---- PAY ATTENTION HOW IMPORTED
    print(inputModule.varInt)
    print(inputModule.varString)
    return
Additional in your case I strongly recommend to use bash script like the following
Copy code
#!/usr/bin/env bash
set -e

pyflyte -c ~/.flyte/demo-config.yaml run --service-account demo --remote core/on_examples.py new_wf_test
z
ty! for reference, the full error message is
Copy code
TypeTransformerFailedError: Type of Val 'MyDataset(name='my_name', label=0)' is not an instance of <class 'types.MyDatasetSchema'>
I’m trying to understand what’s causing the error. afaik
type(MyDataset)
is
MyDataset
, not
MyDatasetSchema
. Would you please help me understand why it isn’t checking that the instance is of type
MyDataset
? (this is for a
class MyDataset
decorated with @dataclass_json and @dataclass)
y
sorry, still skimming, what is MyDatasetSchema
when a workflow runs locally, flytekit will go through the same serialization/deserialization steps as in production btw.
in either case, when this serde process runs, the class that you get back is not the same.
it’s a dataclass(json) class that looks the same, but it’s not the same
z
ty for explaining! Do you think it’s comparing the dataclass type to the dataclass’s json type and saying they’re not the same?
y
you can see that code here
i’m confused where the string “Schema” is coming from…
z
auto-generated in Flyte’s error msg
y
i don’t think this is something added by marshmallow
maybe it is
z
raise TypeTransformerFailedError(f"Type of Val '{v}' is not an instance of {t}")
L97 for flytekit.core.type_engine
I think it’s doing something like v = type(MyDataset) t = type(MyDataset.to_json)
I’m still pretty sure it has something to do with import paths. Locally, if relative paths were used it would produce this error, but once absolute paths were used, the error went away locally, but not in the Flyte cluster
y
sorry do you have a mvp repro? or did i miss that above?
z
nope. It’s going to be easier for me to convert the dataclasses to dictionaries for workflow input and then convert them back again into dataclasses in the workflow than it will be to create a reproducible example of this. So I will do that, but appreciate everyone looking into this
153 Views