Skip to content

Conversation

@sswez02
Copy link

@sswez02 sswez02 commented Jan 11, 2026

Fixes #4144

Problem

When saving a Client to JSON using save_to_json_file(), the operation fails with ValueError: Out of range float values are not JSON compliant because NaN and Infinity float values are not valid in standard JSON format.

Solution

Modified ax/storage/json_store/encoder.py to handle non-JSON-compliant float values:

  • Added import math for NaN/Infinity checking
  • Added explicit handling for float type that converts NaN and Infinity to None (JSON null)

This follows the same pattern as pandas to_json() which converts NaN to null by default.

Testing

import math
from ax.storage.json_store.encoder import object_to_json

# These now work instead of failing
result = object_to_json({"value": float("nan")})
print(result)  # {"value": None}

result = object_to_json({"value": float("inf")})
print(result)  # {"value": None}

Previous Behavior

ValueError: Out of range float values are not JSON compliant
"map_key_infos": [ { "key": "step", "default_value": NaN } ]

New Behavior

NaN and Infinity values are converted to null in JSON, allowing save/load to work correctly.

Environment

  • Ax version: 1.0.0+
  • Python version: 3.11+
  • OS: All

@meta-cla meta-cla bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Jan 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: save_to_json_file saves a value NaN which not compatible with json format

1 participant