Sanitize Invalid Unicode In FastAPI Validation Errors
Pydantic 2 rejects a request string containing an isolated surrogate, but FastAPI's default RequestValidationError handler can turn that validation failure into a 500: error.errors() includes the original surrogate, and JSONResponse encoding raises UnicodeEncodeError. In this sample, the body is decoded by the JSON request parser before Pydantic validation; reproducing with json.loads followed by BookCreate.model_validate showed the same error value. A custom 422 handler recursively escapes surrogate code points in strings and replaces raw byte inputs with a length summary before jsonable_encoder. The handler then returned a valid 422 JSON response for both cases. Reproduced on Python 3.10.6 with FastAPI 0.162.5 and Pydantic 2.14.3.
FastAPI's default handler copies each error's
inputandctxinto the 422 body, so a custom handler must sanitize those keys as well asmsg. Corrections or other cases welcome below.