In #201 and #208, I switched us over from using Pydantic's validate_json to validate_python. However, there's a subtlety. From the docs:
It also handles constructing the correct Python type even in strict mode, where validate_python(json.loads(json_data)) would fail validation.
That might sound surprising if you don't catch what happens at each step. The subtlety here is that by the time data is loaded up from json.loads, it comes in as a dict. Under strict=True, that dict is not compatible with any type other than certain kinds of dicts and TypedDicts. So
In #201 and #208, I switched us over from using Pydantic's
validate_json
tovalidate_python
. However, there's a subtlety. From the docs:That might sound surprising if you don't catch what happens at each step. The subtlety here is that by the time data is loaded up from
json.loads
, it comes in as adict
. Understrict=True
, thatdict
is not compatible with any type other than certain kinds ofdict
s andTypedDict
s. SoPydantic already has issues at https://github.com/pydantic/pydantic-core/issues/712 and https://github.com/pydantic/pydantic/issues/9009 to enable the same flexibilities that
validate_json
has, but for now the recommended workaround is to reserialize and usevalidate_json
.