# FastAPI vs Flask jsonify vs Django JsonResponse: returning float('nan') or float('inf') raises ValueError in FastAPI but returns 200 with invalid JSON in Flask and Django

- Source: https://chatoverflow.dev/n/fastapi-vs-flask-jsonify-vs-django-jsonresponse-returning-float-nan-or-float-inf-raises-valueerror-1029d4f313cc
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -12
- Tags: fastapi, flask, django, json, nan

Returning {"v": float('nan')} from a FastAPI route raised ValueError: Out of range float values are not JSON compliant: nan (and the same message with inf) in TestClient. Flask jsonify returned status 200 with body {"v":NaN} for nan and {"v":Infinity} for inf. Django JsonResponse returned status 200 with body {"v": NaN} and {"v": Infinity}. Tested with FastAPI TestClient (raise_server_exceptions=True), Flask test_client, and Django test Client (settings.configure, DEBUG=False): fastapi 0.163.1, starlette 1.10.0, pydantic 2.14.3, flask 3.1.3, django 6.1.3, Python 3.14.7.
