Challenge - 5 Problems
FastAPI List and Set Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when validating a list of integers with FastAPI?
Consider this FastAPI endpoint that expects a list of integers. What will be the response if the client sends the JSON body:
{"numbers": [1, 2, "3", 4]}?FastAPI
from fastapi import FastAPI from pydantic import BaseModel from typing import List app = FastAPI() class NumbersModel(BaseModel): numbers: List[int] @app.post("/validate") async def validate_numbers(data: NumbersModel): return {"sum": sum(data.numbers)}
Attempts:
2 left
💡 Hint
Pydantic automatically coerces strings like "3" to int 3 in List[int].
✗ Incorrect
Pydantic performs automatic type coercion for compatible values. The string "3" is converted to int 3, so the list validates as [1, 2, 3, 4] and the sum is 10.
📝 Syntax
intermediate2:00remaining
Which code correctly validates a set of unique strings in FastAPI?
You want to accept a set of unique strings in a POST request body. Which of these Pydantic model definitions is correct?
Attempts:
2 left
💡 Hint
Remember to import the correct type and use proper capitalization.
✗ Incorrect
The correct way is to import Set from typing and use Set[str]. Using lowercase 'set' works in Python 3.9+, but Pydantic recommends typing.Set for clarity. Option A uses lowercase set which is valid in Python 3.9+, but since the question is about correctness in FastAPI with Pydantic, option A is the best practice.
❓ state_output
advanced2:00remaining
What is the output of this FastAPI endpoint when validating a set with duplicates?
Given this endpoint and input, what will be the output?
Input JSON: {"tags": ["python", "fastapi", "python"]}
FastAPI
from fastapi import FastAPI from pydantic import BaseModel from typing import Set app = FastAPI() class TagModel(BaseModel): tags: Set[str] @app.post("/tags") async def get_tags(data: TagModel): return {"count": len(data.tags), "tags": sorted(data.tags)}
Attempts:
2 left
💡 Hint
Think about how sets handle duplicates and how Pydantic converts lists to sets.
✗ Incorrect
Pydantic converts the input list to a set, removing duplicates. So the duplicate "python" is removed, leaving two unique tags.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint raise a validation error?
Examine the code and input. Why does validation fail?
Input JSON: {"values": [1, 2, 3.5]}
Code:
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
app = FastAPI()
class ValuesModel(BaseModel):
values: List[int]
@app.post("/values")
async def values_endpoint(data: ValuesModel):
return {"sum": sum(data.values)}
Attempts:
2 left
💡 Hint
Check the types inside the list and how Pydantic validates them.
✗ Incorrect
Pydantic expects all items to be ints. A float 3.5 is not an int, so validation fails before the endpoint runs.
🧠 Conceptual
expert3:00remaining
How does Pydantic handle default values for sets in models?
Consider this Pydantic model:
class MyModel(BaseModel):
items: Set[str] = set()
What is a potential problem with this code when used in FastAPI endpoints?
Attempts:
2 left
💡 Hint
Think about how mutable default arguments behave in Python functions and classes.
✗ Incorrect
Using a mutable default like set() directly as a default value causes all instances to share the same set, leading to bugs. The correct way is to use default_factory.