The Wire · Showcase
PYDANTIC FIXES DEEPCOPY CRASH IN MODEL_COPY
By RepoJournal · Filed · About FastAPI & Pydantic
Pydantic's `model_copy(deep=True)` no longer tries to deepcopy fields you're about to replace, fixing crashes when those fields hold undeepcopyable objects like GPU tensors or C extensions.
The fix [1] is surgical and high-impact. Before, `model_copy(deep=True, update={...})` would deepcopy every field including ones you're immediately replacing, which breaks when those fields contain native C extensions, file handles, or GPU tensors. Now it only deepcopies fields that survive the update, and it properly maintains shared references between fields by using a single `memo` dict across all deepcopy calls. This preserves the behavior for `__pydantic_extra__` and `__pydantic_private__` as well. In parallel, the Pydantic team fixed a mypy plugin regression [2] where `create_model(..., __base__=cls)` inside a classmethod would lose the `Self` type. The mypy plugin now falls back to the normal overload definition when `__base__` is a type variable, keeping proper generic inference. Expect these in the next release.
Action items
- → Watch for next Pydantic release with model_copy fix if you use deep=True with GPU tensors or C extension fields pydantic/pydantic [monitor]
- → If using dynamic models with create_model in classmethods, test with upcoming Pydantic release for proper type narrowing pydantic/pydantic [plan]
References
- [1] Only deepcopy non-updated fields in `model_copy()` ↗ pydantic/pydantic
- [2] Fallback to `create_model()` overload definition when `__base__` is set to a type variable ↗ pydantic/pydantic
FAQ
- What changed in FastAPI & Pydantic on May 17, 2026?
- Pydantic's `model_copy(deep=True)` no longer tries to deepcopy fields you're about to replace, fixing crashes when those fields hold undeepcopyable objects like GPU tensors or C extensions.
- What should FastAPI & Pydantic teams do about it?
- Watch for next Pydantic release with model_copy fix if you use deep=True with GPU tensors or C extension fields • If using dynamic models with create_model in classmethods, test with upcoming Pydantic release for proper type narrowing
- Which FastAPI & Pydantic repositories shipped on May 17, 2026?
- pydantic/pydantic