Skip to content

fix: write numpy scalar settings as Python types in model_to_json - #2787

Open
renuprasads wants to merge 1 commit into
meta:mainfrom
renuprasads:fix/serialize-numpy-scalars
Open

renuprasads wants to merge 1 commit into
meta:mainfrom
renuprasads:fix/serialize-numpy-scalars

Conversation

@renuprasads

Copy link
Copy Markdown

LLM generated content, by Claude Opus 5.5. Written jointly: I directed the work and its validation, and ran the tests myself before submitting.

Fixes #1848.

model_to_json fails with TypeError: Object of type int64 is not JSON serializable when a setting was passed as a numpy integer, for example a value taken from a hyperparameter grid built with numpy, or read from a row of a dataframe of tuning results:

m = Prophet(n_changepoints=np.int64(10))
m.fit(df)
model_to_json(m)  # TypeError: Object of type int64 is not JSON serializable

The same happens for uncertainty_samples, a seasonality's fourier_order and a regressor's prior_scale. Prophet stores these values as given and json.dumps receives them unchanged. np.float64 values already work because that type subclasses Python float, but np.int64 does not. #1701 fixed the same problem for the warm-start arrays in fit_kwargs, and this covers the remaining attributes.

model_to_dict now converts numpy scalars anywhere in its output to the matching Python type before returning, so both model_to_json and json.dumps(model_to_dict(m)) work. It builds new dicts and lists, so the model's own attributes are not modified. model_from_json needs no change.

Tests: test_serialize_numpy_scalar_arguments fits a model with n_changepoints, uncertainty_samples, a seasonality fourier_order and a regressor prior_scale given as np.int64, round-trips it through JSON, checks the restored settings, checks that the original model still holds its np.int64 value, and checks that both models give the same yhat. It fails on main with the error above and passes with the change. The rest of prophet/tests passes (144 passed, 2 slow tests skipped) with pandas 3.1.1 and numpy 2.5.5, and pyrefly check reports 0 errors.

Settings passed as numpy integers, such as n_changepoints=np.int64(10)
or a seasonality's fourier_order, made model_to_json fail with
"Object of type int64 is not JSON serializable". model_to_dict now
converts numpy scalars in its output to Python types.

Fixes meta#1848

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Eu1cgTzRsh9Er7dKH889T8
@meta-cla

meta-cla Bot commented Nov 16, 2026

Copy link
Copy Markdown

Hi @renuprasads!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Nov 16, 2026
@meta-cla

meta-cla Bot commented Nov 16, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model_to_json raises Object of type 'int64' is not JSON serializable on Model object

1 participant