Skip to content

Support Root models with Literal root types as discriminator field types - #12680

Merged
Viicos merged 8 commits into
pydantic:mainfrom
YassinNouh21:fix/root-model-literal-discriminator-12605
Jan 14, 2026
Merged

Viicos merged 8 commits into
pydantic:mainfrom
YassinNouh21:fix/root-model-literal-discriminator-12605

Conversation

@YassinNouh21

Copy link
Copy Markdown
Contributor

Summary

  • Allow using RootModel[Literal[...]] as a discriminator field type in discriminated unions
  • This is useful when using code generators like datamodel-code-generator that wrap Literal types in RootModel classes

The Problem

When using a RootModel[Literal['X']] as a field type for a discriminator, Pydantic previously failed with:

pydantic.errors.PydanticUserError: Model 'OperationActionData' needs field 'action' to be of type `Literal`

The Fix

Added handling in _infer_discriminator_values_for_inner_schema for 'model' schema type with 'root_model': True flag. This unwraps the RootModel to get the inner Literal schema.

Test plan

  • Added test_discriminated_union_with_root_model_literal - basic RootModel[Literal] discriminator
  • Added test_discriminated_union_with_root_model_literal_multiple_values - RootModel with multiple Literal values
  • All existing discriminated union tests pass

Fixes #12605

@github-actions github-actions Bot added the relnotes-fix Used for bugfixes. label Jan 8, 2026
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
YassinNouh21 force-pushed the fix/root-model-literal-discriminator-12605 branch from 6388e96 to cf7d840 Compare January 8, 2026 20:04
@codspeed

codspeed Bot commented Jan 8, 2026 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging this PR will improve performance by 6.4%

Comparing YassinNouh21:fix/root-model-literal-discriminator-12605 (547bbe3) with main (b6dae31)

Summary

⚡ 1 improved benchmark
✅ 211 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_arguments 25.6 µs 24.1 µs +6.4%

@github-actions

github-actions Bot commented Jan 8, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _discriminated_union.py
Project Total  

This report was generated by python-coverage-comment-action

Remove redundant comment about RootModel inner schema.
@YassinNouh21

Copy link
Copy Markdown
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.
@Viicos Viicos changed the title Support RootModel[Literal[...]] as discriminator field type Support Root models with Literal root types as discriminator field types Jan 11, 2026
@Viicos Viicos added relnotes-feature and removed relnotes-fix Used for bugfixes. labels Jan 11, 2026
YassinNouh21 and others added 3 commits January 11, 2026 22:36
- Fix duplicate Model1 class -> Model2
- Fix TypeAdater typo -> TypeAdapter
- Fix model1 -> model2 in assertion
- Fix "specificaly" -> "specifically" typo in docs
@Viicos
Viicos merged commit de141b5 into pydantic:main Jan 14, 2026
67 checks passed
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.

Use RootModel[Literal[..]] as discriminator

2 participants