# TypeError: Object of type int32 is not JSON serializable

**URL:** <https://discuss.pytorch.org/t/typeerror-object-of-type-int32-is-not-json-serializable/169762>\
**Category:** Uncategorized\
**Created:** [January 6, 2023, 6:54pm UTC](https://discuss.pytorch.org/t/typeerror-object-of-type-int32-is-not-json-serializable/169762 "2023-01-06T18:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattpats](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mattpats](https://discuss.pytorch.org/u/mattpats)\
**Post date:** [January 6, 2023, 6:54pm UTC](https://discuss.pytorch.org/t/typeerror-object-of-type-int32-is-not-json-serializable/169762/1 "2023-01-06T18:54:00Z")

</div>

I am training a TabNetClassifier. My code is largely borrowed from:  
‘[tabnet/census\_example.ipynb at develop · dreamquark-ai/tabnet · GitHub](https://github.com/dreamquark-ai/tabnet/blob/develop/census_example.ipynb)’

Everything is working fine until I try to save the model. When trying to save the model, I get the error: ‘TypeError: Object of type int32 is not JSON serializable’

More details below:

```auto
from pytorch_tabnet.tab_model import TabNetClassifier

tabnet_params = {"cat_idxs": cat_idxs,
                 "cat_dims": cat_dims,
                 "cat_emb_dim": 2,
                 "optimizer_fn": torch.optim.Adam,
                 "optimizer_params": dict(lr=2e-2),
                 "scheduler_params": {"step_size": 50, "gamma": 0.9},
                 "scheduler_fn": torch.optim.lr_scheduler.StepLR,
                 "mask_type": 'entmax'}

clf = TabNetClassifier(**tabnet_params)

clf.fit(
    X_train=X_train, y_train=y_train,
    eval_set=[(X_train, y_train), (X_valid, y_valid)],
    eval_name=['train', 'valid'],
    eval_metric=['auc'],
    max_epochs=50,
    patience=20,
    batch_size=1024,
    virtual_batch_size=128,
    num_workers=0,
    weights=1,
    drop_last=False)

```

My model trains fine and I can perform inference with it. However, when I try to save it:

```auto
saving_path_name = "./tabnet_model_test_1"
saved_filepath = clf.save_model(saving_path_name)
>>> TypeError: Object of type int32 is not JSON serializable

```

---

<div class="post-metadata">

**Author:** ![rinkujadhav2013](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rinkujadhav2013/32/54900_2.png) [@rinkujadhav2013](https://discuss.pytorch.org/u/rinkujadhav2013)\
**Post date:** [January 7, 2023, 1:39am UTC](https://discuss.pytorch.org/t/typeerror-object-of-type-int32-is-not-json-serializable/169762/2 "2023-01-07T01:39:43Z")

</div>

Maybe have a look here:

> <https://github.com/dreamquark-ai/tabnet/issues/424#issuecomment-1331885283>
>
> \`numpy.bool\_\` types are not being correctly serialized to json.
> 
> \*\*What is the… current behavior?\*\*
> The \`ComplexEncoder\` class (\[here\](https://github.com/dreamquark-ai/tabnet/blob/4fa545da50796f0d16f49d0cb476d5a30c2a27c1/pytorch\_tabnet/utils.py#L336)) does not handle \`numpy.bool\_\` which is not JSON serializable. This raises a TypeError when saving certain models.
> 
> \*\*If the current behavior is a bug, please provide the steps to reproduce.\*\*
> \`\`\`python
> model = TabNetClassifier(...)
> model.fit(...) # training data and model parameters contain values of type numpy.bool\_
> model.save\_model('path/to/model')
> \`\`\`
> 
> \*\*Expected behavior\*\*
> \`numpy.bool\_\` should be cast to python's \`bool\` before being serialized to JSON. Here is my suggested fix. Please let me know if this is acceptable for a PR:
> 
> \`\`\`python
> class ComplexEncoder(json.JSONEncoder):
> def default(self, obj):
> if isinstance(obj, np.int64):
> return int(obj)
> if isinstance(obj, np.bool\_):
> return bool(obj)
> # Let the base class default method raise the TypeError
> return json.JSONEncoder.default(self, obj)
> \`\`\`
> 
> \*\*Other relevant information:\*\*
> poetry version: "poetry-core\>=1.0.0"
> python version: "^3.9"
> Operating System: "Linux Kernel 5.18.14-arch1-1"
> Additional tools:
> CUDA Version: 11.7
> Driver Version: 515.57
> 
> \*\*Additional context\*\*
> 
> Here's a stacktrace:
> \`\`\`bash
> File ".venv/lib/python3.10/site-packages/pytorch\_tabnet/abstract\_model.py", line 375, in save\_model
> json.dump(saved\_params, f, cls=ComplexEncoder)
> File "/usr/lib/python3.10/json/\_\_init\_\_.py", line 179, in dump
> for chunk in iterable:
> File "/usr/lib/python3.10/json/encoder.py", line 431, in \_iterencode
> yield from \_iterencode\_dict(o, \_current\_indent\_level)
> File "/usr/lib/python3.10/json/encoder.py", line 405, in \_iterencode\_dict
> yield from chunks
> File "/usr/lib/python3.10/json/encoder.py", line 405, in \_iterencode\_dict
> yield from chunks
> File "/usr/lib/python3.10/json/encoder.py", line 405, in \_iterencode\_dict
> yield from chunks
> File "/usr/lib/python3.10/json/encoder.py", line 438, in \_iterencode
> o = \_default(o)
> File ".venv/lib/python3.10/site-packages/pytorch\_tabnet/utils.py", line 339, in default
> return json.JSONEncoder.default(self, obj)
> File "/usr/lib/python3.10/json/encoder.py", line 179, in default
> raise TypeError(f'Object of type {o.\_\_class\_\_.\_\_name\_\_} '
> TypeError: Object of type bool\_ is not JSON serializable
> \`\`\`
> 
> I ran into this when trying tabnet in a \[kaggle competition\](https://www.kaggle.com/competitions/amex-default-prediction). If you need to, you can look \[here in my code\](https://github.com/nishaq503/amex/blob/b4dabc441581a6f29318fcbd2a83ec5376bc1f5a/amex/models/tabnet/training.py#L121) where the error happens.

and here:

> <https://stackoverflow.com/questions/49795525/typeerror-object-of-type-int32-is-not-json-serializable/49797251#49797251>

---

<div class="post-metadata">

**Author:** ![Magill\_Anway](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/magill_anway/32/79999_2.png) [@Magill\_Anway](https://discuss.pytorch.org/u/Magill_Anway)\
**Post date:** [September 5, 2026, 9:26am UTC](https://discuss.pytorch.org/t/typeerror-object-of-type-int32-is-not-json-serializable/169762/3 "2026-09-05T09:26:03Z")

</div>

The failing value is likely a NumPy scalar (for example numpy.int32), which the standard json encoder does not know. Convert NumPy scalars at the boundary with int(value) or value.item(); use .tolist() for NumPy arrays, and .item()/.tolist() for scalar or tensor values. For nested params, recursively normalize dicts and lists before json.dump. To quickly inspect the normalized output without uploading it, this browser-local JSON formatter is useful: [JSON Formatter | Free Online Tool | Lizely](https://www.lizecheng.net/dev/json-formatter/?utm_source=discuss.pytorch.org&utm_medium=forum&utm_campaign=lizely_track1&utm_content=json-formatter)
