You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar issue and didn't find it.
I searched the Prefect documentation for this issue.
I checked that this issue is related to Prefect and not one of its dependencies.
Bug summary
When running a deployment if the flow function has Pydantic Type hinted inputs and no parameters are specified on the deployment default values specified in the function are not coerced into the Pydantic model specified in the function definition.
@flow(name="test_flow")deftest_flow(
input: SampleContract= {"field_1": ["val1", "val2"], "field_2": [1, 2]}
) ->SampleContract2:
logger=get_run_logger()
logger.info(f"Data at Start of Flow: {input}")
output=add_fruits(input)
logger.info(f"Data at End of Flow: {output}")
returnSampleContract2.model_validate(output)
The above code completes successfully when testing locally as the default input is coerced into the model and deployment runs complete successfully when input parameters are specified on the deployment since we are validating those inputs prior to executing the flow run. However running this deployment on a schedule or as a quick run fails since the inputs are not coerced into the Pyadntic model.
It makes sense to set a default that corresponds to the appropriate type like this and in general is something we should encourage
@flow(name="test_flow")deftest_flow(
input: SampleContract=SampleContract(field_1= ["val1", "val2"], field_2= [1, 2])
) ->SampleContract2:
logger=get_run_logger()
logger.info(f"Data at Start of Flow: {input}")
output=add_fruits(input)
logger.info(f"Data at End of Flow: {output}")
returnSampleContract2.model_validate(output)
However this validation step is being performed when running the flow locally and when passing parameters to the deployment at runtime so the experience could be jarring for an end user expecting that validation step to function the same way when deploying the flow without specifying parameters on the deployment and instead relying on the default values specified on the function.
Reproduction
frompydanticimportBaseModelfrompydantic.typesimportListfromprefectimportflow, task, get_run_loggerclassSampleContract(BaseModel):
field_1: List[str]
field_2: List[int]
classSampleContract2(BaseModel):
fruit: List[str]
@taskdefadd_fruits(input: SampleContract) ->SampleContract2:
output=input.model_dump()
output["fruit"] = ["apple", "banana"]
returnoutput@flow(name="test_flow")deftest_flow(
input: SampleContract= {"field_1": ["val1", "val2"], "field_2": [1, 2]}
) ->SampleContract2:
logger=get_run_logger()
logger.info(f"Data at Start of Flow: {input}")
output=add_fruits(input)
logger.info(f"Data at End of Flow: {output}")
returnSampleContract2.model_validate(output)
Error
# Error from a scheduled runEncounteredexceptionduringexecution:
Traceback (mostrecentcalllast):
File"/usr/local/lib/python3.12/site-packages/prefect/engine.py", line2101, inorchestrate_task_runresult=awaitcall.aresult()
^^^^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line327, inaresultreturnawaitasyncio.wrap_future(self.future)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/usr/local/lib/python3.12/site-packages/prefect/_internal/concurrency/calls.py", line352, in_run_syncresult=self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/opt/prefect/mm2-sanbox-main/flows/schema_validation.py", line14, inadd_fruitsoutput=input.model_dump()
^^^^^^^^^^^^^^^^AttributeError: 'dict'objecthasnoattribute'model_dump'01:50:03PMadd_fruits-0prefect.task_runsFinishedinstateFailed("Task run encountered an exception AttributeError: 'dict' object has no attribute 'model_dump'")
Example deployment yaml for deploying the reproduction code, specifically the parameters field on the deployment should be left blank
name: mm2-sanboxprefect-version: 2.18.1build:
- prefect_docker.deployments.steps.build_docker_image:
id: build_imagerequires: prefect-docker>=0.3.1image_name: some_docker_repo_nametag: schemavalidationdockerfile: ./Dockerfile# push section allows you to manage if and how this project is uploaded to remote locationspush:
- prefect_docker.deployments.steps.push_docker_image:
requires: prefect-docker>=0.3.1image_name: '{{ build_image.image_name }}'tag: '{{ build_image.tag }}'# pull section allows you to provide instructions for cloning this project in remote locationspull:
- prefect.deployments.steps.git_clone:
repository: https://github.com/masonmenges/mm2-sanbox.gitbranch: mainaccess_token: null# the deployments section allows you to provide configuration for deploying flowsdeployments:
- name: schema_validation_failversion: nulltags: []description: nullschedule: {}flow_name: nullentrypoint: flows/schema_validation.py:test_flowparameters: {}work_pool:
name: k8s-minikube-testwork_queue_name: nulljob_variables:
image: '{{ build_image.image }}'
The text was updated successfully, but these errors were encountered:
First check
Bug summary
When running a deployment if the flow function has Pydantic Type hinted inputs and no parameters are specified on the deployment default values specified in the function are not coerced into the Pydantic model specified in the function definition.
The above code completes successfully when testing locally as the default input is coerced into the model and deployment runs complete successfully when input parameters are specified on the deployment since we are validating those inputs prior to executing the flow run. However running this deployment on a schedule or as a quick run fails since the inputs are not coerced into the Pyadntic model.
It makes sense to set a default that corresponds to the appropriate type like this and in general is something we should encourage
However this validation step is being performed when running the flow locally and when passing parameters to the deployment at runtime so the experience could be jarring for an end user expecting that validation step to function the same way when deploying the flow without specifying parameters on the deployment and instead relying on the default values specified on the function.
Reproduction
Error
Versions
Additional context
Example deployment yaml for deploying the reproduction code, specifically the parameters field on the deployment should be left blank
The text was updated successfully, but these errors were encountered: