AI and LLM integration · Step-by-step tutorial
A package written for Pydantic 1 breaks after Pydantic 2 is installed
Short answer
ImportError: cannot import name 'ModelField' from 'pydantic.fields' means code written for Pydantic 1 is running on Pydantic 2, where ModelField only survives as pydantic.v1.fields.ModelField. Find the package that makes the import, upgrade it to a release that supports Pydantic 2, or pin pydantic 1.10 in that environment. Applies to Pydantic 2.0 through 2.14.
Pydantic 2 rewrote its core. ModelField was part of the V1 internals and is no longer at pydantic.fields; the migration guide says the V1 API lives on under pydantic.v1. A package that imports the old location fails to load when Pydantic 2 is installed.
Below is a route from the traceback to a working environment: find who imports it, upgrade, and only then fall back to pinning or patching.
Before you start
- Access to the Python environment that raises the error
- pip and, ideally, a way to create a second virtual environment
- A note of which package the traceback or search points at
Step 1
Find who is importing ModelField
- Print the Pydantic version in the failing interpreter.
- Search installed packages for the import.
- Search your own source tree separately.
Locate the V1 import
python -c "import pydantic; print(pydantic.VERSION)"
grep -rln "from pydantic.fields import.*ModelField" "$(python -c 'import sysconfig; print(sysconfig.get_paths()["purelib"])')"
grep -rn "ModelField" --include=*.py .A hit in site-packages means a dependency; a hit only in your repository means your own code.
Step 2
Upgrade the dependency
Most libraries that used this class released a Pydantic 2 version. FastAPI is the best-known case: its release notes record V2 support from 0.100.0, and recent releases require a Pydantic 2 minimum. Upgrade the package the search named, in the same environment, and keep Pydantic on 2.x.
Upgrade and bound the version
python -m pip install -U <package>
python -m pip install -U "pydantic>=2,<3"Step 3
If no release supports Pydantic 2
Check what the newest release of the package requires. If its metadata still asks for pydantic<2, the supported route is the last V1 line, installed in an environment of its own so other packages are not forced down with it. Current FastAPI, for instance, refuses Pydantic 1.
Inspect the requirement, then isolate
python -c "from importlib.metadata import requires; print([r for r in requires('<package>') if 'pydantic' in r])"
python -m venv .venv-legacy
.venv-legacy/bin/pip install "pydantic==1.*" <package>Step 4
If the import is in your own code
Pydantic 2 keeps the whole V1 API under pydantic.v1, and Pydantic 1.10.17 and later also provide that namespace. A guarded import therefore runs on both while you migrate. The real migration replaces __fields__ with model_fields and moves validators to the V2 decorators; the bump-pydantic tool automates many of these changes.
Temporary guard that works on both majors
try:
from pydantic.v1.fields import ModelField
except ImportError:
from pydantic.fields import ModelFieldOn Python 3.14 the pydantic.v1 namespace needs a recent Pydantic 2 release; the changelog lists the exact version.
Step 5
Why a plain downgrade usually backfires
Running pip install "pydantic<2" in the main environment looks like the quickest exit, but any other package that has moved to V2 then breaks with its own import error, and the resolver may warn only after the fact. The same environment can hold only one Pydantic major version, which is why the isolated environment, or a fork of the stale package, is the safer choice when you cannot upgrade.
If the stale package is unmaintained and your project needs V2, the realistic options are to fork it and change the import to pydantic.v1, to replace it with an actively maintained alternative, or to keep the part of your system that uses it in a separate service.
Step 6
The package imports under Pydantic 2
- The grep for the old import finds nothing in the packages you depend on.
python -c "import <package>"succeeds and your tests pass.pip checkreports no conflict between the Pydantic version and any package.
It worked when
- The error is a version mismatch, not a missing install.
- Upgrade first, isolate second, patch your own code last.
- Bound Pydantic in requirements and run
pip checkin CI.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. 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.