Back to Discover

#pydantic v2 migration

1 prompt found

Pydantic v1 to v2 Migration Planner for FastAPI and Python Services: bump-pydantic First Pass, validator to field_validator, root_validator to model_validator, Config to ConfigDict, BaseSettings Move, and Behavior Changes
๐Ÿ’ป Coding

Pydantic v1 to v2 Migration Planner for FastAPI and Python Services: bump-pydantic First Pass, validator to field_validator, root_validator to model_validator, Config to ConfigDict, BaseSettings Move, and Behavior Changes

PpromptstudioยทOct 7, 2026
No rating

Migrate a codebase from Pydantic v1 to v2 without silent behavior changes: run bump-pydantic first, convert validators and root validators, move Config to model_config with renamed keys, replace dict, json, parse_obj, and from_orm calls, move BaseSettings to pydantic-settings, and catch Optional default and strict coercion changes with targeted tests.

Act as a Python backend engineer who has migrated several FastAPI and worker services from Pydantic v1 to v2, uses bump-pydantic for the mechanical pass, and knows which changes it cannot make for you: validator signatures, Optional defaults, and coercion that v1 did quietly. Inputs: - Current versions: Python, pydantic, FastAPI or other frameworks that depend on pydantic, and the target versions: [Versions] - Pasted model code: the models, validators, Config classes, and settings classes to migrate: [ModelCode] - Call sites: where the code uses .dict(), .json(), parse_obj, parse_raw, from_orm, schema(), copy(), or __fields__: [CallSites] - Test coverage and CI notes: what tests exist around these models and which payloads are known in production: [TestNotes] - Rollout constraints: big bang or gradual, other services that import these models, a deadline: [Rollout] - Output format: [Format] Generate: 1. A plan header: dependency changes from Versions (pydantic 2, pydantic-settings if BaseSettings is used, a FastAPI release that supports v2), and whether to use the pydantic.v1 namespace for a gradual path given Rollout. 2. bump-pydantic first pass: the command to run on the package and a list of what it will and will not rewrite in ModelCode. 3. Validator conversions: each @validator to @field_validator with @classmethod, each pre=True to mode='before', each always=True to validate_default, each values argument to info.data via ValidationInfo, and each @root_validator to @model_validator with the right mode. 4. Config conversions: class Config to model_config = ConfigDict(...) with renamed keys (orm_mode to from_attributes, allow_population_by_field_name to populate_by_name, anystr_strip_whitespace to str_strip_whitespace, schema_extra to json_schema_extra), and json_encoders replaced by field_serializer where needed. 5. Field changes: regex to pattern, const removed, __root__ models to RootModel, and Optional fields with no default that become required. 6. Call site replacements from CallSites: dict to model_dump, json to model_dump_json, parse_obj to model_validate, parse_raw to model_validate_json, from_orm to model_validate with from_attributes, schema to model_json_schema, copy to model_copy, __fields__ to model_fields. 7. Behavior change tests from TestNotes: payloads that v1 coerced (an int into a str field), Optional without default, validator order, and serialized output shape, each as a pytest case. 8. A rollout checklist: order of services, the diff to review, and what to watch in logs after deploy. Constraints: - Show before and after code for every model in ModelCode. Do not invent fields or call sites; mark anything unclear as CHECK. - Mark API names that changed between minor v2 releases as CHECK against the installed version. No em dashes.