python data_error ai_generated true

ValueError: Out of range float values are not JSON compliant

ID: python/flask-jsonify-nan-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2025-03-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

Flask 的 jsonify 无法处理 NaN 或 Infinity 浮点值。

generic

中文

Flask 的 jsonify 无法处理 NaN 或 Infinity 浮点值。

Workarounds

  1. 95% success
    使用 math.isnan() 检查并替换为 None
  2. 80% success
    配置 Flask 的 JSONProvider 允许 NaN,如 app.json.ensure_ascii = False
  3. 85% success
    使用自定义 JSONEncoder 覆盖 default 方法

Dead Ends

Common approaches that don't work:

  1. 90% fail

    JSON 标准不支持 Infinity。

  2. 60% fail

    客户端可能期望数值类型。

  3. 80% fail

    导致响应失败。