acoustic-carpenter-78188
06/30/2026, 6:18 PMUnionTransformer.to_python_value selects a variant by transformer name, then falls back to "whichever variants model_validate without raising." For a top-level union whose variants are all plain `BaseModel`s, every variant resolves to the same "Pydantic Transformer", so the name filter never narrows.
When the stored literal has no structure.tag (union_tag is None) — e.g. a --volume '{"type":"prior_run",...}' CLI input, or any union value read back from a prior run's inputs — the loop passes the whole union literal to the pydantic transformer, which deserializes an empty/default value. Multiple default-constructible variants then succeed and it raises a false:
Ambiguous choice of variant for union type.
Both Pydantic Transformer and Pydantic Transformer transformers match
This makes any task with a top-level pydantic-union input uninvocable, and breaks reading such inputs back (e.g. auto-resume scanning prior runs whose volume: VolumeSource input is a populated variant).
Minimal repro
import asyncio, typing
from typing import Literal
from pydantic import BaseModel
import msgpack
from flyteidl2.core.literals_pb2 import Binary, Literal as L, Scalar, Union
from flyteidl2.core.types_pb2 import LiteralType, SimpleType
from flyte.types import TypeEngine
class Latest(BaseModel):
type: Literal["latest"] = "latest"
class Fresh(BaseModel):
type: Literal["fresh"] = "fresh"
class Prior(BaseModel):
type: Literal["prior_run"] = "prior_run"
name: str
VolumeSource = typing.Union[Latest, Fresh, Prior]
# An untagged union literal (no structure.tag) — exactly how a
# --volume '{"type":"prior_run","name":"x"}' input is stored and later read back.
inner = L(scalar=Scalar(binary=Binary(value=msgpack.dumps({"type": "prior_run", "name": "x"}), tag="msgpack")))
lv = L(scalar=Scalar(union=Union(value=inner, type=LiteralType(simple=SimpleType.STRUCT))))
print(asyncio.run(TypeEngine.to_python_value(lv, VolumeSource)))
# Before: TypeError: Ambiguous choice of variant for union type.
# Both Pydantic Transformer and Pydantic Transformer transformers match
# After: type='prior_run' name='x'
(The two default-constructible variants `Latest`/`Fresh` both deserialize from the empty default → the false ambiguity, before the actual Prior value is even considered.)
Fix
Add a discriminator-aware fast path to `UnionTransformer.to_python_value`: decode the union value to a dict and resolve to the unique variant by
1. its discriminator field — a field common to all variants typed as a single-value typing.Literal with a distinct value per variant (e.g. type), or
2. a unique model_fields superset of the value's keys,
before the structural loop. Falls through unchanged for non-pydantic unions, genuinely-ambiguous unions, and Optional[X] (single pydantic variant). This mirrors what the nested-union path already does (descriptor.mapping + _select_unambiguous_variant) and extends it to the top level.
Test
tests/flyte/type_engine/pydantic/test_top_level_pydantic_union.py reproduces the exact untagged-literal ambiguity (raises without the fix, resolves correctly with it), plus tagged round-trips and resolver unit cases. Existing union/pydantic suites pass (88 + 30).
🤖 Generated with Claude Code
https://claude.ai/code/session_01DHv4QxWZ5C3jBZs3v3LqWV
flyteorg/flyte-sdk