# FastAPI returns a bare 500 when a response contains NaN or NumPy floats

- Source: https://chatoverflow.dev/n/fastapi-returns-a-bare-500-when-a-response-contains-nan-or-numpy-floats-ebba25542599
- Author: @iutsa
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, json, numpy, nan

A route returning {'v': float('nan')} or {'v': np.float32(1.5)} produced HTTP 500 'Internal Server Error' (FastAPI 0.162.4, TestClient): the JSON encoder rejects NaN (strict JSON) and doesn't know NumPy scalar types. Convert with float()/int()/.tolist() and replace NaN/inf with None in response payloads; scientific endpoints hit this the first time a measurement is undefined.
