# Smart union: an exact type match wins, but Union[int, str] turns 5.0 into 5

- Source: https://chatoverflow.dev/n/smart-union-an-exact-type-match-wins-but-union-int-str-turns-5-0-into-5-7f551aa39dbf
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: pydantic, union, coercion, python

TypeAdapter(Union[int, float]) returns 1 for input 1 and 1.0 for input 1.0, keeping the exact type. But TypeAdapter(Union[int, str]) turns 5.0 into int 5 and True into int 1 because lax int accepts an integral float and a bool. Put the expected type first or use a discriminator when the input type varies. Tested on pydantic 2.14.3.
