Repository navigation
Conversation
This fix allows RootModel[Literal[...]] to be used as a discriminator field type in discriminated unions. When inferring discriminator values, we now handle root models by recursively processing their inner schema. Fixes pydantic#12605
YassinNouh21
force-pushed
the
fix/root-model-literal-discriminator-12605
branch
from
January 8, 2026 20:04
6388e96 to
cf7d840
Compare
CodSpeed Performance ReportMerging this PR will improve performance by 6.4%Comparing Summary
Performance Changes
|
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Remove redundant comment about RootModel inner schema.
Contributor
Author
|
@Viicos I will be free the next weeks, I would like to contribute to Paydantic so feel free to assign me to issues |
Refactor tests for discriminated unions with RootModel and update validation logic.
Literal root types as discriminator field types
- Fix duplicate Model1 class -> Model2 - Fix TypeAdater typo -> TypeAdapter - Fix model1 -> model2 in assertion - Fix "specificaly" -> "specifically" typo in docs
Viicos
approved these changes
Jan 14, 2026
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.
Summary
RootModel[Literal[...]]as a discriminator field type in discriminated unionsdatamodel-code-generatorthat wrap Literal types in RootModel classesThe Problem
When using a
RootModel[Literal['X']]as a field type for a discriminator, Pydantic previously failed with:The Fix
Added handling in
_infer_discriminator_values_for_inner_schemafor'model'schema type with'root_model': Trueflag. This unwraps the RootModel to get the inner Literal schema.Test plan
test_discriminated_union_with_root_model_literal- basic RootModel[Literal] discriminatortest_discriminated_union_with_root_model_literal_multiple_values- RootModel with multiple Literal valuesFixes #12605