Pydantic v2 with v1 compatibility
Problem
When using fastapi-pagination with Pydantic V1 models in response_model, a validation error occurs during response serialization.
Versions
- Python: 3.9.10
- FastAPI: 0.119.0
- fastapi-pagination: 0.9.1
- Pydantic: 2.4.2 (but models are created using
pydantic.v1)
Details
I am using Page[PydanticV1Model] in the response_model of a FastAPI endpoint. The models are defined via pydantic.v1.BaseModel, while Page from fastapi-pagination uses the Pydantic V2 API. When validating the response, a conflict arises between V1 and V2.
Example
from fastapi import FastAPI
from fastapi_pagination import Page, create_page
from fastapi_pagination.default import Params
from pydantic.v1 import BaseModel
app = FastAPI()
class MyModel(BaseModel): # Pydantic V1 model
id: int
name: str
@app.get("/items", response_model=Page[MyModel])
async def get_items(page_params: Params = Depends()):
items = [MyModel(id=1, name="test")]
return create_page(items, total=1, params=page_params)
Error
TypeError: validate() takes 2 positional arguments but 3 were given
Traceback (most recent call last):
File ".../fastapi/routing.py", line 242, in serialize_response
value, errors_ = field.validate(response_content, {}, loc=("response",))
File ".../fastapi/_compat/v2.py", line 115, in validate
self._type_adapter.validate_python(value, from_attributes=True),
File ".../pydantic/type_adapter.py", line 441, in validate_python
return self.validator.validate_python(
File ".../pydantic/_internal/_validators.py", line 53, in sequence_validator
v_list = validator(input_value)
TypeError: validate() takes 2 positional arguments but 3 were given
I also get a warning:
UserWarning: Mixing V1 models and V2 models (or constructs, like `TypeAdapter`) is not supported.
Please upgrade `MyModel` to V2.
Questions
- Which version of
fastapi-paginationofficially supports Pydantic V1 models? - Is there a way to use
Pagewith Pydantic V1 models without migrating to V2?
Hi @roflatelie ,
It's an interesting use case, I will take a look 👀
@roflatelie Update from my side.
After a quick look, there is no easy way to do it now. You can either work only with pydantic v2 models, or with pydantic v1 with pydanic<2 installed
I will try to come up with smth better, but it might be tricky
- Which version of fastapi-pagination officially supports Pydantic V1 models?
@roflatelie Latest version supports Pydantic V1 models, but you need to have pydantic v1 installed on your system, not pydantic v2 with BaseModel import from pydantic.v1.
pip install 'pydantic<2'
Hi @roflatelie,
This issue should be fixed in new version 0.15.1.
Here is example on how to use pydantic v1:
from fastapi import FastAPI
from fastapi_pagination import add_pagination, Page, paginate
from fastapi_pagination.customization import CustomizedPage, UsePydanticV1
from pydantic.v1 import BaseModel
app = FastAPI()
add_pagination(app)
PageV1 = CustomizedPage[
Page,
UsePydanticV1(),
]
class UserOut(BaseModel):
id: int
name: str
USERS = [
{"id": 0, "name": "John"},
{"id": 1, "name": "Jane"},
{"id": 2, "name": "Doe"},
]
@app.get("/users")
def get_users() -> PageV1[UserOut]:
return paginate(USERS, safe=True)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app)
I'm closing this issue, please reopen it if issue is still present on your side 🙏