microsoft / TypeChat

TypeChat is a library that makes it easy to build natural language interfaces using types.
https://microsoft.github.io/TypeChat/
MIT License
8.06k stars 378 forks source link

Fix validation against dataclasses #212

Closed DanielRosenwasser closed 3 months ago

DanielRosenwasser commented 3 months ago

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

Pydantic 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 use validate_json.