Pydantic v2 error in flow running Prefect 2.14.11

Im running a flow in Prefect cloud with a worker running in Azure kubernetes services. A kubernetes job spins up but fails immediately with the following error:

File "/opt/prefect/leaporchestration-feature/2515-refactor-deployments/flows/config/", line 1, in <module>
    from .model.config import RunConfig
  File "/opt/prefect/leaporchestration-feature/2515-refactor-deployments/flows/config/model/", line 4, in <module>
    from pydantic import BaseModel, parse_file_as
  File "/usr/local/lib/python3.10/site-packages/pydantic/", line 363, in __getattr__
    return _getattr_migration(attr_name)
  File "/usr/local/lib/python3.10/site-packages/pydantic/", line 302, in wrapper
    raise PydanticImportError(f'`{import_path}` has been removed in V2.')
pydantic.errors.PydanticImportError: `pydantic:parse_file_as` has been removed in V2.

For further information visit

When I run pip install -r requirements-dev.txt locally and then pip list |grep pydantic I can see that Pydantic 1.10.13 is installed in my venv.

Im using image for the job in kubernetes.

It looks like Pydantic V2 is used in kubernetes!? How do I resolve this?

We use a json file as a configuration file for our flows and we load that file into a Pydantic model called RunConfig. The function parse_file_as is used but it is removed in Pydantic V2. However I cannot find the documentation describing what function to use instead.

I can confirm that the image I’m using (prefecthq/prefect:2.14-python3.10-kubernetes) has Pydantic 2.5 installed.

Wouldn’t this render this image unusable for Prefect since it relies on Pydantic 1?

If I upgrade Pydantic locally to 2.5 I get the following from pip:

ERROR: Cannot install prefect-shell and pydantic==2.5.2 because these package versions have conflicting dependencies.

The conflict is caused by:
    The user requested pydantic==2.5.2
    prefect 2.13.6 depends on pydantic!=2.0.0, !=2.0.1, !=2.1.0, <2.0.0, <3.0.0 and >=1.10.0
    The user requested pydantic==2.5.2
    prefect 2.13.5 depends on pydantic!=2.0.0, !=2.0.1, !=2.1.0, <2.0.0, <3.0.0 and >=1.10.0

I tried using a custom image based on the above prefect image where I add requirements.txt with pydantic>=1.10.0,<2.0.0 and run pip install -r requirements.txt. The result when running a flow is the same as in my original post. My guess is that pip installs as root in its home directory and whatever user that is running the container in k8 still has pydantic 2.5!?

Deploying a temp k8 job in AKS with the command pip list tells me that pydantic 1.10.13 is installed on the image. So why does Prefect still use Pydantic V2??

hi @martinlarsson

the simplest way to resolve this is to write a Dockerfile that installs the dependencies you need. Its generally not recommended to pull latest if you need specific deps.

Wouldn’t this render this image unusable for Prefect since it relies on Pydantic 1?

No, because we do conditional imports to use pydantic v1 objects (even when pydantic 2 is installed).

If you’re getting unexpected deps on your remote infra, it could be because your deployment is relying on EXTRA_PIP_PACKAGES (e.g. prefect-azure) that would install a different version of deps (like pydantic) than you expect

Thanks @nate!

Im not using any EXTRA_PIP_PACKAGES. I did do a test with a vanilla Prefect image as described above and it had Pydantic 2.5 installed. So I think you need to look at your images.

I managed to solve my issue with reverting to json package to read our config file and then converting it to Pydantic model like so:

with as file:
  json_data = json.load(file)

model_instance = RunConfig(**json_data)

Now I have the next issue! I’m deploying my flow like this:

flow.from_source(  # type: ignore

How do I manage the dependencies?? I tried running this function inside my flow:

from prefect.deployments.steps.utility import pip_install_requirements
await pip_install_requirements(stream_output=True)

But I get ModuleNotFoundError("No module named 'prefect_databricks'") even before the flow is started.

I solved both issues by building a custom image specifying pydantic and prefect-databricks.