{"record":{"id":"f7d5e1f74f5c465e","repo":"mlflow/mlflow","slug":"bad-request-f7d5e1","errorCode":"BAD_REQUEST","errorMessage":"Model libraries are already added","messagePattern":"Model libraries are already added","errorType":"exception","errorClass":"MlflowException","httpStatus":400,"severity":"error","filePath":"mlflow/models/wheeled_model.py","lineNumber":117,"sourceCode":"            mlflow_model: The new :py:mod:`mlflow.models.Model` metadata file to store the\n                updated model metadata.\n        \"\"\"\n        from mlflow.pyfunc import ENV, FLAVOR_NAME, _extract_conda_env\n\n        path = os.path.abspath(path)\n        _validate_and_prepare_target_save_path(path)\n\n        local_model_path = _download_artifact_from_uri(self._model_uri, output_path=path)\n\n        wheels_dir = os.path.join(local_model_path, _WHEELS_FOLDER_NAME)\n        pip_requirements_path = os.path.join(local_model_path, _REQUIREMENTS_FILE_NAME)\n        model_metadata_path = os.path.join(local_model_path, MLMODEL_FILE_NAME)\n\n        model_metadata = Model.load(model_metadata_path)\n\n        # Check if the model file has `wheels` set to True\n        if model_metadata.__dict__.get(_WHEELS_FOLDER_NAME, None) is not None:\n            raise MlflowException(\"Model libraries are already added\", BAD_REQUEST)\n\n        conda_env = _extract_conda_env(model_metadata.flavors.get(FLAVOR_NAME, {}).get(ENV, None))\n        conda_env_path = os.path.join(local_model_path, conda_env)\n        if conda_env is None and not os.path.isfile(pip_requirements_path):\n            raise MlflowException(\n                \"Cannot add libraries for model with no logged dependencies.\", BAD_REQUEST\n            )\n\n        if not os.path.isfile(pip_requirements_path):\n            self._create_pip_requirement(conda_env_path, pip_requirements_path)\n\n        WheeledModel._download_wheels(\n            pip_requirements_path=pip_requirements_path, dst_path=wheels_dir\n        )\n\n        # Keep a copy of the original requirement.txt\n        shutil.copy2(pip_requirements_path, os.path.join(local_model_path, _ORIGINAL_REQ_FILE_NAME))\n","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/models/wheeled_model.py#L99-L135","documentation":"WheeledModel.save_model packages a model's dependencies as pip wheels into the model directory. If the MLmodel file already records a wheels section, the wheels were previously added and adding them again would overwrite/corrupt the package, so MLflow rejects the request with BAD_REQUEST.","triggerScenarios":"Calling WheeledModel(...).save_model() or .log() on a model directory whose MLmodel file already contains a wheels key (i.e., add_libraries was already applied to this model).","commonSituations":"Re-running a script that calls add_libraries on the same logged model; chaining add_libraries calls twice; re-deploying code that modifies an already-wheeled model copy.","solutions":["Do not call add_libraries again on a model that already has wheels; use the existing wheeled model","Log/save a fresh copy of the original (non-wheeled) model and call add_libraries on that copy","If you must re-add, delete the wheels entry and the wheels directory from the model copy first"],"exampleFix":"// before\nmodel_info = mlflow.sklearn.log_model(model, \"model\")\nwheeled = WheeledModel(model_uri=model_info.model_uri)\nwheeled.add_libraries().log(...)  # second run raises\ndef add_wheels():\n    wheeled.add_libraries()\nadd_wheels()\nadd_wheels()\n\n// after\ndef add_wheels():\n    mi = mlflow.sklearn.log_model(model, \"model\")  # fresh copy each run\n    WheeledModel(model_uri=mi.model_uri).add_libraries()","handlingStrategy":"validation","validationCode":"import mlflow\nfrom mlflow.models import Model\nfrom mlflow.models.model import MLMODEL_FILE_NAME\nimport os\ndef has_wheels(model_dir: str) -> bool:\n    meta = Model.load(os.path.join(model_dir, MLMODEL_FILE_NAME))\n    return meta.__dict__.get(\"wheels\") is not None","typeGuard":null,"tryCatchPattern":"from mlflow.exceptions import MlflowException\ntry:\n    wheeled.add_libraries()\nexcept MlflowException as e:\n    if \"already added\" in str(e):\n        print(\"Model already wheeled; reusing existing package\")\n    else:\n        raise","preventionTips":["Track whether add_libraries has already been applied to a model copy","Always create a fresh logged copy of the model before adding libraries","Keep add_libraries calls out of retry loops"],"tags":["mlflow","wheels","dependencies"],"backgroundTag":"model-already-packaged","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}