microsoft/qlib · error · ValueError

User data for {} already exists

Error message

User data for {} already exists

What it means

Raised by UserManager.add_user when the target folder data_path/user_id already exists. add_user creates a fresh account folder for a new online user and refuses to overwrite an existing user's data directory.

Source

Thrown at qlib/contrib/online/manager.py:110

            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()
                   path of config file
        """
        config_file = pathlib.Path(config_file)
        if not config_file.exists():
            raise ValueError("Cannot find config file {}".format(config_file))
        user_path = self.data_path / user_id
        if user_path.exists():
            raise ValueError("User data for {} already exists".format(user_id))

        with config_file.open("r") as fp:
            yaml = YAML(typ="safe", pure=True)
            config = yaml.load(fp)
        # load model
        model = init_instance_by_config(config["model"])

        # load strategy
        strategy = init_instance_by_config(config["strategy"])
        init_args = strategy.get_init_args_from_model(model, add_date)
        strategy.init(**init_args)

        # init Account
        trade_account = Account(init_cash=config["init_cash"])

        # save user
        user_path.mkdir()
        save_instance(model, self.data_path / user_id / "model_{}.pickle".format(user_id))

View on GitHub (pinned to 79633dd950)

Solutions

  1. If the user is stale/leftover, remove it first with um.remove_user(user_id) (or delete the folder), then add_user again.
  2. Pick a fresh unique user_id for the new user.
  3. Guard before creating: if (um.data_path / user_id).exists(), skip or clean up.

Example fix

# before
um.add_user(user_id="u1", config_file=cfg, add_date=d)
# ValueError: User data for u1 already exists

# after
if (um.data_path / "u1").exists():
    um.remove_user("u1")  # or choose a new unique id
um.add_user(user_id="u1", config_file=cfg, add_date=d)
Defensive patterns

Strategy: validation

Validate before calling

user_path = um.data_path / user_id
if user_path.exists():
    um.remove_user(user_id)  # or pick a fresh unique id
um.add_user(user_id=user_id, config_file=cfg, add_date=add_date)

Prevention

When it happens

Trigger: Calling add_user with a user_id whose folder was created previously (active user, or leftover from a partially failed add); re-running an initialization script without cleaning the user data path.

Common situations: Re-running a bootstrap script after a crash midway through add_user left the folder behind; retrying user creation because the first attempt failed after folder creation; id reuse across test runs against the same data path.

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/dde59f84abf0d4c6. Report an issue: GitHub.