Pydantic's TypeAdapter has dump_python/dump_json (to serialize) and validate_python/validate_json to validate the annotations.
At the moment the BaseModelTransform/SerializationTransform only supports annotating BaseModel or list[BaseModel]. If we switch these to use TypeAdapter we could probably directly use TypeAdapter(annotation).validate_python (or validate_json) to also support validating/serializing annotations like dict[str, BaseModel], or more complicated types.
Thoughts on this suggestion? Happy to give it a attempt at a PR depending on feedback
Pydantic's
TypeAdapterhasdump_python/dump_json(to serialize) andvalidate_python/validate_jsonto validate the annotations.At the moment the
BaseModelTransform/SerializationTransformonly supports annotatingBaseModelorlist[BaseModel]. If we switch these to use TypeAdapter we could probably directly useTypeAdapter(annotation).validate_python(orvalidate_json) to also support validating/serializing annotations likedict[str, BaseModel], or more complicated types.Thoughts on this suggestion? Happy to give it a attempt at a PR depending on feedback