Steffen
02/14/2025, 2:33 PMMarvin
02/14/2025, 2:33 PMV1ValidatedFunction
or V2ValidatedFunction
to initialize the model instance. This ensures that your Pydantic models are correctly validated when the flow is called.
3. Warnings and Compatibility: Prefect suppresses certain Pydantic deprecation warnings internally, so you don't need to worry about them when passing models to flows.
For more details on how Prefect handles Pydantic models, you can refer to the flows.py implementation in the Prefect GitHub repository.
Additionally, you can explore Prefect's top-level API where functions like flow
and task
are defined, which might be useful for setting up your flow and tasks with Pydantic models. Here's a brief overview of the Prefect top-level API.
If you're using Prefect 3.x, ensure your models are compatible with the Pydantic version you are using to avoid any issues.Steffen
02/14/2025, 2:36 PMrun_deployment(<flow_name>, parameters={'model'=MyPydanticModel})
but in the function I only have a dictMarvin
02/14/2025, 2:36 PMrun_deployment
with a Pydantic model, Prefect serializes the parameters using a jsonable_encoder
, which turns the Pydantic model into a dictionary. This is why you're seeing a dictionary in your flow function.
To handle this, you can manually rehydrate the Pydantic model inside your flow function. Here's a general approach:
1. Pass the Serialized Model: When you call run_deployment
, the Pydantic model is serialized to a dictionary.
2. Rehydrate the Model: In your flow function, convert the dictionary back into a Pydantic model instance.
Here's an example:
python
from prefect import flow
from pydantic import BaseModel
# Define your Pydantic model
class MyPydanticModel(BaseModel):
field1: str
field2: int
@flow
def my_flow(model_data: dict):
# Rehydrate the Pydantic model
model = MyPydanticModel(**model_data)
# Now you can use `model` as a Pydantic model instance
print(model)
# Run the deployment
run_deployment("my_flow", parameters={"model": MyPydanticModel(field1="value", field2=42).dict()})
This method ensures that you convert the dictionary back into a Pydantic model within the flow. For more on how Prefect handles parameters, you can check the Prefect 3.x documentation on writing flows.Steffen
02/14/2025, 2:37 PMMarvin
02/14/2025, 2:37 PM