Get startedGet started for free

Request and response models

1. Request and response models

Now that we know how to serve ML predictions using FastAPI, we will dive deeper to learn how to send and receive data with APIs. In this video, we'll learn to validate request and response formats using Pydantic.

2. Request and response structures in APIs

To communicate data with APIs, we need to understand how to validate requests that are sent by the client to the API. There are different request methods like GET and POST, which we will talk about more in the next video. Each request contains headers which are metadata sent along with the request. Each request can include query parameters and/or a body with data. For example, here we have a request URL that would send a GET request to our localhost server at port 8000, hitting the /users/ endpoint and providing an email address as a query parameter prefixed with a "question mark".

3. Response structure

The FastAPI application processes this request and returns a JSON response payload with user information like username, email and age. The response also contains a status code, headers, and a response body. Here is an example of a response containing information about a user. Along with the status code, it provides additional information about the user who was requested.

4. Pydantic model

Let's learn more about Pydantic models using an example of a User creation process. Recall that Pydantic models are used to maintain well-structured nested models for HTTP transactions. Here, we're defining the shape of a User model. First, we import BaseModel from Pydantic. Our User class inherits from BaseModel, and then we define our attributes using Python type annotations. It's like telling Pydantic, "Hey, I want a username that's a string, an email that's a string, and an age that's an integer." And just like that, Pydantic knows exactly what to expect and how to validate it.

5. Validation error

For example, here we are trying to create a User where we have intentionally specified age thirty as an alphabetic string and we have wrapped up the entire code in a try-except block. Pydantic will catch this error and will return an instance of the ValidationError class describing the issue.

6. Using Pydantic models in FastAPI

Now, let's see this Pydantic model in action with a FastAPI app. First, we create a FastAPI app after importing the required libraries and classes. Then we create a Pydantic model which will be used in our user creation endpoint. Now to create the endpoint, we add an @app.post decorator with the User model as response_model. This is FastAPI's way of saying, "When I send back a response, make sure it also follows the User model." Then we use the same model as a type hint for the user input to the create_user function. The pydantic model provides a defined structure to validate requests. Under the hood, FastAPI also relies on Pydantic models to deserialize and serialize requests and responses, and generate documentation.

7. Request and response formats

To check the request structure, we can send a POST request using cURL. We have specified the headers, content type and then the user details as JSON body. We get automatic request validation and response payload as JSON from the API.

8. Let's practice!

Now that we've learned how Pydantic helps validate requests and serialize responses, let's move on to the exercises and put all this into practice.