Pydantic request bodies
Define a Pydantic model; FastAPI validates the JSON body, raises 422 if invalid.
Code example
class NotebookCellRequest(BaseModel):
code: str = Field(..., min_length=1, max_length=50_000)
timeout_seconds: int = Field(default=120, ge=5, le=600)
cell_id: Optional[str] = None
@router.post('/api/notebook/cell')
async def cell(req: NotebookCellRequest):
run_code(req.code, timeout_seconds=req.timeout_seconds)
How it works
Pydantic is a data-validation library. You declare a class with typed fields; Pydantic validates incoming JSON against the types and constraints (min_length, max_length, ge, le, regex). If validation fails, FastAPI returns a 422 with a detailed error message — you never write manual validation code. The notebook's cell endpoint uses this to cap code at 50k chars and timeout at 5–600s.
🔍 Where this is used in OpenBenchML
Every POST/PUT endpoint. Notebook, convert, auth, models, competitions all use Pydantic request models.