Skip to content

Loading old binary xgboost model in version 3.1 raises error that is hard to understand. Perhaps it should tell the user to resave the model? #11758

@ikrommyd

Description

@ikrommyd

Edit:

This seems to come from the fact that the original model file was in the old binary format that xgboost 3.1 removed.
I'm therefore changing this issue to a "feature request" of a better error message.

Original issue:

I have a test model that we use in ci that could be loaded fine until version 3.1.
In version 3.1, I get the following error:

In [1]: import xgboost

In [2]: xgboost.__version__
Out[2]: '3.1.0'

In [3]: xgboost.Booster(model_file="/tmp/xgboost_example.xgb")
---------------------------------------------------------------------------
XGBoostError                              Traceback (most recent call last)
Cell In[3], line 1
----> 1 xgboost.Booster(model_file="/tmp/xgboost_example.xgb")

File ~/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/core.py:2019, in Booster.__init__(self, params, cache, model_file)
   2017     self.__dict__.update(state)
   2018 elif isinstance(model_file, (str, os.PathLike, bytearray)):
-> 2019     self.load_model(model_file)
   2020 elif model_file is None:
   2021     pass

File ~/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/core.py:3052, in Booster.load_model(self, fname)
   3049 if isinstance(fname, str) or is_pathlike(fname):
   3050     # assume file name, cannot use os.path.exist to check, file can be from URL.
   3051     fname = os.fspath(os.path.expanduser(fname))
-> 3052     _check_call(_LIB.XGBoosterLoadModel(self.handle, c_str(fname)))
   3053 elif isinstance(fname, bytearray):
   3054     buf = fname

File ~/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/core.py:323, in _check_call(ret)
    312 """Check the return value of C API call
    313
    314 This function will raise exception when error occurs.
   (...)    320     return value from API calls
    321 """
    322 if ret != 0:
--> 323     raise XGBoostError(py_str(_LIB.XGBGetLastError()))

XGBoostError: [14:45:01] /workspace/src/c_api/c_api.cc:1529: Check failed: str[0] == '{' (b vs. {) :
Stack trace:
  [bt] (0) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/lib/libxgboost.so(+0x10d95e) [0x7f34cab0d95e]
  [bt] (1) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/lib/libxgboost.so(+0x7e63b) [0x7f34caa7e63b]
  [bt] (2) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/lib/libxgboost.so(XGBoosterLoadModel+0xf0) [0x7f34caa7e7d0]
  [bt] (3) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/lib-dynload/../../libffi.so.8(+0x6d8a) [0x7f35047fad8a]
  [bt] (4) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/lib-dynload/../../libffi.so.8(+0x61cd) [0x7f35047fa1cd]
  [bt] (5) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/lib-dynload/../../libffi.so.8(ffi_call+0xcd) [0x7f35047fa91d]
  [bt] (6) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/lib-dynload/_ctypes.cpython-313-x86_64-linux-gnu.so(+0x15fd0) [0x7f3504e0ffd0]
  [bt] (7) /home/iason/micromamba/envs/awkward-coffea/lib/python3.13/lib-dynload/_ctypes.cpython-313-x86_64-linux-gnu.so(+0x13d46) [0x7f3504e0dd46]
  [bt] (8) /home/iason/micromamba/envs/awkward-coffea/bin/python3.13(_PyObject_MakeTpCall+0x27c) [0x558eac7fbdbc]

Resaving the model using 3.0.5 and loading it in 3.1 works. See below:

coffea on  master [?] via  v3.13.9 via  awkward-coffea took 30s
✦ ❯ uv pip install 'xgboost-cpu < 3.1'
Using Python 3.13.9 environment at: /home/iason/micromamba/envs/awkward-coffea
Resolved 3 packages in 106ms
Uninstalled 1 package in 2ms
Installed 1 package in 3ms
 - xgboost-cpu==3.1.0
 + xgboost-cpu==3.0.5

coffea on  master [?] via  v3.13.9 via  awkward-coffea
✦ ❯ ipython
Python 3.13.9 | packaged by conda-forge | (main, Oct 16 2025, 10:31:39) [GCC 14.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 9.6.0 -- An enhanced Interactive Python. Type '?' for help.
Tip: IPython supports combining unicode identifiers, eg F\vec<tab> will become F⃗, useful for physics equations. Play with \dot \ddot and others.

In [1]: import xgboost

In [2]: booster = xgboost.Booster(model_file="/tmp/xgboost_example.xgb")

In [3]: booster.save_model("/tmp/xgboost_example_resaved.xgb")
<ipython-input-3-af3c0b0d19ad>:1: UserWarning: [14:46:45] WARNING: /workspace/src/c_api/c_api.cc:1427: Saving model in the UBJSON format as default.  You can use file extension: `json`, `ubj` or `deprecated` to choose between formats.
  booster.save_model("/tmp/xgboost_example_resaved.xgb")

In [4]: exit

coffea on  master [?] via  v3.13.9 via  awkward-coffea took 1m36s
✦ ❯ uv pip install -U xgboost-cpu
Using Python 3.13.9 environment at: /home/iason/micromamba/envs/awkward-coffea
Resolved 3 packages in 112ms
Prepared 1 package in 0.13ms
Uninstalled 1 package in 1ms
Installed 1 package in 3ms
 - xgboost-cpu==3.0.5
 + xgboost-cpu==3.1.0

coffea on  master [?] via  v3.13.9 via  awkward-coffea
✦ ❯ ipython
Python 3.13.9 | packaged by conda-forge | (main, Oct 16 2025, 10:31:39) [GCC 14.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 9.6.0 -- An enhanced Interactive Python. Type '?' for help.
Tip: You can find how to type a LaTeX symbol by back-completing it, eg `\θ<tab>` will expand to `\theta`.

In [1]: import xgboost

In [2]: xgboost.Booster(model_file="/tmp/xgboost_example_resaved.xgb")
/home/iason/micromamba/envs/awkward-coffea/lib/python3.13/site-packages/xgboost/core.py:2019: UserWarning: [14:47:15] WARNING: /workspace/src/c_api/c_api.cc:1511: Unknown file format: `xgb`. Using UBJSON (`ubj`) as a guess.
  self.load_model(model_file)
Out[2]: <xgboost.core.Booster at 0x7f63733f6270>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions