Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] dataclass and pydantic both failed when using Flytekit Remote API #6081

Open
2 tasks done
Future-Outlier opened this issue Dec 4, 2024 · 2 comments
Open
2 tasks done
Assignees
Labels
bug Something isn't working flytekit FlyteKit Python related issue

Comments

@Future-Outlier
Copy link
Member

Future-Outlier commented Dec 4, 2024

Describe the bug

Take dataclass as an example, if you use a dataclass as a task's input or output, and you use the flytekit remote API to fetch the result, you will get an error.

example code

from flytekit.remote.remote import FlyteRemote
from flytekit.configuration import Config
remote = FlyteRemote(
    Config.for_endpoint("localhost:30080", True),
)
o = remote.get("flyte://v1/flytesnacks/development/am8tmmtmlnht4627qc4z/dataclassexamplet1/o") # this is a datacalss or pydantic basemodel
print(o['o0'])

error

╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ /Users/future-outlier/code/dev/flytekit/build/pydantic/dataclass_remote_api. │
│ py:12 in <module>                                                            │
│                                                                              │
│ ❱ 12 print(o['o0'])                                                          │
│                                                                              │
│ /Users/future-outlier/code/dev/flytekit/flytekit/core/type_engine.py:2648 in │
│ __getitem__                                                                  │
│                                                                              │
│ ❱ 2648 │   │   return self.get(key)                                          │
│                                                                              │
│ /Users/future-outlier/code/dev/flytekit/flytekit/core/type_engine.py:2676 in │
│ get                                                                          │
│                                                                              │
│ ❱ 2676 │   │   │   │   │   raise ValueError("as_type argument not supplied a │
╰──────────────────────────────────────────────────────────────────────────────╯
ValueError: as_type argument not supplied and Variable map not specified in 
LiteralsResolver

Expected behavior

This should work.

Additional context to reproduce

@dataclass
class A:
    a: int
    b: float

@task(container_image=image)
def t1(a: A) -> A:
    print(a)
    return a

Screenshots

No response

Are you sure this issue hasn't been raised already?

  • Yes

Have you read the Code of Conduct?

  • Yes
@Future-Outlier Future-Outlier added bug Something isn't working untriaged This issues has not yet been looked at by the Maintainers labels Dec 4, 2024
@Future-Outlier Future-Outlier self-assigned this Dec 4, 2024
@eapolinario eapolinario added flytekit FlyteKit Python related issue and removed untriaged This issues has not yet been looked at by the Maintainers labels Dec 5, 2024
@Future-Outlier
Copy link
Member Author

I will mentor my friend do this, let's go

@Future-Outlier
Copy link
Member Author

Actionable Step to solve this issue

  1. run this code in flyte
import typing
import os
from dataclasses import dataclass, field
from typing import Dict, List
from enum import Enum

from flytekit import task, workflow, ImageSpec

flytekit_hash = "11fbd9aecd375cab70c79b581bdf4a28b37b4a41"
flytekit = f"git+https://github.com/flyteorg/flytekit.git@{flytekit_hash}"
image = ImageSpec(
    packages=[flytekit,],
    apt_packages=["git"],
    registry="localhost:30000",
    env={"FLYTE_USE_OLD_DC_FORMAT": "true"},
)

@dataclass
class DC:
    a: int

@task(container_image=image)
def t1() -> DC:
    return DC(a=5)


if __name__ == "__main__":
    from flytekit.clis.sdk_in_container import pyflyte
    from click.testing import CliRunner
    import os
    runner = CliRunner()
    path = os.path.realpath(__file__)

    result = runner.invoke(pyflyte.main, ["run", "--remote", path, "t1",])
    print("Remote Execution: ", result.output)
  1. run this code in your terminal
from flytekit.remote.remote import FlyteRemote
from flytekit.configuration import Config
remote = FlyteRemote(
    config=Config.for_endpoint("localhost:30080", True),
)
o = remote.get("flyte://v1/flytesnacks/development/a7g427xqqw94d222rsmz/easyexamplet1/o")
# o['o0'] this will fail

task_data = remote.fetch_task(project="flytesnacks", domain="development", name="easy_example.t1",)
print(task_data)
print(task_data.interface.outputs['o0'].type) # flytekit.models.types.LiteralType
# We can use `guess_python_type` to convert json schema (stored in the metadata to python types)
  1. study how flyteadmin's dataproxy parse flyte uri

ref1: https://github.com/flyteorg/flyte/blob/b3330ba4430538f91ae9fc7d868a29a2e96db8bd/flyteadmin/dataproxy/service.go
ref2: https://github.com/flyteorg/flyte/blob/b3330ba4430538f91ae9fc7d868a29a2e96db8bd/flyteadmin/pkg/common/flyte_url.go

  1. when calling get in flytekit remote, provide variable map to LiteralsResolver

https://github.com/flyteorg/flytekit/blob/a3d8f23b838df9addfaddebacd842a02705b2250/flytekit/remote/remote.py#L362

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working flytekit FlyteKit Python related issue
Projects
Status: Backlog
Development

No branches or pull requests

4 participants