ancient-wolf-19325
08/17/2024, 10:27 PMexecution = remote.fetch_execution(name="...", project="...", domain="...")
execution_output = {}
for key, val in execution.outputs.items():
execution_output[key] = val
this works well if the output is a python dict. But it will throw exception Cannot convert from <FlyteLiteral scalar { primitive
{ string_value:… to <class Dict> when the workflow return type is primitive type. how could I handle both scenarios? Any help is appreciated!
# Answer
To convert Flyte execution output to a JSON string, you can use the to_dict method provided by Flyte's TypeEngine to handle both primitive and complex types. Here's an example:
from flytekit.remote.remote import FlyteRemote, Config
from flytekit.core.type_engine import TypeEngine
import json
remote = FlyteRemote(config=Config.auto(config_file="path/to/config.yaml"))
execution = remote.fetch_execution(name="...", project="...", domain="...")
execution_output = {}
for key, val in execution.outputs.items():
execution_output[key] = TypeEngine.to_python_value(val)
json_output = json.dumps(execution_output)
print(json_output)
This approach ensures that both primitive and complex types are correctly converted to their Python equivalents before being serialized to JSON.
Would you like more details on this approach or any other aspect of Flyte?
Sources:
- TypeEngine Source Code
Slack Link
https://flyte-org.slack.com/archives/CP2HDHKE1/p1723933608.410679 👍 0 button 👎 0 button 💡 Hint: Mention @runllm in the thread for followups.