require-pydantic-for-structured-payload
Structured nested FastAPI payloads must be parsed into a named Pydantic model before field access.
Why
A validated outer request does not validate the shape of an open nested mapping; fixed-key reads bypass the route's declared contract.
Fix
Validate the nested value with PayloadModel.model_validate(...) or TypeAdapter(PayloadModel).validate_python(...), then use typed attributes.
Examples
from fastapi import APIRouter
router = APIRouter()
@router.post("/actions")def action(body: RequestModel): payload = body.payload return payload.get("nameOnCard")from fastapi import APIRouter
router = APIRouter()
@router.post("/actions")def action(body: RequestModel): payload = CardPayload.model_validate(body.payload) return payload.name_on_card