<@ULVA73B9P> How do I specify mandatory parameters...
# ask-marvin
j
@Marvin How do I specify mandatory parameters for a flow?
m
It appears that flows in Prefect support parameterization, allowing you to define parameters that can be passed to the subflow to run tasks under different use cases. However, the search does not reveal details on making parameters mandatory directly. To ensure mandatory parameters for a flow, you typically define parameters and then validate or use them within your flow in a way that the flow will fail if a required parameter is not provided. For example, you can raise an exception if a parameter is not supplied or does not meet certain criteria. For more detailed guidance on how to use parameters in Prefect flows, you can refer to the Prefect documentation on parameters in flows. The documentation explains first-class support for parameterization and may provide examples or methods to handle required parameters.
j
@Marvin If I do not specify a default value, the UI seems to render the parameter as optional rather than mandatory.
m
It appears there are no open issues related to your query about mandatory parameters and the Prefect UI in the GitHub repository for Prefect UI. The fact that parameters without default values are rendered as optional in the UI might be the intended behavior, or it could be that this particular issue or feature request has not yet been raised or documented within the Prefect UI GitHub issues. If you believe this behavior is not as expected, you might consider creating a new issue in the Prefect UI GitHub repository to report this or to ask for a feature that allows distinguishing mandatory parameters from optional ones in the UI. It's also possible that the UI is designed to encourage providing defaults for better user experience, or there are other mechanisms in the Prefect Core library to enforce required parameters that do not reflect in the UI. In the meantime, you could handle this programmatically by raising an exception if a necessary parameter is not provided when the flow is executed. As of now, there does not appear to be a feature for marking parameters as mandatory in the UI if no default value is specified. Please check the Prefect documentation or community forums for any potential workarounds or updates on this feature.
n
in general
How do I specify mandatory parameters for a flow?
by just omitting default values like
def foo(x: str)
etc
can you show what example you have thats not working?
j
Thanks for checking. This was just a simple demo of parameterizing a deployment. The flow is defined as shown but shows up the UI as optional.
Copy code
@flow(retries=0)
def load_contacts_to_van(source_data_sql: str, van_api_secret_name: str):
    logger = get_run_logger()
    ....
n
how did you create the deployment? did you provide default parameter values when you did?
j
I did not. I created the deployment with the
prefect deploy
wizard, which created this artifact in prefect.yaml:
Copy code
- name: load to van demo
  version:
  tags: []
  description:
  entrypoint: organizing\load-van.py:load_contacts_to_van
  parameters: {}
  work_pool:
    name: legacy
    work_queue_name:
    job_variables: {}
  schedules: []
n
hmm interesting - thanks. will try and reproduce
weird - cannot yet repro. can you share output from
prefect deployment inspect your-flow/your-deployment
?
j
This is the parameter part. I DM'ed the whole thing.
Copy code
'parameter_openapi_schema': {
        'type': 'object',
        'title': 'Parameters',
        'required': ['source_data_sql', 'van_api_secret_name'],
        'properties': {'source_data_sql': {'type': 'string', 'title': 'source_data_sql', 'position': 0}, 'van_api_secret_name': {'type': 'string', 'title': 'van_api_secret_name', 'position': 1}}
    },
n
hey @Craig Harshbarger - have you seen this before? tl;dr • prefect cloud • OpenAPI schema above (from
load_contacts_to_van(source_data_sql: str, van_api_secret_name: str)
) • form has
(Optional)
hanging around for those fields
c
The screenshot appears to be from editing a deployment. Which will mark everything as required because you do not have to provide parameters when defining a deployment. But if you go to create a run for that deployment it will require that those values exist. I believe that is the intended behavior for creating/editing a deployment. But I could be wrong
j
Thanks @Craig Harshbarger. I can confirm that the parameters are required in order to run the deployment. In this case, the UI on the Edit screen is annotating the parameters as Optional (maybe just a typo where you said everything would be required?). Everything works as it should wrt to the parameters; I was confused by the UI saying they were Optional, which is what started the thread. In any case, it's a minor thing but was confusing as it worked out.
c
I totally understand why that would be confusing. Maybe we can do something to help with that. On the edit page it’s really saying that you don’t have to enter a value for any of the parameters. Because they’re not required to make a deployment. So that Optional is in the context of that form. Not specific to the parameters schema itself.
👍 1