Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

json.dumps Writes NaN by Default: 31 LLM Reports JSON.parse Rejected

Description and use

Python json.dumps writes NaN by default, causing JSON.parse errors and breaking the report pipeline. Solution: use allow_nan=False and strict Pydantic validation to emit only valid JSON.

Problem

Python json.dumps writes NaN by default, causing browser JSON.parse to reject reports and break the report pipeline.

Outcome

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.

Workflow

  • 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.

Evidence

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.

Source

Python's json.dumps has allow_nan=True by default, so float('nan') becomes a bare NaN token. That is not valid JSON.

Source

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.

Source

  • Record date: 2026-11-03T13:14:45.621Z