Complete worked example
python
from pydantic import BaseModel, Field, ValidationError
class Role(BaseModel):
title: str
slots: int
def inspect_example(payload):
try:
role = Role.model_validate(payload)
return {"ok": True, "role": role.model_dump()}
except ValidationError as error:
return {"ok": False, "types": sorted({e["type"] for e in error.errors()})}- Reasoning
- The Role class names two fields. inspect_example sends the mapping through model_validate and then returns model_dump, making the converted values visible to the fixture. ValidationError is caught only to report stable error types; arbitrary internal exceptions are not transformed into a successful result. Follow the slots value across the boundary: external text arrives, integer validation runs, then the output mapping contains an integer. A nonsensical value such as "many" never becomes a valid Role. The original input mapping remains unchanged. The lab checks that immutability separately from the expected return value.