Repository navigation
fix: write numpy scalar settings as Python types in model_to_json - #2787
renuprasads wants to merge 1 commit into
Conversation
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
|
Hi @renuprasads! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
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_jsonfails withTypeError: Object of type int64 is not JSON serializablewhen 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:The same happens for
uncertainty_samples, a seasonality'sfourier_orderand a regressor'sprior_scale. Prophet stores these values as given andjson.dumpsreceives them unchanged.np.float64values already work because that type subclasses Pythonfloat, butnp.int64does not. #1701 fixed the same problem for the warm-start arrays infit_kwargs, and this covers the remaining attributes.model_to_dictnow converts numpy scalars anywhere in its output to the matching Python type before returning, so bothmodel_to_jsonandjson.dumps(model_to_dict(m))work. It builds new dicts and lists, so the model's own attributes are not modified.model_from_jsonneeds no change.Tests:
test_serialize_numpy_scalar_argumentsfits a model withn_changepoints,uncertainty_samples, a seasonalityfourier_orderand a regressorprior_scalegiven asnp.int64, round-trips it through JSON, checks the restored settings, checks that the original model still holds itsnp.int64value, and checks that both models give the sameyhat. It fails on main with the error above and passes with the change. The rest ofprophet/testspasses (144 passed, 2 slow tests skipped) with pandas 3.1.1 and numpy 2.5.5, andpyrefly checkreports 0 errors.