shani
10/20/2024, 6:43 AMclass MyClass:
@flow
def my_instance_method(self):
passNate
10/20/2024, 5:00 PMNate
10/20/2024, 5:01 PMMarvin
10/20/2024, 5:01 PMNate
10/20/2024, 5:01 PMMarvin
10/20/2024, 5:01 PMBaseModel subclass as a parameter.
In this example, I'll demonstrate how to use a Pydantic model as a parameter for a flow. The model comprises of a simple structure with some validation rules.
python
from prefect import flow
from pydantic import BaseModel, ValidationError
# Define a Pydantic model
class ComplexModel(BaseModel):
a: int
b: str
# Define a flow that accepts a ComplexModel
@flow
def flow_with_complex_model_param(model: ComplexModel):
print(f"Received model: {model}")
if __name__ == "__main__":
# Valid input example
valid_model_data = {"a": 42, "b": "hello"}
try:
flow_with_complex_model_param(model=ComplexModel(**valid_model_data))
except ValidationError as e:
print(f"Validation Error: {e}")
# Invalid input example
invalid_model_data = {"a": "not_an_int", "b": "world"}
try:
flow_with_complex_model_param(model=ComplexModel(**invalid_model_data))
except ValidationError as e:
print(f"Validation Error: {e}")
In this example:
- We use Pydantic to define a model ComplexModel with fields a and b.
- The flow_with_complex_model_param flow accepts a ComplexModel instance as its parameter.
- We demonstrate both valid and invalid data scenarios, showing Prefect's integration with Pydantic's validation capabilities, which will raise a ValidationError when invalid data is provided.
You can test this code by running it in your local environment configured with Prefect 3.x. The example shows how Prefect's flow parameters seamlessly integrate with Pydantic models.
For more details, you can refer to the Prefect 3.x documentation here.shani
10/20/2024, 5:06 PMNate
10/20/2024, 5:10 PMIs this just data validation?not necessarily. you said
I have a lot of context that needs to pass in my flow like the logger, the account_id, and some other parameters.so instead of the class having instance methods which invoke the flow / task, the
ComplexModel (for example) is just a clean way to organize that context you need to pass into your flow
and yes
I want the data to be accessible in every task of the flowall standard python rules apply, so you can pass your inputs anywhere in your flow that you need, including to other tasks
@flow
def f(inputs: ComplexModel):
t1(inputs, ...)
t2(inputs, ...)shani
10/20/2024, 5:26 PMNate
10/20/2024, 5:37 PMshani
10/21/2024, 2:18 PM