Challenge - 5 Problems
FastAPI Example Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI schema example?
Given this Pydantic model with example data, what will the OpenAPI schema show for the example?
FastAPI
from pydantic import BaseModel, Field class User(BaseModel): id: int = Field(..., example=123) name: str = Field(..., example="Alice") user_example = User.schema()['properties']
Attempts:
2 left
💡 Hint
Look at the Field example values and their types.
✗ Incorrect
The example values in the schema match the Field example arguments with correct types.
📝 Syntax
intermediate2:00remaining
Which option correctly sets example data in a Pydantic schema?
Choose the code snippet that correctly uses Field to add example data for a Pydantic model.
Attempts:
2 left
💡 Hint
Example should match the field type and use ... for required fields.
✗ Incorrect
Option A correctly uses Field with required marker ... and example as a float matching the type.
🔧 Debug
advanced2:00remaining
What error occurs with this example data in schema?
This Pydantic model has example data that does not match the field type. What error will FastAPI raise when generating docs?
FastAPI
from pydantic import BaseModel, Field class Product(BaseModel): quantity: int = Field(..., example="ten")
Attempts:
2 left
💡 Hint
Example must match the declared field type.
✗ Incorrect
Pydantic validates example values against the field type and raises ValidationError if mismatched.
❓ state_output
advanced2:00remaining
What is the example output in OpenAPI for nested Pydantic models?
Given nested models with example data, what does the OpenAPI example show for the nested field?
FastAPI
from pydantic import BaseModel, Field class Address(BaseModel): city: str = Field(..., example="Paris") class User(BaseModel): name: str = Field(..., example="Bob") address: Address = Field(...) user_schema = User.schema()
Attempts:
2 left
💡 Hint
Nested models use $ref in schema, examples are inside referenced model.
✗ Incorrect
The User schema references Address by $ref; example for nested fields is inside Address schema.
🧠 Conceptual
expert2:00remaining
How does FastAPI use example data in schema for API docs?
Why is setting example data in Pydantic models important for FastAPI-generated OpenAPI docs?
Attempts:
2 left
💡 Hint
Think about the purpose of examples in documentation.
✗ Incorrect
Examples improve API docs by showing sample data, making it easier for users to test and understand endpoints.