Skip to content

Serialize NaN and Infinity values as strings for Hamilton UI #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Phrogz
Copy link

@Phrogz Phrogz commented May 20, 2025

Fix it so that NaN and Infinity floats do not throw an error when being passed to Hamilton UI.

Changes

This commit uses json.dumps(…, allow_nan=False), which raises a ValueError for such values, and in that case converts them to strings.

The result is that Hamliton UI will show such values as strings "nan", "inf", and "-inf".
This could be confusing only to users who are also using such string values.

How I tested this

Created failing unit tests, patched the code, ensured the failing tests now pass.

Ran test code from #1310 and confirmed that runs made with this fix show up, they just show "nan" as the input value for the run.

Ran all unit tests; while some fail, they also failed before this fix.

Notes

Float values that are "out of range" were previously serialized—to send to the tracker/Hamilton UI—in a way that produced invalid JSON (but value JS), which caused a runtime error trying to send values to the tracker.

math.nan, math.inf, and -math.inf are serialized by json.dumps() to NaN, Infinity, and -Infinity values by default(NOT as strings).

Fixes #1310

Checklist

  • PR has an informative and human-readable title (this will be pulled into the release notes)
  • Changes are limited to a single goal (no scope creep)
  • Code passed the pre-commit check & code is left cleaner/nicer than when first encountered.
  • Any change in functionality is tested
  • New functions are documented (with a description, list of inputs, and expected output)
  • Placeholder code is flagged / future TODOs are captured in comments
  • Project documentation has been updated if adding/changing functionality.

Float values that are "out of range" were previously serialized—to send to the
tracker/Hamilton UI—in a way that produced invalid JSON (but value JS), which
caused a runtime error trying to send values to the tracker.

`math.nan`, `math.inf`, and `-math.inf` are serialized by `json.dumps()` to
`NaN`, `Infinity`, and `-Infinity` values by default(NOT as strings).

This commit uses `json.dumps(…, allow_nan=False)`, which raises a `ValueError`
for such values, and in that case converts them to strings.

The result is that Hamliton UI will show such values as **strings**
`"nan"`, `"inf"`, and `"-inf"`.
This could be confusing only to users who are also using such string values.

Fixes apache#1310
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NaN values kill Hamilton UI (can't serialize to JSON)
1 participant