https://prefect.io logo
Title
g

Giuliano Mega

11/10/2022, 11:54 AM
Hello there! I'm having an issue with default parameters on a flow that uses pydantic validation. My flow looks something like:
class FlowParameters(BaseModel):
    requests_per_minute: int = 10
    ids: Optional[List[str]] = None


@flow(
    description='Imports and enriches Entities.',
    task_runner=SequentialTaskRunner()
)
def token_pipeline(parameters: FlowParameters):
    new_entities = fetch_new_entities(parameters.ids)
    enrich_entity.map(new_entities, parameters.requests_per_minute)
If I call my flow from Prefect Cloud and pass it some parameters (run Custom), all runs smoothly. If, on the other hand, I try to run it with the default parameters, I get:
Validation of flow parameters failed with error: 
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/prefect/engine.py", line 275, in retrieve_flow_then_begin_flow_run
    parameters = flow.validate_parameters(flow_run.parameters)
  File "/usr/local/lib/python3.10/site-packages/prefect/flows.py", line 324, in validate_parameters
    raise ParameterTypeError.from_validation_error(exc) from None
prefect.exceptions.ParameterTypeError: Flow run received invalid parameters:
 - parameters: field required
Which is weird. Expected behavior would be having the flow run with default parameters. Can't seem to understand what I'm doing wrong from reading the docs, any ideas? 🙂
1
r

Ryan Peden

11/10/2022, 1:20 PM
Hi, Giuliano! It looks like you're providing a type for your parameter, but not a default value that gets assigned at runtime. Does changing the function definition to something like this do what you are expecting?
def token_pipeline(parameters: FlowParameters = FlowParameters())
g

Giuliano Mega

11/10/2022, 2:41 PM
Oh I see. I didn't think of it because: 1. it's usually not a good idea to have mutable objects in function's defaults; 2. pydantic has its way of specifying defaults, which is by setting them in the model. Is there any reason not to have the flow decorator do this job of attempting to instantiate the parameter with no args and pass it along if not supplied? That's what I actually thought it would do.
And thanks a lot for the help! 🙂