Backtestspydanticrun-20261005-183002#13599
PEP 695 type aliases don't work with callable discriminators
Better
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the root cause the same basic way: they pass `definitions` from `apply_discriminator()` into `Discriminator._convert_schema()`, so `definition-ref` schemas built from PEP 695 aliases can be resolved when there is no handler. That covers the `Field(discriminator=callable)` case (M3).
Patch A handles the edge cases better:
1. **Unresolvable choices.** When a choice's ref cannot be resolved on the `definitions` path, A re-raises `MissingDefinitionForUnionRef`, so the discriminator application is deferred until definitions are available. B returns None instead and falls through to a `PydanticUserError` ('Tag not provided'). That is a spurious hard failure in deferred or recursive scenarios.
2. **What goes into the tagged union.** A reads the tag from the resolved definition's metadata but keeps the original `definition-ref` as the choice. B replaces `choice` with the resolved definition schema, which inlines it into the tagged union. This copies the old handler-path behaviour, but it is less safe for recursive or shared definitions.
3. **Tests.** A tests more scenarios:
- a plain alias
- a generic `TypeAliasType` with `type_params`
- an alias as one member of a union
- the error path (`union_tag_invalid`)
- `TypeAdapter` with an `Annotated` `Field`
- a self-referencing deferred case (`Tree`/`Node`) exercising the deferral path
B adds only one minimal happy-path test, with a placeholder `<link>` docstring.
4. **Craft.** A also simplifies the control flow by moving the definition-ref tag lookup into a single block and documents the method. B's `# pragma: no cover` on a branch that now handles real cases is slightly misleading.
Overall, A is more robust and better tested.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
12m 22s
Wall-clock time in the container
Full test suite
No new failures
12,322 passed · 499 skipped · 12,821 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
128vs 60 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial 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.
M1, M2, M4 ok
M3
pydantic.errors.PydanticUserError: `Tag` not provided for choice {'type': 'definition-ref', 'schema_ref': '__main__.TaggedAnyA:2210408548368'} used with `Discriminator`
Example Code from #12843 also doesn’t work.
Example Code
import traceback
from sys import stderr
from pydantic import BaseModel, Discriminator, Field, Tag
from typing_extensions import Annotated, Any, Literal, TypeAlias
class A(BaseModel):
type_: str
class A1(A):
type_: Literal['A1'] = Field(alias='type')
class A2(A):
type_: Literal['A2'] = Field(alias='type')
def _discriminator(data: Any) -> str:
if isinstance(data, dict):
return data['type']
return data.type_
discriminator = Discriminator(_discriminator)
type AnyA = A1 | A2
AnyA_old: TypeAlias = A1 | A2
type TaggedAnyA = Annotated[A1, Tag('A1')] | Annotated[A2, Tag('A2')]
TaggedAnyA_old: TypeAlias = Annotated[A1, Tag('A1')] | Annotated[A2, Tag('A2')]
_data = {"a" : {'type': 'A1'}}
try:
class M1(BaseModel):
a: AnyA = Field(discriminator='type_')
M1.model_validate(_data)
except Exception as e:
traceback.print_exc()
else:
print("OK", file=stderr)
try:
class M2(BaseModel):
a: AnyA_old = Field(discriminator='type_')
M2.model_validate(_data)
except Exception as e:
traceback.print_exc()
else:
print("OK", file=stderr)
try:
class M3(BaseModel):
a: TaggedAnyA = Field(discriminator=discriminator)
M3.model_validate(_data)
except Exception as e:
traceback.print_exc()
else:
print("OK", file=stderr)
try:
class M4(BaseModel):
a: TaggedAnyA_old = Field(discriminator=discriminator)
M4.model_validate(_data)
except Exception as e:
traceback.print_exc()
else:
print("OK", file=stderr)
Python, Pydantic & OS Version
pydantic version: 2.13.4
pydantic-core version: 2.46.4
pydantic-core build: profile=release pgo=false
python version: 3.14.7 (tags/v3.14.7:823f032, Aug 5 2026, 10:51:32) [MSC v.1944 64 bit (AMD64)]
platform: Windows-11-10.0.26200-SP0
related packages: mypy-2.3.0 typing_extensions-4.16.0
commit: unknownThe two patches
Our patch+102 −26
pydantic/_internal/_discriminated_union.pyindex 94c82026..abc5aa25 100644@@ -65,7 +65,7 @@ def apply_discriminator( if isinstance(discriminator.discriminator, str): discriminator = discriminator.discriminator else:- return discriminator._convert_schema(schema)+ return discriminator._convert_schema(schema, definitions=definitions or {}) return _ApplyInferredDiscriminator(discriminator, definitions or {}).apply(schema) pydantic/types.pyindex f32a2ab8..8685bf7f 100644@@ -3112,16 +3112,33 @@ class Discriminator: return self._convert_schema(original_schema, handler) def _convert_schema(- self, original_schema: core_schema.CoreSchema, handler: GetCoreSchemaHandler | None = None+ self,+ original_schema: core_schema.CoreSchema,+ handler: GetCoreSchemaHandler | None = None,+ definitions: dict[str, core_schema.CoreSchema] | None = None, ) -> core_schema.TaggedUnionSchema:- if handler is not None and original_schema['type'] == 'definition-ref':- # Same logic as `_ApplyInferredDiscriminator._apply_to_root()`- try:- def_schema = handler.resolve_ref_schema(original_schema)- except LookupError: # pragma: no cover- from pydantic._internal._discriminated_union import MissingDefinitionForUnionRef+ """Convert `original_schema` (expected to be a union) into a tagged union schema.++ `definition-ref` schemas (e.g. built from PEP 695 type aliases) are resolved using+ `handler` when available (direct `Annotated[..., Discriminator(...)]` usage), or using+ `definitions` otherwise (when called from `apply_discriminator()`, e.g. for `Field(discriminator=...)`).+ """ - raise MissingDefinitionForUnionRef(original_schema['schema_ref'])+ from pydantic._internal._discriminated_union import MissingDefinitionForUnionRef++ def resolve_ref(schema: core_schema.DefinitionReferenceSchema) -> core_schema.CoreSchema:+ if handler is not None:+ try:+ return handler.resolve_ref_schema(schema)+ except LookupError:+ pass+ elif definitions is not None and schema['schema_ref'] in definitions:+ return definitions[schema['schema_ref']]+ raise MissingDefinitionForUnionRef(schema['schema_ref'])++ if original_schema['type'] == 'definition-ref':+ # Same logic as `_ApplyInferredDiscriminator._apply_to_root()`+ def_schema = resolve_ref(original_schema) # If using a referenceable union as discriminated (e.g. `type Pet = Cat | Dog; field: Pet = Field(discriminator=...)`): if def_schema['type'] == 'union':@@ -3143,24 +3160,24 @@ class Discriminator: metadata = cast('CoreMetadata | None', choice.get('metadata')) if metadata is not None: tag = metadata.get('pydantic_internal_union_tag_key') or tag+ if tag is None and choice['type'] == 'definition-ref':+ # If choice was built from a PEP 695 type alias, the tag is set on the referenced definition:+ try:+ def_schema = resolve_ref(choice)+ except MissingDefinitionForUnionRef:+ if definitions is not None:+ # Let `apply_discriminator()` callers defer until definitions are available.+ raise+ else:+ def_metadata = cast('CoreMetadata | None', def_schema.get('metadata'))+ if def_metadata is not None:+ tag = def_metadata.get('pydantic_internal_union_tag_key')+ if tag is None:- # `handler` is None when this method is called from `apply_discriminator()` (deferred discriminators)- if handler is not None and choice['type'] == 'definition-ref':- # If choice was built from a PEP 695 type alias, try to resolve the def:- try:- choice = handler.resolve_ref_schema(choice)- except LookupError:- pass- else:- metadata = cast('CoreMetadata | None', choice.get('metadata'))- if metadata is not None:- tag = metadata.get('pydantic_internal_union_tag_key')-- if tag is None:- raise PydanticUserError(- f'`Tag` not provided for choice {choice} used with `Discriminator`',- code='callable-discriminator-no-tag',- )+ raise PydanticUserError(+ f'`Tag` not provided for choice {choice} used with `Discriminator`',+ code='callable-discriminator-no-tag',+ ) tagged_union_choices[tag] = choice # Have to do these verbose checks to ensure falsy values ('' and {}) don't get ignoredtests/types/unions/test_discriminated_union.pyindex fccb82fb..5bdd854f 100644@@ -2338,6 +2338,65 @@ def test_discriminated_union_type_alias_type_separate_callable_discriminator() - assert m.g == Bar(type='bar') +def test_type_alias_type_callable_discriminator_in_field() -> None:+ """Callable discriminator provided through `Field(discriminator=...)` (or as `FieldInfo` metadata),+ with a PEP 695 type alias as the union.+ """++ class Foo(BaseModel):+ type: Literal['foo']++ class Bar(BaseModel):+ type: Literal['bar']++ discriminator = Discriminator(lambda v: v['type'] if isinstance(v, dict) else v.type)++ FooBar = TypeAliasType('FooBar', Annotated[Foo, Tag('foo')] | Annotated[Bar, Tag('bar')])++ T = TypeVar('T')+ FooBarTV = TypeAliasType('FooBarTV', Annotated[Foo, Tag('foo')] | Annotated[Bar, Tag('bar')], type_params=(T,))++ TaggedFoo = TypeAliasType('TaggedFoo', Annotated[Foo, Tag('foo')])++ class Main(BaseModel):+ f: FooBar = Field(discriminator=discriminator)+ g: FooBarTV[int] = Field(discriminator=discriminator)+ h: TaggedFoo | Annotated[Bar, Tag('bar')] = Field(discriminator=discriminator)++ m = Main(f={'type': 'foo'}, g={'type': 'bar'}, h={'type': 'foo'})+ assert m.f == Foo(type='foo')+ assert m.g == Bar(type='bar')+ assert m.h == Foo(type='foo')++ with pytest.raises(ValidationError) as exc_info:+ Main(f={'type': 'baz'}, g={'type': 'bar'}, h={'type': 'foo'})+ assert exc_info.value.errors(include_url=False)[0]['type'] == 'union_tag_invalid'++ ta = TypeAdapter(Annotated[FooBar, Field(discriminator=discriminator)])+ assert ta.validate_python({'type': 'bar'}) == Bar(type='bar')+ assert ta.validate_python(Foo(type='foo')) == Foo(type='foo')+++def test_type_alias_type_callable_discriminator_in_field_deferred() -> None:+ """Same as `test_type_alias_type_callable_discriminator_in_field()`, but with the discriminator+ application being deferred because of a self-referencing model."""++ class Leaf(BaseModel):+ type: Literal['leaf'] = 'leaf'++ Node = TypeAliasType('Node', "Annotated[Leaf, Tag('leaf')] | Annotated[Tree, Tag('tree')]")++ class Tree(BaseModel):+ type: Literal['tree'] = 'tree'+ child: Node = Field(discriminator=Discriminator(lambda v: v['type'] if isinstance(v, dict) else v.type))+ other: Node = Field(discriminator=Discriminator(lambda v: v['type'] if isinstance(v, dict) else v.type))++ t = Tree.model_validate(+ {'child': {'type': 'tree', 'child': {'type': 'leaf'}, 'other': {'type': 'leaf'}}, 'other': {'type': 'leaf'}}+ )+ assert t == Tree(child=Tree(child=Leaf(), other=Leaf()), other=Leaf())++ @pytest.mark.xfail( reason="Nested references aren't flattened (see comment in `_ApplyInferredDiscriminator._handle_choice()`)", )
Merged fix · PR #13604+47 −13
pydantic/_internal/_discriminated_union.pyindex 94c82026c..64f231459 100644@@ -65,7 +65,7 @@ def apply_discriminator( if isinstance(discriminator.discriminator, str): discriminator = discriminator.discriminator else:- return discriminator._convert_schema(schema)+ return discriminator._convert_schema(schema, definitions=definitions) return _ApplyInferredDiscriminator(discriminator, definitions or {}).apply(schema) pydantic/types.pyindex f32a2ab80..50f94cf43 100644@@ -3112,13 +3112,29 @@ class Discriminator: return self._convert_schema(original_schema, handler) def _convert_schema(- self, original_schema: core_schema.CoreSchema, handler: GetCoreSchemaHandler | None = None+ self,+ original_schema: core_schema.CoreSchema,+ handler: GetCoreSchemaHandler | None = None,+ definitions: dict[str, core_schema.CoreSchema] | None = None, ) -> core_schema.TaggedUnionSchema:- if handler is not None and original_schema['type'] == 'definition-ref':+ def _resolve_ref(schema: core_schema.CoreSchema) -> core_schema.CoreSchema | None:+ # `handler` is set when this method is called while generating the schema for the field+ # (e.g. via `Annotated[<type>, Discriminator(...)]`). `definitions` is set instead when+ # this method is called from `apply_discriminator()` (deferred/callable discriminators).+ if handler is not None:+ try:+ return handler.resolve_ref_schema(schema)+ except LookupError: # pragma: no cover+ return None+ elif definitions is not None:+ schema_ref = cast(core_schema.DefinitionReferenceSchema, schema)['schema_ref']+ return definitions.get(schema_ref)+ return None++ if original_schema['type'] == 'definition-ref': # Same logic as `_ApplyInferredDiscriminator._apply_to_root()`- try:- def_schema = handler.resolve_ref_schema(original_schema)- except LookupError: # pragma: no cover+ def_schema = _resolve_ref(original_schema)+ if def_schema is None: from pydantic._internal._discriminated_union import MissingDefinitionForUnionRef raise MissingDefinitionForUnionRef(original_schema['schema_ref'])@@ -3144,14 +3160,11 @@ class Discriminator: if metadata is not None: tag = metadata.get('pydantic_internal_union_tag_key') or tag if tag is None:- # `handler` is None when this method is called from `apply_discriminator()` (deferred discriminators)- if handler is not None and choice['type'] == 'definition-ref':+ if choice['type'] == 'definition-ref': # If choice was built from a PEP 695 type alias, try to resolve the def:- try:- choice = handler.resolve_ref_schema(choice)- except LookupError:- pass- else:+ resolved_choice = _resolve_ref(choice)+ if resolved_choice is not None:+ choice = resolved_choice metadata = cast('CoreMetadata | None', choice.get('metadata')) if metadata is not None: tag = metadata.get('pydantic_internal_union_tag_key')tests/types/unions/test_discriminated_union.pyindex fccb82fbb..637679102 100644@@ -2338,6 +2338,27 @@ def test_discriminated_union_type_alias_type_separate_callable_discriminator() - assert m.g == Bar(type='bar') +def test_discriminated_union_type_alias_type_separate_callable_discriminator_field() -> None:+ """https://github.com/pydantic/pydantic/issues/13599"""++ class Foo(BaseModel):+ type: Literal['foo']++ class Bar(BaseModel):+ type: Literal['bar']++ FooBar = TypeAliasType('FooBar', Annotated[Foo, Tag('foo')] | Annotated[Bar, Tag('bar')])++ discriminator = Discriminator(lambda v: v['type'])++ class Main(BaseModel):+ f: FooBar = Field(discriminator=discriminator)++ m = Main(f={'type': 'foo'})++ assert m.f == Foo(type='foo')++ @pytest.mark.xfail( reason="Nested references aren't flattened (see comment in `_ApplyInferredDiscriminator._handle_choice()`)", )