Jackson Nguyen
02/24/2025, 7:49 PMMarvin
02/24/2025, 7:49 PMwith_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:
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.Jackson Nguyen
02/24/2025, 7:51 PMMarvin
02/24/2025, 7:51 PMField()
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:
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.Jackson Nguyen
02/24/2025, 7:54 PMMarvin
02/24/2025, 7:55 PMField()
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.