Discover how a tiny addition to your schema can make your API docs shine effortlessly!
Why Example data in schema in FastAPI? - Purpose & Use Cases
Imagine you are building an API and want to show users what kind of data your endpoints expect or return. You try to write this example data manually in your documentation or comments.
Manually writing example data is easy to get wrong, often outdated, and requires extra work every time your data model changes. This leads to confusing docs and frustrated users.
FastAPI lets you add example data directly inside your schema definitions. This keeps examples accurate, up-to-date, and automatically shown in your API docs.
"""User data: { 'id': 1, 'name': 'Alice' }"""
class User(BaseModel): id: int name: str class Config: schema_extra = { "example": {"id": 1, "name": "Alice"} }
This makes your API self-explanatory and easier to use, saving time and avoiding mistakes.
When a developer visits your API docs, they instantly see example user data, so they know exactly what to send or expect back.
Manual example data is error-prone and hard to maintain.
FastAPI schemas can include example data directly.
API docs become clearer and more reliable automatically.
Practice
example data in a FastAPI schema field?Solution
Step 1: Understand the role of example data in FastAPI
Example data is used to provide sample values in API documentation, helping users know what to send or expect.Step 2: Differentiate example data from validation or encryption
Example data does not validate or encrypt; it only shows sample values in docs.Final Answer:
To show sample values for API fields to help users understand expected data -> Option DQuick Check:
Example data = sample values [OK]
- Confusing example with validation
- Thinking example encrypts data
- Assuming example creates database schema
Solution
Step 1: Recall the Field syntax for example data
FastAPI usesField(..., example=...)to add example data to schema fields.Step 2: Identify the correct option
name: str = Field(example="John Doe") usesField(example="John Doe"), which is the correct syntax for example data.Final Answer:
name: str = Field(example="John Doe") -> Option AQuick Check:
Field(example=...) = correct syntax [OK]
- Using default instead of example
- Confusing description with example
- Omitting Field() entirely
from pydantic import BaseModel, Field
class User(BaseModel):
username: str = Field(..., example="alice123")
age: int = Field(..., example=30)What example data will appear in the OpenAPI docs for the
age field?Solution
Step 1: Check the example value assigned to the age field
The age field hasexample=30, which is an integer value 30.Step 2: Understand how example data is shown in docs
FastAPI shows the example exactly as given, so the example for age will be 30 (integer).Final Answer:
30 -> Option BQuick Check:
Example for age = 30 [OK]
- Thinking example converts int to string
- Assuming example is None if not set
- Confusing example with default value
from pydantic import BaseModel, Field
class Item(BaseModel):
name: str = Field(example=123)
price: float = Field(..., example="9.99")Solution
Step 1: Check example data types against field types
The name field is str but example=123 (int) is allowed but not ideal; price is float but example="9.99" is a string, which is incorrect.Step 2: Identify the type mismatch causing error
Example for price should be a float (9.99), not a string "9.99"; this can cause schema confusion.Final Answer:
Example for price should be a float, not a string -> Option CQuick Check:
Example type must match field type [OK]
- Using string example for numeric fields
- Ignoring type mismatch warnings
- Omitting Field(...) for required fields
Solution
Step 1: Understand example data usage in nested models
Each field in nested models needs its own example data usingField(example=...)to show correct samples in docs.Step 2: Confirm nested fields do not inherit example data
Example data is not inherited; you must specify it explicitly for each nested field.Final Answer:
Use example in Field() for each nested field separately -> Option AQuick Check:
Example data must be set per field [OK]
- Assuming nested fields inherit example data
- Skipping example in nested models
- Thinking example data is only for top-level fields
Ready to prove it?
Pass the test and this pattern moves unknown
