0
0
FastAPIframework~10 mins

Example data in schema in FastAPI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Example data in schema
Define Pydantic Schema
Add example data in Config
Use schema in FastAPI endpoint
Auto-generate docs show example
Client sees example data in docs
This flow shows how to add example data to a Pydantic schema and how FastAPI uses it to show examples in API docs.
Execution Sample
FastAPI
from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int

    class Config:
        schema_extra = {"example": {"name": "Alice", "age": 30}}
Defines a User schema with example data that FastAPI will show in API docs.
Execution Table
StepActionEvaluationResult
1Define User schema with fields name and ageClass createdUser schema ready
2Add example data in Config.schema_extraExample attached{"name": "Alice", "age": 30} available
3Use User schema in FastAPI endpointEndpoint accepts UserExample shown in docs
4Client opens docsDocs render exampleExample data visible to client
5Client sends request with example dataRequest acceptedServer processes User data
6End of flowNo more stepsExecution stops
💡 All steps completed, example data is used in docs and requests
Variable Tracker
VariableStartAfter Step 2After Step 3Final
User.schema_extraNone{"example": {"name": "Alice", "age": 30}}{"example": {"name": "Alice", "age": 30}}{"example": {"name": "Alice", "age": 30}}
FastAPI endpointNot definedNot definedUses User schemaUses User schema with example
Key Moments - 2 Insights
Why does the example data appear in the API docs?
Because the example is set inside the schema's Config.schema_extra, FastAPI reads it and shows it automatically in the docs (see execution_table step 4).
Does the example data affect the actual data validation?
No, example data is only for documentation and testing help. Validation uses the schema fields, not the example (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the example data attached to the schema?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check the 'Action' column for when example data is added to Config.schema_extra.
According to the variable tracker, what is the value of User.schema_extra after step 3?
A{"example": {"name": "Alice", "age": 30}}
BNone
CEmpty dictionary
DUndefined
💡 Hint
Look at the 'After Step 3' column for User.schema_extra in variable_tracker.
If we remove the example from Config, what will happen in the docs?
ADocs will break and not load
BDocs will show no example data
CDocs will show the last example data anyway
DDocs will show an error message
💡 Hint
Refer to the concept_flow where example data is added to show in docs.
Concept Snapshot
Define a Pydantic schema with fields
Add example data inside Config.schema_extra
Use schema in FastAPI endpoint
FastAPI auto-shows example in docs
Example helps users understand API input
Example does not affect validation
Full Transcript
This visual execution shows how to add example data to a Pydantic schema in FastAPI. First, you define a schema class with fields like name and age. Then, inside the Config class, you add schema_extra with an example dictionary. When you use this schema in a FastAPI endpoint, the example data automatically appears in the generated API documentation. This helps clients see sample input data. The example data is only for docs and does not affect how FastAPI validates incoming requests. The execution table traces each step from defining the schema to the client seeing the example in docs. The variable tracker shows how the example data is stored in the schema. Key moments clarify common confusions about example usage and validation. The quiz tests understanding of when and how example data is attached and used.