Migrate to Pydantic v2 - #4077
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1844
The PR is mostly a mechanical migration from Pydantic v1 features to Pydantic v2 features as described in https://pydantic.dev/docs/validation/dev/get-started/migration/. Parsing and serialization of all models is preserved with a few exceptions noted below. Python 3.14 is now supported for dstack installation and in run configurations. Overall, the PR improves validation and serialization speed, CLI startup times (due to pydantic-duality drop), code maintainability and drops several dependencies.
Main changes:
pydantic-dualityreplaced withextra="forbid"CoreModel+ per-callextra="ignore". Every model was previously generated as a request/response pair bygenerate_dual_core_model, where theX.__response__variant allowed unknown fields. Pydantic since version 2.12 supports per-callextra=argument, so this is how now dstack does the validation in two different modes withoutpydantic-duality.__root__replaced withRootModel.__get_validators__replaced with types implementing__get_pydantic_core_schema__(Duration,Memory,Range, etc).Duration) and introducedOptionalOffableDurationandOptionalIdleDurationtypes to avoid duplicating per-model/per-field validators and json input types. Improves configurations json schemas and references.orjsonand replacedCustomORJSONResponsewith Pydantic v2 native serialization.@validator=>field_validator,@root_validator=>model_validator,GenericModel=>BaseModel, Generic[T], model*()methods =>model_*()methods.Behavior changes:
Z, not+00:00. This is Pydantic v2 default behavior that I think is not worth overriding. The dstack clients (Python, Go, JS) should all handleZfine. Some HTTP API client may break (e.g. Python'sfromisoformat()before 3.11) but this should be uncommon and is accepted.nodes: 2.5was allowed and truncated. Now it is a validation error;nodes: 2.0still parses as2.Breaking changes: