Complete the code to import the correct FastAPI class.
from fastapi import [1] app = [1]()
You need to import FastAPI to create the app instance.
Complete the code to define a Pydantic model for error response with a message field.
from pydantic import BaseModel class ErrorResponse([1]): message: str
Pydantic models inherit from BaseModel to define data schemas.
Fix the error in the route decorator to specify the response model for errors.
@app.get("/items/{item_id}", responses={404: {"model": [1]) async def read_item(item_id: int): return {"item_id": item_id}
The responses dictionary uses the error response model class name to describe the error schema.
Fill both blanks to raise an HTTPException with a custom error response model.
from fastapi import HTTPException if not item: raise HTTPException(status_code=[1], detail=[2])
Use status code 404 for not found and a clear message string for detail.
Fill all three blanks to define a route that returns a custom error response model on failure.
from fastapi import status @app.get("/users/{user_id}", responses=[1]: {"model": [2]) async def get_user(user_id: int): if user_id != 1: raise HTTPException(status_code=[3], detail="User not found") return {"user_id": user_id}
Use the status code constant for 404, the error model class, and the same status code for the exception.