Validate a type without a wrapper model
Not every boundary is a named record. A JSON endpoint might return a list of integers or a mapping of names to counts. TypeAdapter validates those existing Python type expressions without inventing an artificial BaseModel field around them.
Create an adapter once when repeatedly validating the same type. It can validate Python objects or JSON text, and can also serialize values. Validation of raw JSON and serialization of an already trusted value are different operations; do not assume dumping a bad value performs validation first.
from pydantic import TypeAdapter
adapter = TypeAdapter(dict[str, int])
print(adapter.validate_json(
'{"tea": "2", "water": 1}'))Your task
Implement parse_counts(text) using TypeAdapter(dict[str,int]).validate_json. Return a dictionary of integer counts. Reject malformed JSON, nonobject JSON and values that cannot become integers.
The explanation opens here once every check passes.