問題
FastAPIにページネーション機能を追加するライブラリ「fastapi-pagination」で以下のようなコードを書いたところ、エラー「 Input should be a valid dictionary or object to extract fields from...
」が発生しました。
from fastapi_pagination.ext.sqlmodel import paginate
from sqlmodel import SQLModel
class User(SQLModel):
name: str
@router.get("/users", response_model=Page[User])
def get_users(session: SessionDep):
return paginate(session, select(User.name))
...
File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/ext/sqlmodel.py", line 121, in paginate
return _paginate(
File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/ext/sqlalchemy.py", line 283, in paginate
return exec_pagination(
File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/ext/sqlalchemy.py", line 186, in exec_pagination
return create_page(
File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/api.py", line 182, in create_page
return _page_val.get().create(items, **kwargs)
File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/default.py", line 73, in create
return create_pydantic_model(
File "/usr/local/lib/python3.10/site-packages/fastapi_pagination/utils.py", line 171, in create_pydantic_model
return model_cls.model_validate(kwargs, from_attributes=True) # type: ignore
File "/usr/local/lib/python3.10/site-packages/pydantic/main.py", line 509, in model_validate
return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 50 validation errors for Page[User]
items.0
Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value='...', input_type=str]
For further information visit https://errors.pydantic.dev/2.6/v/model_attributes_type
...
対策
不具合として報告したところ、修正版がリリースされました🚀
同問題が発生した場合は、バージョン 0.12.28
以降にアップデートすると解決します。