TIL - Python
[FastAPI] Pydantic field 타입 에러 (fastapi.exceptions.FastAPIError: Invalid args for response field!)
cocokaribou
2023. 7. 19. 12:46
FastAPI 공식 문서를 참고해서 token api를 구현했다.
@app.post("/token")
async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]):
user_dict = fake_users_db.get(form_data.username)
if not user_dict:
raise HTTPException(status_code=400, detail="Incorrect username or password")
user = UserInDB(**user_dict)
hashed_password = fake_hash_password(form_data.password)
if not hashed_password == user.hashed_password:
raise HTTPException(status_code=400, detail="Incorrect username or password")
return {"access_token": user.username, "token_type": "bearer"}
그런데 로컬에서는 빌드가 잘 되는데, 원격 서버에서는 Pydantic 타입 에러가 떴다.
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that typing.Annotated[fastapi.security.oauth2.OAuth2PasswordRequestForm, Depends(NoneType)] is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/
Annotated 안에 인자를 어떻게 바꿔줘도 같은 에러가 떴다.
그러다가 두 서버에 설치된 fastapi 버전을 체크했는데 차이가 있었다.
pip freeze | grep fastapi
fastapi==0.97.0
fastapi==0.93.0
FastAPI requirements.txt에 버전정보를 명시해주자 잘 해결됐다.
fastapi==0.97.0
pymssql
fastapi_pagination
728x90