-
Notifications
You must be signed in to change notification settings - Fork 278
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
Pydantic Transformer V2 #2792
Open
Future-Outlier
wants to merge
20
commits into
master
Choose a base branch
from
pydantic-plugin-v2-with-msgpack
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+950
−29
Open
Pydantic Transformer V2 #2792
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
333a05c
Pydantic Transformer V2
Future-Outlier 9251508
add __init__.py
Future-Outlier 4c46dee
add json schema
Future-Outlier 63ab9fd
convert float to int
Future-Outlier 357ca00
change gitsha in test script mode
Future-Outlier cdd1b25
change gitsha
Future-Outlier c89b59a
use strict map=false
Future-Outlier 6b480da
Test flytefile console input + attr access
Future-Outlier 15ce9ad
add conditional branch
Future-Outlier 94ce092
better rx
Future-Outlier d97bc2a
Add flytedir generic -> flytedir
Future-Outlier 4b0f008
merge async type engine
Future-Outlier afd4344
support enum
Future-Outlier 773a3b6
update
Future-Outlier 7afbb00
add tests for input from flyte console
Future-Outlier 0258853
Add Tests for dataclass in BaseModel and pydantic.dataclass in BaseModel
Future-Outlier f0fa0b9
update
Future-Outlier f4e7581
Merge branch 'master' into pydantic-plugin-v2-with-msgpack
Future-Outlier dac2da4
update thomas's advice
Future-Outlier 3531201
change tree file structure
Future-Outlier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,10 @@ def is_pydantic_basemodel(python_type: typing.Type) -> bool: | |
return False | ||
else: | ||
try: | ||
from pydantic.v1 import BaseModel | ||
from pydantic import BaseModel as BaseModelV2 | ||
from pydantic.v1 import BaseModel as BaseModelV1 | ||
|
||
return issubclass(python_type, BaseModelV1) or issubclass(python_type, BaseModelV2) | ||
Comment on lines
+39
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for backward compatible |
||
except ImportError: | ||
from pydantic import BaseModel | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,13 +10,14 @@ | |
:template: file_types.rst | ||
|
||
FlyteDirectory | ||
FlyteDirToMultipartBlobTransformer | ||
TensorboardLogs | ||
TFRecordsDirectory | ||
""" | ||
|
||
import typing | ||
|
||
from .types import FlyteDirectory | ||
from .types import FlyteDirectory, FlyteDirToMultipartBlobTransformer | ||
Comment on lines
+13
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to import |
||
|
||
# The following section provides some predefined aliases for commonly used FlyteDirectory formats. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# TMP |
Empty file.
25 changes: 25 additions & 0 deletions
25
plugins/flytekit-pydantic-v2/flytekitplugins/pydantic/v2/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from flytekit.types.directory import FlyteDirectory | ||
from flytekit.types.file import FlyteFile | ||
from flytekit.types.schema import FlyteSchema | ||
from flytekit.types.structured import StructuredDataset | ||
|
||
from . import transformer | ||
from .custom import ( | ||
deserialize_flyte_dir, | ||
deserialize_flyte_file, | ||
deserialize_flyte_schema, | ||
deserialize_structured_dataset, | ||
serialize_flyte_dir, | ||
serialize_flyte_file, | ||
serialize_flyte_schema, | ||
serialize_structured_dataset, | ||
) | ||
|
||
setattr(FlyteFile, "serialize_flyte_file", serialize_flyte_file) | ||
setattr(FlyteFile, "deserialize_flyte_file", deserialize_flyte_file) | ||
setattr(FlyteDirectory, "serialize_flyte_dir", serialize_flyte_dir) | ||
setattr(FlyteDirectory, "deserialize_flyte_dir", deserialize_flyte_dir) | ||
setattr(FlyteSchema, "serialize_flyte_schema", serialize_flyte_schema) | ||
setattr(FlyteSchema, "deserialize_flyte_schema", deserialize_flyte_schema) | ||
setattr(StructuredDataset, "serialize_structured_dataset", serialize_structured_dataset) | ||
setattr(StructuredDataset, "deserialize_structured_dataset", deserialize_structured_dataset) |
130 changes: 130 additions & 0 deletions
130
plugins/flytekit-pydantic-v2/flytekitplugins/pydantic/v2/custom.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from typing import Dict | ||
|
||
from flytekit.core.context_manager import FlyteContextManager | ||
from flytekit.models.core import types as _core_types | ||
from flytekit.models.literals import Blob, BlobMetadata, Literal, Scalar, Schema | ||
from flytekit.types.directory import FlyteDirectory, FlyteDirToMultipartBlobTransformer | ||
from flytekit.types.file import FlyteFile, FlyteFilePathTransformer | ||
from flytekit.types.schema import FlyteSchema, FlyteSchemaTransformer | ||
from flytekit.types.structured import ( | ||
StructuredDataset, | ||
StructuredDatasetMetadata, | ||
StructuredDatasetTransformerEngine, | ||
StructuredDatasetType, | ||
) | ||
from pydantic import model_serializer, model_validator | ||
|
||
|
||
@model_serializer | ||
def serialize_flyte_file(self) -> Dict[str, str]: | ||
lv = FlyteFilePathTransformer().to_literal(FlyteContextManager.current_context(), self, type(self), None) | ||
return {"path": lv.scalar.blob.uri} | ||
|
||
|
||
@model_validator(mode="after") | ||
def deserialize_flyte_file(self) -> FlyteFile: | ||
pv = FlyteFilePathTransformer().to_python_value( | ||
FlyteContextManager.current_context(), | ||
Literal( | ||
scalar=Scalar( | ||
blob=Blob( | ||
metadata=BlobMetadata( | ||
type=_core_types.BlobType( | ||
format="", dimensionality=_core_types.BlobType.BlobDimensionality.SINGLE | ||
) | ||
), | ||
uri=self.path, | ||
) | ||
) | ||
), | ||
type(self), | ||
) | ||
pv._remote_path = None | ||
return pv | ||
|
||
|
||
@model_serializer | ||
def serialize_flyte_dir(self) -> Dict[str, str]: | ||
lv = FlyteDirToMultipartBlobTransformer().to_literal(FlyteContextManager.current_context(), self, type(self), None) | ||
return {"path": lv.scalar.blob.uri} | ||
|
||
|
||
@model_validator(mode="after") | ||
def deserialize_flyte_dir(self) -> FlyteDirectory: | ||
pv = FlyteDirToMultipartBlobTransformer().to_python_value( | ||
FlyteContextManager.current_context(), | ||
Literal( | ||
scalar=Scalar( | ||
blob=Blob( | ||
metadata=BlobMetadata( | ||
type=_core_types.BlobType( | ||
format="", dimensionality=_core_types.BlobType.BlobDimensionality.MULTIPART | ||
) | ||
), | ||
uri=self.path, | ||
) | ||
) | ||
), | ||
type(self), | ||
) | ||
pv._remote_directory = None | ||
return pv | ||
|
||
|
||
@model_serializer | ||
def serialize_flyte_schema(self) -> Dict[str, str]: | ||
FlyteSchemaTransformer().to_literal(FlyteContextManager.current_context(), self, type(self), None) | ||
return {"remote_path": self.remote_path} | ||
|
||
|
||
@model_validator(mode="after") | ||
def deserialize_flyte_schema(self) -> FlyteSchema: | ||
# If we call the method to_python_value, FlyteSchemaTransformer will overwrite the local_path, | ||
# which will lose our data. | ||
# If this data is from an existed FlyteSchema, local path will be None. | ||
|
||
if hasattr(self, "_local_path"): | ||
return self | ||
|
||
t = FlyteSchemaTransformer() | ||
return t.to_python_value( | ||
FlyteContextManager.current_context(), | ||
Literal(scalar=Scalar(schema=Schema(self.remote_path, t._get_schema_type(type(self))))), | ||
type(self), | ||
) | ||
|
||
|
||
@model_serializer | ||
def serialize_structured_dataset(self) -> Dict[str, str]: | ||
lv = StructuredDatasetTransformerEngine().to_literal(FlyteContextManager.current_context(), self, type(self), None) | ||
sd = StructuredDataset(uri=lv.scalar.structured_dataset.uri) | ||
sd.file_format = lv.scalar.structured_dataset.metadata.structured_dataset_type.format | ||
return { | ||
"uri": sd.uri, | ||
"file_format": sd.file_format, | ||
} | ||
|
||
|
||
@model_validator(mode="after") | ||
def deserialize_structured_dataset(self) -> StructuredDataset: | ||
# If we call the method to_python_value, StructuredDatasetTransformerEngine will overwrite the 'dataframe', | ||
# which will lose our data. | ||
# If this data is from an existed StructuredDataset, dataframe will be None. | ||
|
||
if hasattr(self, "dataframe"): | ||
return self | ||
|
||
return StructuredDatasetTransformerEngine().to_python_value( | ||
FlyteContextManager.current_context(), | ||
Literal( | ||
scalar=Scalar( | ||
structured_dataset=StructuredDataset( | ||
metadata=StructuredDatasetMetadata( | ||
structured_dataset_type=StructuredDatasetType(format=self.file_format) | ||
), | ||
uri=self.uri, | ||
) | ||
) | ||
), | ||
type(self), | ||
) |
73 changes: 73 additions & 0 deletions
73
plugins/flytekit-pydantic-v2/flytekitplugins/pydantic/v2/transformer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import json | ||
from typing import Type | ||
|
||
import msgpack | ||
from google.protobuf import json_format as _json_format | ||
|
||
from flytekit import FlyteContext | ||
from flytekit.core.constants import MESSAGEPACK | ||
from flytekit.core.type_engine import TypeEngine, TypeTransformer, TypeTransformerFailedError | ||
from flytekit.loggers import logger | ||
from flytekit.models import types | ||
from flytekit.models.literals import Binary, Literal, Scalar | ||
from flytekit.models.types import LiteralType, TypeStructure | ||
from pydantic import BaseModel | ||
|
||
|
||
class PydanticTransformer(TypeTransformer[BaseModel]): | ||
def __init__(self): | ||
super().__init__("Pydantic Transformer", BaseModel, enable_type_assertions=False) | ||
|
||
def get_literal_type(self, t: Type[BaseModel]) -> LiteralType: | ||
schema = t.model_json_schema() | ||
literal_type = {} | ||
fields = t.__annotations__.items() | ||
|
||
for name, python_type in fields: | ||
try: | ||
literal_type[name] = TypeEngine.to_literal_type(python_type) | ||
except Exception as e: | ||
logger.warning( | ||
"Field {} of type {} cannot be converted to a literal type. Error: {}".format(name, python_type, e) | ||
) | ||
|
||
ts = TypeStructure(tag="", dataclass_type=literal_type) | ||
|
||
return types.LiteralType(simple=types.SimpleType.STRUCT, metadata=schema, structure=ts) | ||
|
||
def to_literal( | ||
self, | ||
ctx: FlyteContext, | ||
python_val: BaseModel, | ||
python_type: Type[BaseModel], | ||
expected: types.LiteralType, | ||
) -> Literal: | ||
dict_obj = python_val.model_dump() | ||
msgpack_bytes = msgpack.dumps(dict_obj) | ||
return Literal(scalar=Scalar(binary=Binary(value=msgpack_bytes, tag="msgpack"))) | ||
|
||
def from_binary_idl(self, binary_idl_object: Binary, expected_python_type: Type[BaseModel]) -> BaseModel: | ||
if binary_idl_object.tag == MESSAGEPACK: | ||
dict_obj = msgpack.loads(binary_idl_object.value) | ||
python_val = expected_python_type.model_validate(obj=dict_obj, strict=False) | ||
return python_val | ||
else: | ||
raise TypeTransformerFailedError(f"Unsupported binary format: `{binary_idl_object.tag}`") | ||
|
||
def to_python_value(self, ctx: FlyteContext, lv: Literal, expected_python_type: Type[BaseModel]) -> BaseModel: | ||
""" | ||
There will have 2 kinds of literal values: | ||
1. protobuf Struct (From Flyte Console) | ||
2. binary scalar (Others) | ||
Hence we have to handle 2 kinds of cases. | ||
""" | ||
if lv and lv.scalar and lv.scalar.binary is not None: | ||
return self.from_binary_idl(lv.scalar.binary, expected_python_type) # type: ignore | ||
|
||
json_str = _json_format.MessageToJson(lv.scalar.generic) | ||
dict_obj = json.loads(json_str) | ||
python_val = expected_python_type.model_validate(obj=dict_obj, strict=False) | ||
return python_val | ||
|
||
|
||
TypeEngine.register(PydanticTransformer()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from setuptools import setup | ||
|
||
PLUGIN_NAME = "pydantic" | ||
|
||
microlib_name = f"flytekitplugins-{PLUGIN_NAME}-v2" | ||
|
||
plugin_requires = ["flytekit>1.13.7", "pydantic>=2.9.2"] | ||
|
||
__version__ = "0.0.0+develop" | ||
|
||
setup( | ||
name=microlib_name, | ||
version=__version__, | ||
author="flyteorg", | ||
author_email="[email protected]", | ||
description="Plugin adding type support for Pydantic models", | ||
url="https://github.com/flyteorg/flytekit/tree/master/plugins/flytekitplugins-pydantic-v2", | ||
long_description=open("README.md").read(), | ||
long_description_content_type="text/markdown", | ||
namespace_packages=["flytekitplugins"], | ||
packages=[f"flytekitplugins.{PLUGIN_NAME}.v2"], | ||
install_requires=plugin_requires, | ||
license="apache2", | ||
python_requires=">=3.9", | ||
classifiers=[ | ||
"Intended Audience :: Science/Research", | ||
"Intended Audience :: Developers", | ||
"License :: OSI Approved :: Apache Software License", | ||
"Programming Language :: Python :: 3.9", | ||
"Programming Language :: Python :: 3.10", | ||
"Programming Language :: Python :: 3.11", | ||
"Programming Language :: Python :: 3.12", | ||
"Topic :: Scientific/Engineering", | ||
"Topic :: Scientific/Engineering :: Artificial Intelligence", | ||
"Topic :: Software Development", | ||
"Topic :: Software Development :: Libraries", | ||
"Topic :: Software Development :: Libraries :: Python Modules", | ||
], | ||
entry_points={"flytekit.plugins": [f"{PLUGIN_NAME}.v2=flytekitplugins.{PLUGIN_NAME}.v2"]}, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for cases when you input from the flyte console, and you use attribute access directly, you have to the
float
toint
.Since javascript has only
number
, it can't tell the difference between int and float, and when goland (propeller) doing attribute access, it doesn't have the expected python typeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the javascript issue and the attribute access issue are orthogonal right?
this should only be a javascript problem. attribute access should work since msgpack preserves float/int even in attribute access correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YES, the attribute access works well, it's because javascript pass float to golang, and golang pass float to python.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but when you are accessing a simple type, you have to change the behavior of
SimpleTransformer
.For Pydantic Transformer, we will use
strict=False
as argument to convert it to right type.