PythiloLesson 16 of 20 · progress kept in this browser

Give a root collection model behaviour

A RootModel wraps one value at the root instead of declaring an object with several named fields. Its root may be a list, a dictionary or another supported type. Unlike an ordinary model with a field called root, its JSON representation is the root value itself.

Use it when a collection deserves a model's validation methods or additional behaviour but the external format must remain a bare collection. Access the parsed collection through .root. Choose TypeAdapter instead if you only need validation and no named model behaviour.

from pydantic import RootModel
class Tags(RootModel[list[str]]):
    pass
tags = Tags.model_validate(["tea", "new"])
print(tags.root)
print(tags.model_dump_json())
Your task

Implement root_numbers(raw) with a RootModel[list[int]]. Return (the sum of .root, the dumped collection). Empty input returns (0, []). Reject an invalid member.

The first run loads pydantic into this browser.