skip to content

In Pydantic 2, what does importing BaseModel from the pydantic.v1 namespace buy a team mid-migration, and where does that shim stop helping?

level: seniorimportance: should knowfreq 35%

answer

  1. a frozen copy inside the package
  2. old behaviour, new install
  3. works from late 1.10 releases too
  4. the two kinds do not nest
  5. fixes only until version 3

basics

~20 s

pydantic.v1 is a copy of the version 1 library bundled inside Pydantic 2, so unmigrated models keep version 1 behaviour on the new install. It stops at the boundary: version 1 and version 2 models must not be nested or mixed.

solid answer

~50 s

Pydantic 2 bundles the last version 1 release under `pydantic.v1` (2.14.0 carries 1.10.26), so `from pydantic.v1 import BaseModel, validator` keeps old validators, `class Config`, `orm_mode` and `__root__` working with version 1 semantics. From 1.10.17 the migration guide supports the same namespace within version 1, so a team can switch imports to `pydantic.v1` first, upgrade the dependency with no behaviour change, then migrate models to `pydantic` one cluster at a time. Its limits: mixing is not supported, so a Pydantic 2 model or `TypeAdapter` that references a version 1 model gets a `UserWarning` and should not be relied on; models that nest each other should move together; the version 1 line gets only critical and security fixes until version 3; and type checking needs the `pydantic.v1.mypy` plugin beside `pydantic.mypy`. It is a bridge, not a place to stay.

code

python · 30 lines
python
from typing import List

from pydantic.v1 import BaseModel, validator


class ProductOut(BaseModel):
    sku: str
    price: int

    class Config:
        orm_mode = True

    @validator('sku', pre=True)
    def normalise_sku(cls, value):
        return value.strip().upper()


class ProductList(BaseModel):
    __root__: List[ProductOut]


class ProductRow:  # stands in for an ORM object
    sku = ' a-1 '
    price = 500


print(ProductOut.from_orm(ProductRow()))
#> sku='A-1' price=500
print(ProductList.parse_obj([{'sku': 'b-2', 'price': '9'}]).__root__)
#> [ProductOut(sku='B-2', price=9)]

go deeper

for a junior

Recall that Pydantic 2 bundles the old library as pydantic.v1, so old models keep working when imported from there.

for a middle

Explain the three-step route: switch imports while on 1.10.17 or later, upgrade the package, then migrate models one cluster at a time.

for a senior

Name the limits: no mixing of the two model kinds, clusters that must move together, both mypy plugins, and version 1 fixes ending at version 3.

for a principal

Treat pydantic.v1 as a funded bridge with an end date: decide which clusters move first, and track what still imports from it.

## What the namespace is Pydantic 2 ships two libraries in one package. `pydantic` is the version 2 API. **`pydantic.v1`** is a vendored copy of the latest version 1 release, updated as version 1 gets fixes: the 2.13 release notes say it moved to 1.10.26, and that is the copy in 2.14.0. Code that imports from `pydantic.v1` gets version 1 behaviour: `@validator`, `@root_validator`, `class Config`, `orm_mode`, `from_orm()`, `__root__`, the old coercion rules, `parse_obj()` and `.dict()` with no deprecation warnings, because nothing there is deprecated. ## The three-step import route The migration guide describes a route that separates the dependency upgrade from any behaviour change: 1. **Still on version 1**, require `pydantic>=1.10.17` (the release the migration guide names for using `pydantic.v1` within version 1; the namespace first appeared in 1.10.15) and rewrite every `from pydantic.<module> import X` as `from pydantic.v1.<module> import X`. Behaviour is unchanged because it is the same library. 2. **Upgrade to Pydantic 2.** The imports already point at `pydantic.v1`, so the code still runs version 1 semantics, now on the new install. 3. **Migrate models to `pydantic`** in clusters, each cluster rewritten to the version 2 API and tested before the next. For a library that must import under either major version, the guide shows a `try: from pydantic.v1.fields import ModelField` / `except ImportError: from pydantic.fields import ModelField` pattern. ## What a bridged codebase looks like During step 3, modules on each side of the boundary sit next to each other but never reference each other's models: ```python # catalog/legacy.py: not migrated yet from pydantic.v1 import BaseModel, validator # catalog/orders.py: migrated cluster from pydantic import BaseModel, field_validator ``` Conversion between the two sides goes through plain data, for example `NewModel.model_validate(legacy.dict())`, never through a field typed as the other kind of model. ## What it buys - **A dependency upgrade with no behaviour change**, so other packages that need Pydantic 2 can be adopted without migrating every model first. - **Small, reviewable migration changes**, because each cluster moves on its own schedule. - **A reference implementation for tests**: the old definition can sit beside the new one, and both can validate the same recorded payloads. - **Access to removed helpers**, such as `pydantic.v1.utils.lenient_isinstance`. ## Where it stops - **No mixing.** A version 2 model with a field typed as a version 1 model, or a `TypeAdapter` built over one, triggers `UserWarning: Mixing V1 models and V2 models (or constructs, like TypeAdapter) is not supported`. The warning is the contract; on 2.14.0, validating such a model then failed with a `TypeError`. Generic version 2 models also cannot take version 1 models as type parameters. - **Clusters, not files.** Because models cannot cross the boundary, a model, the models it nests and the models that nest it should move in one change. - **Frozen features.** The copy receives version 1 fixes only; nothing from Pydantic 2's core, serializers or speed reaches it. - **Tooling needs both plugins.** Type checking mixed code needs `pydantic.v1.mypy` listed beside `pydantic.mypy`. - **New Python releases arrive late.** The 2.12 release notes warned that version 1 did not work on Python 3.14; support came with the 1.10.26 copy shipped in 2.13. ## Support horizon | Line | What the version policy promises | |---|---| | version 1 (and `pydantic.v1`) | critical bug fixes and security fixes until Pydantic 3 is released | | deprecated version 1 names in `pydantic` | kept on every 2.x release, removed in version 3 | | Pydantic 2 API | no intentional breaking changes in minor releases | The namespace is a migration aid with a clock on it: plan to have nothing importing from `pydantic.v1` by the time version 3 lands.

  • Why should models that nest each other move to Pydantic 2 in the same change?
    Version 1 and version 2 models cannot be mixed: a Pydantic 2 model with a field typed as a `pydantic.v1` model only gets a `UserWarning` saying mixing is not supported, and its validation cannot be trusted. So the unit of migration is a cluster: a model, what it nests and what nests it. Map those references first, then move each cluster whole.
  • Does pydantic.v1 keep working on new Python releases?
    Only as far as the version 1 line is fixed. The Pydantic 2.12 release notes said version 1 was not compatible with Python 3.14; the 1.10.26 copy shipped in 2.13 added that support. Treat every Python upgrade as a check on remaining `pydantic.v1` code, and remember fixes stop when version 3 is released.

  • A Pydantic 2 model can safely nest a pydantic.v1 model during migration
  • pydantic.v1 is a compatibility layer that translates old code to the version 2 core
  • The pydantic.v1 namespace only exists once you install Pydantic 2
  • Code under pydantic.v1 gets Pydantic 2's speed and serializers
  • pydantic.v1 is supported indefinitely, so there is no deadline to leave it

Assumes Pydantic 2 (checked on 2.14.0, which bundles 1.10.26 as pydantic.v1); the migration guide supports the namespace within version 1 from 1.10.17.