diff --git a/docs/examples/workflows/experimental/script_pydantic_io.md b/docs/examples/workflows/experimental/script_pydantic_io.md index f26f7e7c4..fbca1c029 100644 --- a/docs/examples/workflows/experimental/script_pydantic_io.md +++ b/docs/examples/workflows/experimental/script_pydantic_io.md @@ -8,6 +8,11 @@ === "Hera" ```python linenums="1" + try: + from pydantic.v1 import BaseModel + except ImportError: + from pydantic import BaseModel + from hera.shared import global_config from hera.workflows import Artifact, ArtifactLoader, Parameter, Workflow, script from hera.workflows.io import RunnerInput, RunnerOutput @@ -20,9 +25,13 @@ global_config.experimental_features["script_annotations"] = True global_config.experimental_features["script_pydantic_io"] = True + class MyObject(BaseModel): + a_dict: dict = {} + a_str: str = "a default string" class MyInput(RunnerInput): param_int: Annotated[int, Parameter(name="param-input")] = 42 + an_object: Annotated[MyObject, Parameter(name="obj-input")] = MyObject(a_dict={"my-key": "a-value"}, a_str="hello world!") artifact_int: Annotated[int, Artifact(name="artifact-input", loader=ArtifactLoader.json)] @@ -58,6 +67,8 @@ parameters: - default: '42' name: param-input + - default: '{"a_dict": {"my-key": "a-value"}, "a_str": "hello world!"}' + name: obj-input name: pydantic-io outputs: artifacts: diff --git a/examples/workflows/experimental/script-pydantic-io.yaml b/examples/workflows/experimental/script-pydantic-io.yaml index 54a6fe892..55256a985 100644 --- a/examples/workflows/experimental/script-pydantic-io.yaml +++ b/examples/workflows/experimental/script-pydantic-io.yaml @@ -11,6 +11,8 @@ spec: parameters: - default: '42' name: param-input + - default: '{"a_dict": {"my-key": "a-value"}, "a_str": "hello world!"}' + name: obj-input name: pydantic-io outputs: artifacts: diff --git a/examples/workflows/experimental/script_pydantic_io.py b/examples/workflows/experimental/script_pydantic_io.py index 79406753a..625809a3a 100644 --- a/examples/workflows/experimental/script_pydantic_io.py +++ b/examples/workflows/experimental/script_pydantic_io.py @@ -1,3 +1,8 @@ +try: + from pydantic.v1 import BaseModel +except ImportError: + from pydantic import BaseModel + from hera.shared import global_config from hera.workflows import Artifact, ArtifactLoader, Parameter, Workflow, script from hera.workflows.io import RunnerInput, RunnerOutput @@ -10,9 +15,13 @@ global_config.experimental_features["script_annotations"] = True global_config.experimental_features["script_pydantic_io"] = True +class MyObject(BaseModel): + a_dict: dict = {} + a_str: str = "a default string" class MyInput(RunnerInput): param_int: Annotated[int, Parameter(name="param-input")] = 42 + an_object: Annotated[MyObject, Parameter(name="obj-input")] = MyObject(a_dict={"my-key": "a-value"}, a_str="hello world!") artifact_int: Annotated[int, Artifact(name="artifact-input", loader=ArtifactLoader.json)] diff --git a/src/hera/workflows/io.py b/src/hera/workflows/io.py index 020c40e01..24709920f 100644 --- a/src/hera/workflows/io.py +++ b/src/hera/workflows/io.py @@ -3,6 +3,7 @@ from typing import Any, List, Union from hera.shared._pydantic import BaseModel +from hera.shared.serialization import serialize from hera.workflows.artifact import Artifact from hera.workflows.parameter import Parameter @@ -30,7 +31,8 @@ def _get_parameters(cls) -> List[Parameter]: if isinstance(get_args(annotations[field])[1], Parameter): param = get_args(annotations[field])[1] if cls.__fields__[field].default: - param.default = cls.__fields__[field].default + # Serialize the value (usually done in Parameter's validator) + param.default = serialize(cls.__fields__[field].default) parameters.append(param) else: # Create a Parameter from basic type annotations