Enforce input validation on Parameter Tasks and add descriptions
See original GitHub issueCurrent behavior
Parameters
are pretty simple entities that look for a key and take whatever value is presented at that key, to then forward off to the Tasks that consume it.
This makes troubleshooting invalid values passed into Parameters tricky, as the failure will happen at the downstream Task(s), rather than the Parameter. Which is essentially wasting compute time, because if the Parameter was bad, there’s no chance the downstream tasks can succeed.
But to enforce input validation, we’d need to also have some basic documentation/description of what the provided Parameter
is.
Proposed behavior
starlette has a slick wrapper for the way it handles environment configuration, where you can specify a cast
type. The code for this is pretty small.
Taking that a step further, I’ve gotten spoiled by the way FastAPI leverages the pydantic library by using python type annotations to handle input validation of input parameters to endpoints, and think this would be a great feature to extend into the way Prefect takes in Parameters
.
Where you’d be able to optionally specify an object type for the Parameter, and if set, when the Parameter Task is ran, it would perform basic datatype validation on what was passed in. If someone passed a datatype other than what that Parameter was expecting, it’d fail at that step before trying to run any downstream Tasks that consume the Parameter.
Example
During Parameter
initialization, it can evaluate the value passed into the Parameter and fail at that point if it is not the expected input type.
with Flow("example") as flow:
# basic types
int_param = Parameter("int_param", default=100, cast=int, description="how many to pull")
str_param = Parameter("str_param", default="string", cast=str, description="a string to search")
# class types
from enum import Enum
class StrictValues(str, Enum):
VALUE = 'one'
EXAMPLE = 'two'
enum_param = Parameter("enum_param", default=StrictValues.VALUE, cast=StrictValues, description="which value do you want?")
...
Or we could go all in with overloading pydantic.BaseModel to have a single validators (but this would break backwards compatibility):
class MyCustomParameters(pydantic.BaseModel):
int_param: int = pydantic.Field(
default=100,
description='how many to pull'
)
str_param: str = pydantic.Field(
default="string",
description='how many to pull'
)
enum_param: StrictValues = pydantic.Field(
default=StrictValues.VALUE,
description='which value do you want?'
)
with Flow(
name="example",
parameters=MyCustomParameters
) as flow:
...
pydantic raises a specific exception with details on what input validation failed, which can aid in troubleshooting bad input parameters to the Parameter Tasks.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:8 (6 by maintainers)
Hi @Julian-Brendel, good news: we have already implemented Parameter type validation in what will eventually become Prefect 2.0
Complete with 2.0.