Fix FastAPI "Invalid args for response field: check that class is a valid Pydantic field type" error
David Y.
—I’m seeing the error below when I run my FastAPI project:
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'main.User'> is a valid pydantic field type
My code looks like this:
from fastapi import FastAPI app = FastAPI() class User(): firstname: str surname: str age: int @app.post("/create-user/") async def create_user(user: User): # ... user creation operations ... return {"Message": "User created."}
What is going wrong and how do I fix it?
FastAPI expects the values passed into its API route functions to be of types it recognizes. As FastAPI uses Pydantic to validate data types, any types we pass into route functions should inherit from Pydantic’s BaseModel
class. Therefore, we can fix this error by making User
inherit from BaseModel
:
from fastapi import FastAPI from pydantic import BaseModel # import BaseModel app = FastAPI() class User(BaseModel): # inherit BaseModel firstname: str surname: str age: int @app.post("/create-user/") async def create_user(user: User): # ... user creation operations ... return {"Message": "User created."}
As an alternative, we could alter our create_user
function to take the attributes of the User
class as individual parameters:
from fastapi import FastAPI app = FastAPI() class User(): firstname: str surname: str age: int @app.post("/create-user/") async def create_user(firstname: str, surname: str, age: int): # ... user creation operations ... return {"Message": "User created."}
Pydantic already recognizes built-in types such as str
and int
, so this will work as expected. This approach may be useful when working with classes that do not inherit from BaseModel
or when writing functions with inputs that do not correspond directly to a class’s attributes. However, for a function like this one, the first approach would be considered best practice, as it reduces code duplication and allows us to leverage Pydantic’s powerful data validation functionality for our custom class.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.