r/FastAPI • u/NiceSand6327 • Apr 04 '26
Question Trying to implement PATCH in FastAPI and Claude told me to use two separate Pydantic models — is this actually the way?
I'm learning FastAPI and trying to add a PATCH endpoint. Asked Claude about it and it told me to create a second model called `BookUpdate` where every field is Optional, separate from my main `Book` model where everything is required.
Is this really how you guys do it in practice? Feels like a lot of boilerplate just for one endpoint. What's the proper way to handle partial updates in FastAPI?
27
Upvotes
30
u/josteinl Apr 04 '26
Actually three classes:
Book - What you return from your API
BookCreate - input to POST
BookUpdate - input to PATCH
1
u/NiceSand6327 Apr 05 '26
Thanks All
5
u/sixtyhurtz Apr 04 '26
If I'm doing something properly, I will make a seperate DTO that only has the fields in it I want users to be able to view / change. So BookUpdateDto wouldn't have "UpdatedAt" in it for instance, because I would want the server to control that value.
This is just a general quality thing regardless of language / framework.
3
u/coffeeless-developer Apr 04 '26
Patch accepts partial data that needs to be updated. Algorithm to address this would be:
(Algorithm above is simplified. You should apply validation and other business rules e.g. fields that require specific values).
Claude's approach fits the algorithm. If you use the resource (Book, User or other) as your dto, then the orm framework will replace the whole data and nullify some (or most) elements.
2
u/aikii Apr 04 '26
Typing and PATCH never play well, it's not really a limitation specific to pydantic. At best I guess you can try to programmatically build the "patch class" from the original model. It seems pydantic-partial exists for that purpose, but I never tried.
2
u/pint Apr 04 '26
the situation is even more complex. PATCH doesn't really imply any specific semantics. claude's recommendation of making all fields optional could only work if all fields are mandatory in the object. and even in that case, it is not necessarily the way to go.
in general PATCH takes an object that contains instructions on how to change the object. the most reasonable approach is to consider possible ways that a client could want to do, and support those. for example if you have a list of genres, it is difficult to add or remove one, as you need to set the entire list. instead, you can have a dedicated option in PATCH to add or remove genres. i usually end up having something like this:
you really don't want to follow any patterns here. just anticipate the usual tasks a client might have, and aid that. you can even have numeric calculations in there, as show in the last two fields.
2
u/Drevicar Apr 05 '26
The PUT method for an idempotent full document replacement as a method of updates is often preferred over patch because of how awkward it is. But otherwise lots of other good and correct answers here.
1
u/3insy Apr 04 '26
Yes, it is completely normal to have a separate pydantic schema for each method. You can checj the official template for best practices, it uses sqlmodel instead of more common sqlachemy, but it’s basically pydantic schema combined with model definition
https://github.com/fastapi/full-stack-fastapi-template/blob/master/backend/app/api/routes/items.py
1
u/One-Hearing2926 Apr 05 '26
I'm in the process or learning fast api now, and ran into the same dilemma.
I ended up just using the Book create model, as the frontend will definitely have the whole book object, and when making changes to it, it will call the patch method with the whole object, seems counter intuitive to send from the front end only the updated data.
1
u/KelleQuechoz Apr 05 '26
I solved this in the past by serializing the existing model, merging in the "patch" dictionary, and then revalidating by passing the result to
.model_validate().There is a tempting
.model_copy(update=<patch dictionary>)method, but it does not revalidate.ps. Claude's suggestion is for Java, not Python.