Skip to content

PEP 695 type aliases don't work with callable discriminators #12843

@abrahammurciano

Description

@abrahammurciano

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

New type aliases prefixed with the type keyword are not supported as the type annotation for a callable discriminator.

Related:

Example Code

import random
from typing import Annotated
from pydantic import BaseModel, Discriminator, Field, Tag, TypeAdapter

discriminator = Discriminator(lambda _: random.choice(["a", "b", None]))
OldUnion = Annotated[int, Tag("a")] | Annotated[str, Tag("b")]
type NewUnion = Annotated[int, Tag("a")] | Annotated[str, Tag("b")]

TypeAdapter[OldUnion](Annotated[OldUnion, discriminator]) # OK
TypeAdapter[NewUnion](Annotated[NewUnion, discriminator]) # TypeError: Discriminator must be used with a Union type, not NewUnion

class M(BaseModel):
    u1: OldUnion = Field(discriminator=discriminator) # OK
    u2: NewUnion = Field(discriminator=discriminator) # pydantic.errors.PydanticUserError: `Tag` not provided for choice {'type': 'definition-ref', 'schema_ref': '__main__.NewUnion:140737344689696'} used with `Discriminator`

Python, Pydantic & OS Version

pydantic version: 2.12.5
        pydantic-core version: 2.41.5
          pydantic-core build: profile=release pgo=false
               python version: 3.14.0 (main, Oct 14 2025, 21:27:55) [Clang 20.1.4 ]
                     platform: Linux-5.15.0-157-generic-x86_64-with-glibc2.35
             related packages: fastapi-0.129.2 email-validator-2.3.0 typing_extensions-4.15.0 mypy-1.19.1
                       commit: unknown

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions