microsoft/qlib · error · ValueError
Cannot find user {}
Error message
Cannot find user {} What it means
Raised by UserManager.save_user_data when the given user_id is not a key in the in-memory self.users dict. Saving serializes the user's account, strategy and model pickles, so it requires the user to have been loaded/created in this UserManager instance first.
Source
Thrown at qlib/contrib/online/manager.py:84
cur_user_list = list(self.users)
if user_id in cur_user_list:
raise ValueError("User {} has been loaded".format(user_id))
else:
trade_account = Account(0)
trade_account.load_account(account_path)
strategy = load_instance(strategy_file)
model = load_instance(model_file)
user = User(account=trade_account, strategy=strategy, model=model)
return user
def save_user_data(self, user_id):
"""
save a instance of User() to user data path
Parameter
user_id : string
"""
if not user_id in self.users:
raise ValueError("Cannot find user {}".format(user_id))
self.users[user_id].account.save_account(self.data_path / user_id)
save_instance(
self.users[user_id].strategy,
self.data_path / user_id / "strategy_{}.pickle".format(user_id),
)
save_instance(
self.users[user_id].model,
self.data_path / user_id / "model_{}.pickle".format(user_id),
)
def add_user(self, user_id, config_file, add_date):
"""
add the new user {user_id} into user data
will create a new folder named "{user_id}" in user data path
Parameter
user_id : string
init_cash : int
config_file : str/pathlib.Path()View on GitHub (pinned to 79633dd950)
Solutions
- Call um.load_users() after constructing UserManager so existing users are in um.users.
- Verify the id: assert user_id in um.users before save_user_data(user_id).
- If the user is new, create it first with create_user/add_user before saving.
Example fix
# before
um = UserManager(user_data_path=p)
um.save_user_data("u1") # ValueError: Cannot find user u1
# after
um = UserManager(user_data_path=p)
um.load_users() # populate um.users from disk
if "u1" in um.users:
um.save_user_data("u1") Defensive patterns
Strategy: validation
Validate before calling
um.load_users()
assert user_id in um.users, f"user {user_id} not loaded; check users_file and folders"
um.save_user_data(user_id) Type guard
def can_save_user(um, user_id) -> bool:
return user_id in um.users Prevention
- Always call load_users() right after constructing UserManager.
- Assert the id exists before save/remove operations.
- Normalize user id strings once at the system boundary to avoid format mismatches.
When it happens
Trigger: Calling save_user_data(user_id) before load_users() or before create_user added that id; passing a typo'd or removed user_id; calling save on a fresh UserManager constructed with a data_path but never populated.
Common situations: Scripts that assume users are auto-discovered from disk without calling load_users(); user was removed earlier in the session and then saved; case/format mismatch of the id string (e.g. '01' vs '1').
Related errors
- User {} has been loaded
- User data for {} already exists
- Cannot find user data {}
- {freq} is not supported in NumpyQuote
- {method} is not supported
AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15).
Data as JSON: /api/errors/ea13532b8d0cb605.
Report an issue: GitHub.