Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.3k
-
First Check
Commit to Help
Example CodefromfastapiimportFastAPIfrompydanticimportBaseModel,EmailStr,Fieldapp=FastAPI()classUserIn(BaseModel):email:EmailStrage:int=Field(default=18,gt=0)not_null:str@app.post("/user/",response_model=UserIn)asyncdefcreate_user(user:UserIn)->UserIn:user.email="invalid email"user.age=-1user.not_null=Nonereturnuser DescriptionThedoc says FastAPI will use this return type to validate the returned data: Input validations work normally at the same time. I'm not sure if this is a bug. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.121.3 Pydantic Version2.12.4 Python Version3.12.7 Additional ContextNo response |
BetaWas this translation helpful?Give feedback.
All reactions
By default, Pydantic would expect to validate the external data received when creating a model, but it will assume that if you set values in your own code, that code is already correct and has correct values.
If you're in a situation where you cannot trust the assignment of the values in the model from inside your same code, you can change the Pydantic model to also validate on assignment:https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.validate_assignment
This way it will raise a validation error that will become a server error right on the first line where the first incorrect value is set.
For example:
fromfastapiimportFastAPIfrompydanticimportBaseModel,E…
Replies: 2 comments 4 replies
-
Reproducible with pure Pydantic: frompydanticimportBaseModel,EmailStr,Field,TypeAdapterclassUserIn(BaseModel):email:EmailStrage:int=Field(default=18,gt=0)not_null:struser=UserIn(email="I@example.com",age=25,not_null="Some value")user.email="invalid email"user.age=-1user.not_null=Nonetype_adapter=TypeAdapter(UserIn)res=type_adapter.validate_python(user,from_attributes=True)print(res)# email='invalid email' age=-1 not_null=None |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Or |
BetaWas this translation helpful?Give feedback.
All reactions
❤️ 1
-
Thanks a lot! |
BetaWas this translation helpful?Give feedback.
All reactions
-
I think we should still do something with this. Maybe add a note in docs that FastAPI with Pydantic V2 will not re-validate response if you return the instance of |
BetaWas this translation helpful?Give feedback.
All reactions
-
By default, Pydantic would expect to validate the external data received when creating a model, but it will assume that if you set values in your own code, that code is already correct and has correct values. If you're in a situation where you cannot trust the assignment of the values in the model from inside your same code, you can change the Pydantic model to also validate on assignment:https://docs.pydantic.dev/latest/api/config/#pydantic.config.ConfigDict.validate_assignment This way it will raise a validation error that will become a server error right on the first line where the first incorrect value is set. For example: fromfastapiimportFastAPIfrompydanticimportBaseModel,EmailStr,Fieldapp=FastAPI()classUserIn(BaseModel):email:EmailStrage:int=Field(default=18,gt=0)not_null:strmodel_config= {"validate_assignment":True}@app.post("/user/",response_model=UserIn)asyncdefcreate_user(user:UserIn)->UserIn:user.email="invalid email"user.age=-1user.not_null=Nonereturnuser Notice the: model_config= {"validate_assignment":True} |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Running the same initial code (without So, we have:
|
BetaWas this translation helpful?Give feedback.


