fastapi response_model 사용 시 validation error

fastapi에서 response_model을 지정하여 return 하는 경우 아래와 같은 에러가 발생할 수 있다.

pydantic.error_wrappers.ValidationError: 1 validation error for BaseResponseOut[List[~UserListOut]]
response -> data -> 0 -> created_at

이러한 경우 response model의 type이 잘못 되어 Model과 실제 return하는 객체가 다르게 타입 매핑 되는 경우이므로 정확한 type 지정이 필요하다. 예를들어 created_at 이라는 필드를 리턴하는데 이를 string type으로 리턴하면 에러, datetime으로 정확히 설정하면 정상 동작한다.

Leave a Reply