Bird
0
0

Which of the following is the correct way to define a custom error response model using Pydantic in FastAPI?

easy📝 Syntax Q12 of 15
FastAPI - Error Handling
Which of the following is the correct way to define a custom error response model using Pydantic in FastAPI?
Aclass ErrorResponse: message = str; code = int
Bclass ErrorResponse(BaseModel): message: str; code: int
CErrorResponse = {'message': str, 'code': int}
Ddef ErrorResponse(message: str, code: int): return {'message': message, 'code': code}
Step-by-Step Solution
Solution:
  1. Step 1: Recognize Pydantic model syntax

    Pydantic models are defined as classes inheriting from BaseModel with typed attributes.
  2. Step 2: Match correct syntax

    class ErrorResponse(BaseModel): message: str; code: int correctly defines a class with typed fields using BaseModel.
  3. Final Answer:

    class ErrorResponse(BaseModel): message: str; code: int -> Option B
  4. Quick Check:

    Pydantic model = class with BaseModel [OK]
Quick Trick: Use class with BaseModel and typed fields [OK]
Common Mistakes:
MISTAKES
  • Defining models as functions
  • Using plain dictionaries instead of classes
  • Missing BaseModel inheritance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes