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

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

Описание и применение

Python json.dumps по умолчанию записывает NaN, что приводит к ошибкам JSON.parse в браузере и ломает pipeline отчётов. Решение: использовать 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