Export model artifacts for inference and explanation

Vertex AI offersprebuilt containersto serve inferences and explanations from models trained using the followingmachine learning (ML) frameworks:

  • TensorFlow
  • PyTorch
  • XGBoost
  • scikit-learn

To use one of these prebuilt containers, you must save your model as one ormoremodel artifacts that comply with the requirements of the prebuiltcontainer. These requirements apply whether or not your model artifacts arecreated on Vertex AI.

If you use acustom container to serve inferences, youdon't need to comply with the requirements in this page, but you can still usethem as guidelines.

Framework-specific requirements for exporting to prebuilt containers

Depending onwhich ML framework you plan to use forinference, you must export model artifactsin different formats. The following sections describe the acceptable modelformats for each ML framework.

TensorFlow

If youuse TensorFlow to train amodel, export your model as aTensorFlow SavedModeldirectory.

There are several ways to export SavedModels from TensorFlow training code. Thefollowing list describes a few ways that work for various TensorFlow APIs:

To serve inferences using these artifacts, create aModel with theprebuiltcontainer for inference matchingthe version of TensorFlow that you used for training.

TensorFlow for Vertex Explainable AI

If you want toget explanations from aModel that usesa TensorFlow prebuilt container to serve inferences, read theadditionalrequirements for exporting a TensorFlow model forVertex Explainable AI.

Enable server-side request batching for TensorFlow

If you want to enable request batching for aModel that uses a TensorFlowprebuilt container to serve inferences, includeconfig/batching_parameters_config in the same Cloud Storage directory assaved_model.pbfile. To configure the batching config file, see theTensorFlow'sofficial documentation.

PyTorch

You must package your model artifacts into a model archive (.mar) file.This archive must include a handler script, eitherdefault orcustom, thatdefines how the model processes inference requests. For more advanced workflows,you should consider using custom containers instead of relying on the prebuiltPyTorch serving image.

The prebuilt PyTorch images expect the archive to be namedmodel.mar, so makesure you set the model-name to 'model' when creating the archive.

For information about optimizing the memory usage, latency or throughput of aPyTorch model served with TorchServe, see thePyTorch performance guide.

XGBoost

If you use anXGBoost prebuilt containerto train a model, you can export the trained model in the following ways:

Your model artifact's filename must exactly match one of these options.

The following examples show how to train and export a model:

Note: These examples assume that you train your model onVertex AI and use theAIP_MODEL_DIR environment variable set byVertex AI.

xgboost.Booster

importosfromgoogle.cloudimportstoragefromsklearnimportdatasetsimportxgboostasxgbdigits=datasets.load_digits()dtrain=xgb.DMatrix(digits.data,label=digits.target)bst=xgb.train({},dtrain,20)artifact_filename='model.bst'# Save model artifact to local filesystem (doesn't persist)local_path=artifact_filenamebst.save_model(local_path)# Upload model artifact to Cloud Storagemodel_directory=os.environ['AIP_MODEL_DIR']storage_path=os.path.join(model_directory,artifact_filename)blob=storage.blob.Blob.from_string(storage_path,client=storage.Client())blob.upload_from_filename(local_path)

joblib

importosfromgoogle.cloudimportstoragefromsklearnimportdatasetsimportjoblibimportxgboostasxgbdigits=datasets.load_digits()dtrain=xgb.DMatrix(digits.data,label=digits.target)bst=xgb.train({},dtrain,20)artifact_filename='model.joblib'# Save model artifact to local filesystem (doesn't persist)local_path=artifact_filenamejoblib.dump(bst,local_path)# Upload model artifact to Cloud Storagemodel_directory=os.environ['AIP_MODEL_DIR']storage_path=os.path.join(model_directory,artifact_filename)blob=storage.blob.Blob.from_string(storage_path,client=storage.Client())blob.upload_from_filename(local_path)

To serve inferences using this artifact, create aModel with theprebuiltcontainer for inference matchingthe version of XGBoost that you used for training.

scikit-learn

If youuse ascikit-learn prebuilt model to train amodel, you can export itby using thejoblib libraryto export a file namedmodel.joblib.

Your model artifact's filename must exactly match one of these options. You canexport standard scikit-learn estimators orscikit-learnpipelines.

The following example shows how to train and export a model:

Note: These examples assume that you train your model onVertex AI and use theAIP_MODEL_DIR environment variable set byVertex AI.

joblib

importosfromgoogle.cloudimportstoragefromsklearnimportdatasetsfromsklearn.ensembleimportRandomForestClassifierimportjoblibdigits=datasets.load_digits()classifier=RandomForestClassifier()classifier.fit(digits.data,digits.target)artifact_filename='model.joblib'# Save model artifact to local filesystem (doesn't persist)local_path=artifact_filenamejoblib.dump(classifier,local_path)# Upload model artifact to Cloud Storagemodel_directory=os.environ['AIP_MODEL_DIR']storage_path=os.path.join(model_directory,artifact_filename)blob=storage.blob.Blob.from_string(storage_path,client=storage.Client())blob.upload_from_filename(local_path)

To serve inferences using this artifact, create aModel with theprebuiltcontainer for inferencematching the version of scikit-learn that you used for training.

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.