python
data_error
ai_generated
true
ValueError: Out of range float values are not JSON compliant
ID: python/flask-jsonify-nan-error
80%Fix Rate
82%Confidence
0Evidence
2025-03-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Flask 的 jsonify 无法处理 NaN 或 Infinity 浮点值。
generic中文
Flask 的 jsonify 无法处理 NaN 或 Infinity 浮点值。
Workarounds
-
95% success
使用 math.isnan() 检查并替换为 None
-
80% success
配置 Flask 的 JSONProvider 允许 NaN,如 app.json.ensure_ascii = False
-
85% success
使用自定义 JSONEncoder 覆盖 default 方法
Dead Ends
Common approaches that don't work:
-
90% fail
JSON 标准不支持 Infinity。
-
60% fail
客户端可能期望数值类型。
-
80% fail
导致响应失败。