Skip to content

feat: switch default output model to pydantic v2#3029

Merged
koxudaxi merged 1 commit intomainfrom
split/pr2b-switch-default-v2
Mar 6, 2026
Merged

feat: switch default output model to pydantic v2#3029
koxudaxi merged 1 commit intomainfrom
split/pr2b-switch-default-v2

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Mar 6, 2026

Summary by CodeRabbit

  • Breaking Changes
    • Default code generation target changed from Pydantic v1 BaseModel to Pydantic v2 BaseModel. Users can still explicitly specify Pydantic v1 output via configuration.
    • Deprecation warning about the default model type has been removed.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 6, 2026

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 6, 2026

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing split/pr2b-switch-default-v2 (e6902a6) with main (3bedaaa)

Open in CodSpeed

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Base automatically changed from split/pr2b-prep-explicit-output to main March 6, 2026 15:11
@koxudaxi koxudaxi force-pushed the split/pr2b-switch-default-v2 branch from b547ca6 to e6902a6 Compare March 6, 2026 15:13
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 6, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4b064f7f-31a0-4859-bd40-c7a9f368e649

📥 Commits

Reviewing files that changed from the base of the PR and between 3bedaaa and e6902a6.

📒 Files selected for processing (11)
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/arguments.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/input_model.py
  • tests/data/expected/main/space_and_special_characters_dict_default_v2.py
  • tests/main/conftest.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/openapi/test_main_openapi.py
  • tests/main/test_main_general.py
  • tests/main/test_public_api_signature_baseline.py
  • tests/test_input_model.py

📝 Walkthrough

Walkthrough

The pull request updates the default code-generation target from Pydantic v1 BaseModel to Pydantic v2 BaseModel across configuration and public API declarations. The deprecation warning about the previous default has been removed. Tests are adjusted to maintain coverage for the previous default behavior by explicitly specifying Pydantic v1 where needed, and a new test fixture file documents expected output with the new default.

Changes

Cohort / File(s) Summary
Default Output Model Type Configuration
src/datamodel_code_generator/__main__.py, src/datamodel_code_generator/config.py, src/datamodel_code_generator/input_model.py
Changed default value of output_model_type from DataModelType.PydanticBaseModel to DataModelType.PydanticV2BaseModel across Config, GenerateConfig, and load_model_schema. Removed deprecation warning block in main.py.
CLI Help Text Update
src/datamodel_code_generator/arguments.py
Updated help text for --output-model-type option to indicate pydantic_v2.BaseModel as the new default instead of pydantic.BaseModel.
Test Fixture for New Default
tests/data/expected/main/space_and_special_characters_dict_default_v2.py
New Pydantic v2 generated model file containing nested BaseModel classes (InitialParameters, Data, Values, Recursive, Model) with field aliases for irregular naming patterns.
Test Infrastructure Updates
tests/main/conftest.py
Extended URL-based test entry points with default_output_model_type parameter; updated _should_skip_exec logic to only treat explicit PydanticBaseModel.value as v1; modified output validation to normalize extra arguments before validation.
Test Baseline Adjustments
tests/main/test_public_api_signature_baseline.py, tests/test_input_model.py
Changed default parameter value of output_model_type in _baseline_generate from PydanticBaseModel to PydanticV2BaseModel; updated docstring to reflect new default.
Test Cases with Explicit v1 Model Type
tests/main/jsonschema/test_main_jsonschema.py, tests/main/openapi/test_main_openapi.py, tests/main/test_main_general.py
Added explicit output_model_type=DataModelType.PydanticBaseModel parameter to specific test invocations to maintain testing of Pydantic v1 generation path; updated test assertion to use new v2 fixture file.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • #3028: Modifies test integration helpers in conftest.py to propagate explicit default_output_model_type and normalize output model type arguments for CLI invocations.
  • #2832: Modifies the _baseline_generate function default in test_public_api_signature_baseline.py, directly affected by this PR's baseline parameter change.
  • #2910: Addresses the deprecation warning and default handling logic in __main__.py, touching the same code sections modified in this PR.

Suggested labels

breaking-change, breaking-change-analyzed

Poem

🐰 With a hop and a skip through version two,
Our models now default to Pydantic v2's hue,
The warning's been buried, the default's in place,
Tests adjusted with v1 in just the right space!
Onward we bounce to a newer baseline!

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch split/pr2b-switch-default-v2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3bedaaa) to head (e6902a6).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3029   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           95        95           
  Lines        18174     18174           
  Branches      2099      2098    -1     
=========================================
  Hits         18174     18174           
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi marked this pull request as ready for review March 6, 2026 15:19
@koxudaxi koxudaxi merged commit 03200be into main Mar 6, 2026
40 checks passed
@koxudaxi koxudaxi deleted the split/pr2b-switch-default-v2 branch March 6, 2026 15:19
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 6, 2026

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: This PR changes the default value of output_model_type from DataModelType.PydanticBaseModel (Pydantic v1) to DataModelType.PydanticV2BaseModel (Pydantic v2) across multiple files: __main__.py, config.py, and input_model.py. This is a breaking change because users who previously ran datamodel-codegen without explicitly specifying --output-model-type will now get different generated output. The generated code will use Pydantic v2 syntax which is incompatible with Pydantic v1. The deprecation warning that previously alerted users to this upcoming change has also been removed.

Content for Release Notes

Default Behavior Changes

  • Default output model switched from Pydantic v1 to v2 - Running datamodel-codegen without specifying --output-model-type now generates Pydantic v2 models (pydantic_v2.BaseModel) instead of Pydantic v1 models (pydantic.BaseModel). Users who depend on the previous default behavior must now explicitly specify --output-model-type pydantic.BaseModel to continue generating Pydantic v1 compatible code. (feat: switch default output model to pydantic v2 #3029)

Code Generation Changes

  • Generated model syntax changed for default output - The default generated code now uses Pydantic v2 syntax including RootModel instead of __root__ fields, native union syntax (str | None) instead of Optional[str], and Pydantic v2 validator/serializer decorators. Existing code that consumes generated models may need updates to work with the new Pydantic v2 output format. (feat: switch default output model to pydantic v2 #3029)

This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

🎉 Released in 0.55.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant