<@ULVA73B9P> When I create the parameters for flow...
# ask-marvin
j
@Marvin When I create the parameters for flow using a pydantic base model, can I specify a default value for a parameter that is non-optional?
m
Yes, you can specify a default value for a non-optional parameter in a Pydantic BaseModel when using it as a parameter for a Prefect flow. The Prefect documentation indicates that you can set default values for fields in your model using the
with_initial_data
method, which is useful for providing default values for fields in your own
RunInput
class. Here's a concise example of how you might define such a model:
Copy code
python
from pydantic import BaseModel
from prefect import flow

class MyParameters(BaseModel):
    name: str = "default_name"

@flow
def my_flow(params: MyParameters):
    print(params.name)
In this example, the
name
field has a default value of
"default_name"
, and you can still pass a different value when initializing
MyParameters
. This approach aligns with your preference for an imperative programming style. For more details, you can explore the relevant Prefect documentation here.
j
@Marvin I would like to make use of the Field() class from pydantic to create a description. Can I maintain the same behaviour as above in that case?
m
Yes, you can use the
Field()
class from Pydantic to add descriptions to your flow parameters while maintaining the same behavior of default values. Prefect supports Pydantic models as type hints for flow parameters, allowing you to leverage Pydantic's features like default values and descriptions. Here's how you might implement it:
Copy code
python
from pydantic import BaseModel, Field
from prefect import flow

class MyParameters(BaseModel):
    name: str = Field(default="default_name", description="The name of the user")

@flow
def my_flow(params: MyParameters):
    print(params.name)

# Running the flow
my_flow()
In this example, the
name
field is given a default value of
"default_name"
and a description
"The name of the user"
. This setup is compatible with Prefect's orchestration engine, which will handle the Pydantic model appropriately. For more details, you can explore the Prefect documentation on writing flows.
j
@Marvin If I specify a default value inside my Field(), the word "Optional" appears next to that parameter on the Prefect UI. Can I change this so it doesn't appear?
m
It seems that the appearance of the word "Optional" in the Prefect UI when you specify a default value using Pydantic's
Field()
is a known behavior. This is because when a default value is provided, Pydantic treats the field as optional, even if logically you might consider it mandatory with a default. Currently, there is no direct way to change this behavior in the Prefect UI. The UI reflects the underlying Pydantic model's interpretation, where fields with default values are considered optional. For more context, you can check related discussions and issues in the Prefect GitHub repository, such as the issue about default parameter handling. If this behavior is problematic for your use case, you might consider contributing to the discussion or following the issue for updates.