microsoft / microsoft/qlib

Critical Bug: Backtest Module Crash Due to Gym Dependency and NumPy 2.0+ Incompatibility​

Open
#2,018 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
48.7k
Forks
7.7k
PR merge metrics
No merged PRs in 30d

Description

🐛 Bug Description

When using the backtestmodule, the application crashes abruptly due to two interconnected issues

  1. ​​Internal gym dependency​​: The backtest module implicitly relies on the gymlibrary, which is not explicitly declared as a dependency or handled gracefully if missing.
  2. ​​NumPy 2.0+ incompatibility​​: The gymversion used (likely gym==0.26.2or earlier) does not support NumPy 2.0+

This results in a ​​hard crash​​ that terminates the entire Python process, including the IDE/kernel, without providing actionable error messages for end-users. It actually happen when i use the WeightStrategyBase class and run the entire programme with backtestmodule instead of workflow.

To Reproduce

Steps to reproduce the behavior:

1.Environment: Python 3.9.23 or 3.13 with NumPy 2.0+ installed.
2.Run a basic backtest script using QLib's backtestmodule(using WeightStrategyBase class )
3.run the backtest with the 'backtest' like situation below :
portfolio_metric_dict, indicator_dict = backtest(
executor=executor_obj,
strategy=strategy_obj,
**backtest_config
)

Expected Behavior

Expected​​: Backtest should run successfully or fail gracefully with a clear error message if dependencies are missing/incompatible.

​​Actual​​: Complete runtime crash with traceback

Screenshot

Image

Gym has been unmaintained since 2022 and does not support NumPy 2.0 amongst other critical functionality.
Please upgrade to Gymnasium, the maintained drop-in replacement of Gym, or contact the authors of your software and request that they upgrade.
Users of this version of Gym should be able to simply replace 'import gym' with 'import gymnasium as gym' in the vast majority of cases.
See the migration guide at https://gymnasium.farama.org/introduction/migration_guide/ for additional information.

and when i run the backtest the whole programme crashed. Then, i have to restart my IDE.

Environment

Note: User could run cd scripts && python collect_info.py all under project directory to get system information
and paste them here directly.

  • Qlib version:0.9.6
  • Python version:3.9.23
  • OS (Windows, Linux, MacOS):Windows
  • Commit number (optional, please provide it if you are using the dev version):

Additional Notes

🛠️ ​​Suggested Fixes​​
​​Replace Gym with Gymnasium​​:

gymis unmaintained since 2022 and incompatible with NumPy 2.0+ .

Migrate to gymnasium(maintained fork) by replacing import gymwith import gymnasium as gym.

​​Declare Explicit Dependencies​​:

Add gymnasium(or gymif retained) to QLib's requirements.txtor setup.py.

​​NumPy Version Locking​​:

Temporarily restrict NumPy to <2.0in dependencies until migration is complete.

​​Error Handling​​:

Add try-catch blocks around gymimports and initialize backtest dependencies lazily.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the crash with WeightStrategyBase and the shown backtest(...) call under Python 3.9.23 and NumPy 2.0+, then inspect the backtest module and the dependency declarations in requirements.txt or setup.py. Use scripts/collect_info.py if environment details are needed; done means the backtest no longer hard-crashes and reports a clear dependency or compatibility error when it cannot run.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.