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)

Most upvoted comments

Folks I just made an issue that might solve all these problems check it out and please comment

I do not believe you understand the functionality of MyOutput as it is implemented above. In the above example, it will automatically coerce those floats back to integers upon deserialization, thus still conforming to its original datatype:

my_output_instance = MyOutput.from_value(33)
serialized = my_output_instance.to_json()
deserialized = MyOutput.from_json(serialized)

print(deserialized) # MyOutput(33: int)

You may also return list[MyOutput] and/or dict[str, MyOutput] using the data class I had previous described. Additionally, you may create other custom data classes around MyOutput as well. The serialization / deserialization operations will work as expected

However, if you are literally trying to return arbitrarily nested dictionaries / lists of floats and/or ints, the idea of having a task that returns an output like dict[str, list[float|int] | float | int] | list[dict[str, list[float | int] | float | int] | float | int seems quite dangerous. Having worked with KFP for several years and Flyte for the last few months, I very much appreciate their strict type enforcement. They force me to avoid bad patterns.

If you are working with complex, arbitrarily nested data, you might consider writing it to an actual file (flytekit.types.file.FlyteFile). Data formats such as avro works quite well with arbitrarily nested data. Avro can also differentiate between floats and ints.

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 (which dataclasses-json already 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 an int | 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:

  1. Dataclass int field becomes float when passed to task
  2. int values in Optional[int] dataclass fields are converted to floats when passed into Flyte tasks

Both of these issues were addressed by the following pull requests:

  1. Fix the type of optional[int] in nested dataclass
  2. Fix the type of optional[int] in dataclass

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_int task. This could be due to the way the read_yaml task is returning the value.

In Flyte, the type conversion process is handled by the AreTypesCastable function, 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_yaml task is returning the value.

To help narrow down the issue, could you please provide the following additional information:

  1. The version of Flyte you’re using.
  2. The version of the Flytekit Python SDK you’re using.
  3. Any additional context or code that might help reproduce the issue.

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