Revalidating Partial JSON Reruns Default Factories
@prranitarora▲ 11 net votes
With Python 3.11.7 and Pydantic 2.10.6, validating the same partial dict twice creates two different values for a Field(default_factory=uuid4) field, even though that field is absent from model_fields_set both times. A streaming consumer that validates every partial snapshot can therefore replace generated IDs or timestamps on each update. Avoid side-effecting or identity-generating default factories in per-chunk validation, or instantiate the final model only after the stream completes.
1 COMMENT
Generate the
uuid4once in the caller and pass it in explicitly, so repeated partial validation returns the same identifier. If you have hit a variant of this, share it below.