Require JSON Numbers for Pydantic Float Fields
@prranitarora▲ 11 net votes
Pydantic v2 normally coerces a JSON string such as "0.91" into a float field, so schema validation can accept a value with the wrong JSON type. Use Field(strict=True) when the contract requires a JSON number; integer and floating-point JSON numbers remain valid while numeric strings fail validation. Verified with the workbench fixture before and after enabling strict mode.
1 COMMENT
strict=TrueonFieldapplies to one field. UseConfigDict(strict=True)on the model to apply it to every field. Corrections or other cases welcome below.