Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Python loads Pydantic 1, which has no model_validator

Your error

ImportError: cannot import name 'model_validator' from 'pydantic'1 more line it can show

Fix it fast

Most common cause, 1 of 3

A requirements file or lockfile pins Pydantic 1.x

Raise the pin so it allows Pydantic 2, then upgrade through python -m pip, which installs into the interpreter you just checked rather than whatever pip is first on PATH.

Fix
# requirements.txt
pydantic>=2,<3

python -m pip install -U "pydantic>=2,<3"
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: cannot import name 'model_validator' from 'pydantic' means the interpreter running your code loads Pydantic 1.x, and model_validator only exists from Pydantic 2. Print pydantic.VERSION and pydantic.__file__, then install pydantic>=2 into that same environment, or use a venv when the path is Ubuntu's /usr/lib/python3/dist-packages. Any 2.x release up to 2.14 has it.

  • Applies to Pydantic 2.14.4, Pydantic 1.10.x, Python 3.15.2, Ubuntu 22.04, 24.04 and 26.04 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Start this with the exact interpreter that fails: the venv's python, the one your IDE uses, or the one in the container. It shows which Python runs and which Pydantic it imports, from which folder. A 1.x version inside your own environment means cause 1 or 2. A path under /usr/lib/python3/dist-packages means cause 3.

Run this first
python -c "import sys, pydantic; print(sys.executable); print(pydantic.VERSION, pydantic.__file__)"

Then find the line you see in this list and open its fix:

  1. 1A requirements file or lockfile pins Pydantic 1.xVERSION prints 1.x from your project's environment, and requirements.txt, pyproject.toml or a lockfile holds pydantic==1.x or pydantic<2
  2. 2Another package in the environment requires pydantic<2After you raise the pin, pip install -r requirements.txt stops with ResolutionImpossible, and the lines under 'The conflict is caused by:' name a package that depends on pydantic<2
  3. 3The code runs on Ubuntu's system Python with apt's python3-pydanticsys.executable is /usr/bin/python3 and pydantic.__file__ is under /usr/lib/python3/dist-packages

What does the error look like?

  • ImportError: cannot import name 'model_validator' from 'pydantic'
  • ImportError: cannot import name 'field_validator' from 'pydantic'

Cause 1 of 3

A requirements file or lockfile pins Pydantic 1.x

You seeVERSION prints 1.x from your project's environment, and requirements.txt, pyproject.toml or a lockfile holds pydantic==1.x or pydantic<2

Check

The import fails because model_validator, field_validator and the other V2 decorators are missing from the 1.x release your pin installs. Pydantic 1 offered root_validator and validator instead, and the migration guide names model_validator as the replacement for root_validator. Each fresh install from an old pin restores the 1.x release.

Check
grep -rniE "pydantic *(==|<|~=)" requirements*.txt pyproject.toml

Fix

Raise the pin so it allows Pydantic 2, then upgrade through python -m pip, which installs into the interpreter you just checked rather than whatever pip is first on PATH.

Fix
# requirements.txt
pydantic>=2,<3

python -m pip install -U "pydantic>=2,<3"

Verify

The first-step command now reports a 2.x version, and the import runs.

Verify
python -c "from pydantic import model_validator; print('ok')"

Confirmed by pydantic.dev

Did this fix it?

Cause 2 of 3

Another package in the environment requires pydantic<2

You seeAfter you raise the pin, pip install -r requirements.txt stops with ResolutionImpossible, and the lines under 'The conflict is caused by:' name a package that depends on pydantic<2

Check

Here the old release is held in place by a dependency. pip must satisfy every requirement at once, so a package that caps Pydantic below 2 blocks the upgrade. pip's guide explains how to read the conflict lines to find which package sets the cap.

Check
python -m pip install -r requirements.txt

Fix

Upgrade the package named in the conflict to a release that accepts Pydantic 2, using its changelog to find one. If none exists, the pip guide's options apply: ask its maintainers to loosen the cap, or replace the package.

Fix
python -m pip install -U <package-from-the-conflict>
python -m pip install -r requirements.txt

Verify

pip completes the install with no conflict report, and pydantic.VERSION shows 2.x.

Verify
python -c "import pydantic; print(pydantic.VERSION)"

Confirmed by pip.pypa.io

Did this fix it?

Cause 3 of 3

The code runs on Ubuntu's system Python with apt's python3-pydantic

You seesys.executable is /usr/bin/python3 and pydantic.__file__ is under /usr/lib/python3/dist-packages

Check

Ubuntu packages Pydantic as python3-pydantic: 1.8.2 on 22.04 LTS and 1.10.14 on 24.04 LTS, with 2.12.5 arriving only in 26.04 LTS. When a script starts with /usr/bin/python3 on the two older releases and no newer copy sits earlier on the path, it imports that 1.x package.

Check
/usr/bin/python3 -c "import pydantic; print(pydantic.VERSION, pydantic.__file__)"

Fix

Leave the apt package as it is. Give the project its own virtual environment, install Pydantic 2 there, and start the code with that environment's interpreter. By default a venv is isolated from the packages installed in the operating system, so the apt copy is not seen.

Fix
python3 -m venv .venv
.venv/bin/python -m pip install "pydantic>=2,<3"
.venv/bin/python main.py

Verify

The first-step command, run with .venv/bin/python, prints 2.x and a path inside .venv.

Verify
.venv/bin/python -c "import pydantic; print(pydantic.VERSION, pydantic.__file__)"

Confirmed by docs.python.org

Did this fix it?

How do I stop it coming back?

  • Declare pydantic>=2,<3 in pyproject.toml or requirements.txt, so no lockfile can quietly reinstall 1.x.
  • Fail fast at start-up with assert int(pydantic.VERSION.split('.')[0]) >= 2, which gives a clear message instead of an import error deep in a module.
  • Run services from a project venv or a container image, not from /usr/bin/python3 on a server.
  • Set your editor's interpreter to the project venv so editor runs and terminal runs import the same Pydantic.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.