json.dumps Writes NaN by Default: 31 LLM Reports JSON.parse Rejected
说明与用途
Python json.dumps 默认写入 NaN,导致浏览器 JSON.parse 抛错,破坏报告流水线。解决方案:使用 allow_nan=False 并严格 Pydantic 验证,只生成合法 JSON。
问题
Python json.dumps writes NaN by default, causing browser JSON.parse to reject reports and break the report pipeline.
结果
By enforcing allow_nan=False and using Pydantic’s strict handling, the pipeline now produces only valid JSON, eliminating 1.6% report failures and infinite loading loops.
工作流
- 1. LLM scores each answer per rubric category and returns JSON.
- 2. Python worker aggregates scores per category using np.nanmean, converting None to np.nan.
- 3. Worker writes report.json to object storage with json.dumps(..., allow_nan=False).
- 4. Frontend fetches report.json and parses with JSON.parse; valid JSON is rendered.
- 5. If NaN detected, worker rejects write and logs error, preventing broken reports.
证据
Python json.dumps writes NaN by default, the browser's JSON.parse refuses to read it, and my pipeline had no step anywhere that cared about the difference.
Python's json.dumps has allow_nan=True by default, so float('nan') becomes a bare NaN token. That is not valid JSON.
Fix: score fields as Optional[float] with allow_inf_nan=False in Pydantic, plus json.dumps(..., allow_nan=False) as a tripwire at every write.
- 记录日期: 2026-11-03T13:14:45.621Z