Skip to content

Migrate to Pydantic v2 - #4077

Merged
r4victor merged 15 commits into
masterfrom
issue_1844_pydantic_v2_migration
Aug 3, 2026
Merged

Migrate to Pydantic v2#4077
r4victor merged 15 commits into
masterfrom
issue_1844_pydantic_v2_migration

Conversation

@r4victor

@r4victor r4victor commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

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-duality replaced with extra="forbid" CoreModel + per-call extra="ignore". Every model was previously generated as a request/response pair by generate_dual_core_model, where the X.__response__ variant allowed unknown fields. Pydantic since version 2.12 supports per-call extra= argument, so this is how now dstack does the validation in two different modes without pydantic-duality.
  • __root__ replaced with RootModel.
  • Types with custom parsing implementing __get_validators__ replaced with types implementing __get_pydantic_core_schema__ (Duration, Memory, Range, etc).
  • Fixed annotated stored types and json input types. (In some cases, the annotated type was different from the actual stored type or a json type was missing.) Defined proper json input types on types instead of fields (e.g. Duration) and introduced OptionalOffableDuration and OptionalIdleDuration types to avoid duplicating per-model/per-field validators and json input types. Improves configurations json schemas and references.
  • Dropped orjson and replaced CustomORJSONResponse with Pydantic v2 native serialization.
  • @validator => field_validator, @root_validator => model_validator, GenericModel => BaseModel, Generic[T], model *() methods => model_*() methods.

Behavior changes:

  • UTC datetimes now serialize as 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 handle Z fine. Some HTTP API client may break (e.g. Python's fromisoformat() before 3.11) but this should be uncommon and is accepted.
  • Fractional floats are rejected where an int is declared. nodes: 2.5 was allowed and truncated. Now it is a validation error; nodes: 2.0 still parses as 2.

Breaking changes:

  • dstack Python API now works with Pydantic V2 models.
  • dstack plugins now work with Pydantic V2 models.

@r4victor
r4victor marked this pull request as ready for review August 3, 2026 06:26
@r4victor
r4victor merged commit d10edfd into master Aug 3, 2026
27 checks passed
@r4victor
r4victor deleted the issue_1844_pydantic_v2_migration branch August 3, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Support for Pydantic v2

1 participant