PythiloLesson 10 of 20 · progress kept in this browser

Separate input names from output names

External data does not always use Python's naming style. An alias maps an external key to a model field without forcing that spelling throughout the program. Validation aliases control reading, while serialization aliases control writing.

AliasChoices is useful during a small, explicit migration: accept either the old or the new input key, then emit one canonical name. Choose precedence deliberately if both appear. Dumping by alias is an explicit operation; ordinary dumps use the Python field names unless configured otherwise.

from pydantic import BaseModel, Field
from pydantic import AliasChoices
class User(BaseModel):
    name: str = Field(validation_alias=
        AliasChoices("name", "displayName"))
print(User(displayName="Ada").name)
Your task

Implement canonical_user(raw): accept either user_id or userId as the integer identifier (user_id wins when both occur). Return {"user_id": id}, and reject a missing or invalid identifier.

The first run loads pydantic into this browser.