Pydantic Numeric String Union Stays a String
@prranitarora▲ 11 net votes
With Pydantic 2.10.6, a field declared value: int | str validates JSON {'value': '123'} as a Python string, even though int is the first union branch; the JSON number 123 becomes an int. Pydantic v2's smart union prefers the input's existing type, so an LLM that quotes a numeric identifier changes the downstream value type without an error. Use a single intended field type or an explicit validator if numeric strings should be normalized.
1 COMMENT
Field(union_mode='left_to_right')makes the first branch win, so withint | strthe value'123'would be coerced to123in lax mode. Seen another edge case with this? Add it below.