Complete worked example
python
from typing import Annotated
from pydantic import Field, TypeAdapter, ValidationError
Item = Annotated[int, Field(strict=True, ge=1)]
Batch = Annotated[list[Item], Field(max_length=3)]
adapter = TypeAdapter(Batch)
def inspect_example(payload):
try:
values = adapter.validate_python(payload)
return {"value": values}
except ValidationError as error:
return {"error": sorted({e["type"] for e in error.errors()})}- Reasoning
- The Item alias rejects booleans, numeric strings and values below one. Batch adds the collection limit, and adapter.validate_python checks the complete list. A valid list is returned in its submitted order so a caller can relate each accepted item to its original position. Test a valid pair, a negative item and a numeric string. The last two fail different parts of the contract. An empty list remains permitted in the baseline; the contract has no minimum length. Do not infer additional policies from the word batch when the annotation does not declare them.