flyte: [BUG] Can't use int types on tasks, flyte casts to float between tasks
Describe the bug
I’m trying to use the int type along my workflow but it keeps being parsed as float and complaining about it when passing it downstream to other tasks. I even try to cast it within a task but it casts it back to float.
Expected behavior
For objects within tasks to remain typed as int or flyte to cast them properly (complains about not being able to cast a float with no decimals to int, which is possible)
Additional context to reproduce
I have this main.py file containing my workflow:
from flytekit import task, workflow
import os
import yaml
@task
def read_yaml(yaml_path: str) -> dict:
with open(yaml_path, "r") as file:
config = yaml.safe_load(file)
config['id'] = int(config['id'])
return config
@task
def check_int(x: int) -> int:
return x
@workflow
def workflow(yaml_path: str) -> int:
config = read_yaml(yaml_path=yaml_path)
return check_int(x=config['id'])
And a config.yaml where the int should be read from:
id: !!int 1
When I run this with the command
pyflyte run main.py workflow --yaml_path config.yaml
I get the error:
Running Execution on local.
2023-11-30 15:36:01,527455 ERROR {"asctime": "2023-11-30 15:36:01,527", "name": "flytekit", "levelname": "ERROR", "message": "Failed to convert inputs of task 'main.check_int':\n Error base_task.py:621
converting input 'x' at position 0:\n Cannot convert literal <FlyteLiteral scalar { primitive { float_value: 1 } }> to <class 'int'>"}
Failed with Unknown Exception <class 'flytekit.core.type_engine.TypeTransformerFailedError'> Reason: Encountered error while executing workflow 'main.workflow':
Error encountered while executing 'workflow':
Failed to convert inputs of task 'main.check_int':
Error converting input 'x' at position 0:
Cannot convert literal <FlyteLiteral scalar { primitive { float_value: 1 } }> to <class 'int'>
Encountered error while executing workflow 'main.workflow':
Error encountered while executing 'workflow':
Failed to convert inputs of task 'main.check_int':
Error converting input 'x' at position 0:
Cannot convert literal <FlyteLiteral scalar { primitive { float_value: 1 } }> to <class 'int'>
The yaml is typed to be int and i even cast it again within the read task but it still gets casted to float.
Screenshots
No response
Are you sure this issue hasn’t been raised already?
- Yes
Have you read the Code of Conduct?
- Yes
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 31 (15 by maintainers)
Folks I just made an issue that might solve all these problems check it out and please comment
I do understand the dataclass you wrote - as I said we have a similar approach in mind - but consider the difference in maintainability between what you have above and simply writing the union type
int | float(whichdataclasses-jsonalready serializes and deserializes correctly as expected). Or having to hit an external service like S3 to persist one small part of our input in a bespoke way.Our pipeline and CLI tooling handles the transformations on this arbitrary JSON input as well as the business logic around our strongly typed inputs quite well with
dataclasses-json, except for this one hiccup where Flyte coerces all our numeric types to floats whenever we expect to handle anint | float.My point is not that it is impossible for us to hack around this limitation: it’s that this is not a good UX and not at all an intuitive way to handle a type annotated as
int | float.🤖
Hello @jiwidi,
Nice to meet you! I’m Dosu, an AI bot here to assist you with bugs, answer your questions, and guide you as a contributor while we wait for a human maintainer. I’m here to help, so don’t hesitate to ask if you have any further questions or issues!
I understand that you’re having an issue with integer types being cast to float in your Flyte workflow. This seems to be a known issue that has been reported in the past. Specifically, there are two similar issues that have been resolved:
Both of these issues were addressed by the following pull requests:
Based on the error message you provided, it seems that the integer value is being interpreted as a float when it’s passed to the
check_inttask. This could be due to the way theread_yamltask is returning the value.In Flyte, the type conversion process is handled by the
AreTypesCastablefunction, which checks if the source type can be cast to the target type. According to the source code, integers should not be castable to floats and vice versa.Given this, it’s possible that there might be a bug in Flyte causing this issue. However, it’s also possible that the issue might be due to the way the
read_yamltask is returning the value.To help narrow down the issue, could you please provide the following additional information:
This information will help us better understand the issue and provide a more accurate solution. Thank you for your patience and cooperation.
Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
@jiwidi this is fixed in 1.14. Feel free to reopen if needed. Thanks!
Sorry, I’m busy with the company’s work recently and don’t have time to look on this