Skip to content

Model construction fails on non-union types annotated with discriminator. #11661

@palle-k

Description

@palle-k

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Annotating a type with a discriminator that is not a union fails with the following error:

File /path/to/venv/lib/python3.13/site-packages/pydantic/_internal/_model_construction.py:237, in ModelMetaclass.__new__(mcs, cls_name, bases, namespace, __pydantic_generic_metadata__, __pydantic_reset_parent_namespace__, _create_model_module, **kwargs)
    233     set_model_mocks(cls)
    234 else:
    235     # Any operation that requires accessing the field infos instances should be put inside
    236     # `complete_model_class()`:
--> 237     complete_model_class(
    238         cls,
    239         config_wrapper,
    240         raise_errors=False,
    241         ns_resolver=ns_resolver,
    242         create_model_module=_create_model_module,
    243     )
    245 if config_wrapper.frozen and '__hash__' not in namespace:
    246     set_default_hash_func(cls, bases)

File /path/to/venv/lib/python3.13/site-packages/pydantic/_internal/_model_construction.py:620, in complete_model_class(cls, config_wrapper, raise_errors, ns_resolver, create_model_module)
    616 set_deprecated_descriptors(cls)
    618 cls.__pydantic_core_schema__ = schema
--> 620 cls.__pydantic_validator__ = create_schema_validator(
    621     schema,
    622     cls,
    623     create_model_module or cls.__module__,
    624     cls.__qualname__,
    625     'create_model' if create_model_module else 'BaseModel',
    626     core_config,
    627     config_wrapper.plugin_settings,
    628 )
    629 cls.__pydantic_serializer__ = SchemaSerializer(schema, core_config)
    630 cls.__pydantic_complete__ = True

File /path/to/venv/lib/python3.13/site-packages/pydantic/plugin/_schema_validator.py:51, in create_schema_validator(schema, schema_type, schema_type_module, schema_type_name, schema_kind, config, plugin_settings)
     41     return PluggableSchemaValidator(
     42         schema,
     43         schema_type,
   (...)
     48         plugin_settings or {},
     49     )
     50 else:
---> 51     return SchemaValidator(schema, config)

SchemaError: Definitions error: definition `__main__.Foo:4365581472` was never filled

This only happens when FooWithDiscriminator is declared as a PEP-695-style type alias.

Notes:

  1. This only started failing with Pydantic 2.11.1. The provided code works in 2.11.0
  2. This might be a mis-use of discriminator, given that it should typically discriminate between different union cases. However, I would still consider this as a bug in Pydantic given that the exception does not clarify what is wrong with the declared model.

Example Code

from pydantic import BaseModel, Field
from typing import Annotated, Literal

class Foo(BaseModel):
    type: Literal["foo"] = "foo"

type FooWithDiscriminator = Annotated[Foo, Field(discriminator="type")]

class Container(BaseModel):
    f: FooWithDiscriminator

Python, Pydantic & OS Version

pydantic version: 2.11.1
        pydantic-core version: 2.33.0
          pydantic-core build: profile=release pgo=false
                 install path: /path/to/venv/lib/python3.13/site-packages/pydantic
               python version: 3.13.1 (main, Dec  3 2024, 17:59:52) [Clang 19.1.5 ]
                     platform: macOS-15.4-arm64-arm-64bit-Mach-O
             related packages: mypy-1.15.0 pydantic-settings-2.8.1 fastapi-0.115.12 typing_extensions-4.13.0
                       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