Get startedGet started for free

POST endpoint for model registration

While the GET endpoint you created earlier allows users to retrieve information about existing models, you now need a way for authorized team members to register new models or update information about existing ones.

You need to create a POST endpoint that allows team members to register new models or update existing ones. This endpoint will store model information on the server.

This exercise is part of the course

Deploying AI into Production with FastAPI

View Course

Exercise instructions

  • Create a POST request endpoint at "/register-model" and specify the status code to indicate successful creation.
  • Define the function parameter model_info to accept and validate the incoming model information using the ModelInfo Pydantic model.
  • Convert the ModelInfo object to a dictionary and store it in the model_db using the model_id as the key.
  • In the return statement, include the stored model information and the status code for successful model registration.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

model_db = {}

class ModelInfo(BaseModel):
    model_id: int
    model_name: str
    description: str

# Specify the status code for successful POST request
@app.____("/register-model", status_code=____)
# Pass the model info from the request as function parameter 
def register_model(____: ModelInfo):
    # Add new model's information dictionary to the model database
    model_db[model_info.model_id] = ____
    # Return model info dictionary corresponding to model along with success status code
    return {"message": "Model registered successfully", "model": ____}, ____
Edit and Run Code