|
| 1 | +# 3.0.0 Migration Guide |
| 2 | + |
| 3 | +The 2.0 release of the `google-cloud-automl` client is a significant upgrade based on a [next-gen code generator](https://github.com/googleapis/gapic-generator-python), and includes substantial interface changes. Existing code written for earlier versions of this library will likely require updates to use this version. This document describes the changes that have been made, and what you need to do to update your usage. |
| 4 | + |
| 5 | +If you experience issues or have questions, please file an [issue](https://github.com/googleapis/python-automl/issues). |
| 6 | + |
| 7 | +## Supported Python Versions |
| 8 | + |
| 9 | +> **WARNING**: Breaking change |
| 10 | +
|
| 11 | +The 2.0.0 release requires Python 3.6+. |
| 12 | + |
| 13 | + |
| 14 | +## Method Calls |
| 15 | + |
| 16 | +> **WARNING**: Breaking change |
| 17 | +
|
| 18 | +Methods expect request objects. We provide a script that will convert most common use cases. |
| 19 | + |
| 20 | +* Install the library |
| 21 | + |
| 22 | +```py |
| 23 | +python3 -m pip install google-cloud-automl |
| 24 | +``` |
| 25 | + |
| 26 | +* The script `fixup_automl_{version}_keywords.py` is shipped with the library. It expects |
| 27 | +an input directory (with the code to convert) and an empty destination directory. |
| 28 | + |
| 29 | +```sh |
| 30 | +$ fixup_automl_v1_keywords.py --input-directory .samples/ --output-directory samples/ |
| 31 | +``` |
| 32 | + |
| 33 | +**Before:** |
| 34 | +```py |
| 35 | +from google.cloud import automl |
| 36 | + |
| 37 | +project_id = "YOUR_PROJECT_ID" |
| 38 | +model_id = "YOUR_MODEL_ID" |
| 39 | + |
| 40 | +client = automl.AutoMlClient() |
| 41 | +# Get the full path of the model. |
| 42 | +model_full_id = client.model_path(project_id, "us-central1", model_id) |
| 43 | +response = client.deploy_model(model_full_id) |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +**After:** |
| 48 | +```py |
| 49 | +from google.cloud import automl |
| 50 | + |
| 51 | +project_id = "YOUR_PROJECT_ID" |
| 52 | +model_id = "YOUR_MODEL_ID" |
| 53 | + |
| 54 | +client = automl.AutoMlClient() |
| 55 | +# Get the full path of the model. |
| 56 | +model_full_id = client.model_path(project_id, "us-central1", model_id) |
| 57 | +response = client.deploy_model(name=model_full_id) |
| 58 | +``` |
| 59 | + |
| 60 | +### More Details |
| 61 | + |
| 62 | +In `google-cloud-automl<2.0.0`, parameters required by the API were positional parameters and optional parameters were keyword parameters. |
| 63 | + |
| 64 | +**Before:** |
| 65 | +```py |
| 66 | + def batch_predict( |
| 67 | + self, |
| 68 | + name, |
| 69 | + input_config, |
| 70 | + output_config, |
| 71 | + params=None, |
| 72 | + retry=google.api_core.gapic_v1.method.DEFAULT, |
| 73 | + timeout=google.api_core.gapic_v1.method.DEFAULT, |
| 74 | + metadata=None, |
| 75 | + ): |
| 76 | +``` |
| 77 | + |
| 78 | +In the 2.0.0 release, all methods have a single positional parameter `request`. Method docstrings indicate whether a parameter is required or optional. |
| 79 | + |
| 80 | +Some methods have additional keyword only parameters. The available parameters depend on the [`google.api.method_signature` annotation](https://github.com/googleapis/googleapis/blob/2db5725bf898b544a0cf951e1694d3b0fce5eda3/google/cloud/automl/v1/prediction_service.proto#L86) specified by the API producer. |
| 81 | + |
| 82 | + |
| 83 | +**After:** |
| 84 | +```py |
| 85 | +def batch_predict( |
| 86 | + self, |
| 87 | + request: prediction_service.BatchPredictRequest = None, |
| 88 | + *, |
| 89 | + name: str = None, |
| 90 | + input_config: io.BatchPredictInputConfig = None, |
| 91 | + output_config: io.BatchPredictOutputConfig = None, |
| 92 | + params: Sequence[prediction_service.BatchPredictRequest.ParamsEntry] = None, |
| 93 | + retry: retries.Retry = gapic_v1.method.DEFAULT, |
| 94 | + timeout: float = None, |
| 95 | + metadata: Sequence[Tuple[str, str]] = (), |
| 96 | + ) -> operation.Operation: |
| 97 | +``` |
| 98 | + |
| 99 | +> **NOTE:** The `request` parameter and flattened keyword parameters for the API are mutually exclusive. |
| 100 | +> Passing both will result in an error. |
| 101 | +
|
| 102 | + |
| 103 | +Both of these calls are valid: |
| 104 | + |
| 105 | +```py |
| 106 | +response = client.batch_predict( |
| 107 | + request={ |
| 108 | + "name": name, |
| 109 | + "input_config": input_config, |
| 110 | + "output_config": output_config, |
| 111 | + "params": params, |
| 112 | + } |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +```py |
| 117 | +response = client.batch_predict( |
| 118 | + name=name, |
| 119 | + input_config=input_config, |
| 120 | + output_config=output_config, |
| 121 | + params=params, |
| 122 | +) |
| 123 | +``` |
| 124 | + |
| 125 | +This call is invalid because it mixes `request` with a keyword argument `params`. Executing this code |
| 126 | +will result in an error. |
| 127 | + |
| 128 | +```py |
| 129 | +response = client.batch_predict( |
| 130 | + request={ |
| 131 | + "name": name, |
| 132 | + "input_config": input_config, |
| 133 | + "output_config": output_config, |
| 134 | + }, |
| 135 | + params=params, |
| 136 | +) |
| 137 | +``` |
| 138 | + |
| 139 | + |
| 140 | +The method `list_datasets` takes an argument `filter` instead of `filter_`. |
| 141 | + |
| 142 | +**Before** |
| 143 | +```py |
| 144 | +from google.cloud import automl |
| 145 | + |
| 146 | +project_id = "PROJECT_ID" |
| 147 | + |
| 148 | +client = automl.AutoMlClient() |
| 149 | +project_location = client.location_path(project_id, "us-central1") |
| 150 | + |
| 151 | +# List all the datasets available in the region. |
| 152 | +response = client.list_datasets(project_location, filter_="") |
| 153 | +``` |
| 154 | + |
| 155 | +**After** |
| 156 | +```py |
| 157 | +from google.cloud import automl |
| 158 | + |
| 159 | +project_id = "PROJECT_ID" |
| 160 | +client = automl.AutoMlClient() |
| 161 | +# A resource that represents Google Cloud Platform location. |
| 162 | +project_location = f"projects/{project_id}/locations/us-central1" |
| 163 | + |
| 164 | +# List all the datasets available in the region. |
| 165 | +response = client.list_datasets(parent=project_location, filter="") |
| 166 | +``` |
| 167 | + |
| 168 | +### Changes to v1beta1 Tables Client |
| 169 | + |
| 170 | +Optional arguments are now keyword-only arguments and *must* be passed by name. |
| 171 | +See [PEP 3102](https://www.python.org/dev/peps/pep-3102/). |
| 172 | + |
| 173 | +***Before** |
| 174 | +```py |
| 175 | + def predict( |
| 176 | + self, |
| 177 | + inputs, |
| 178 | + model=None, |
| 179 | + model_name=None, |
| 180 | + model_display_name=None, |
| 181 | + feature_importance=False, |
| 182 | + project=None, |
| 183 | + region=None, |
| 184 | + **kwargs |
| 185 | + ): |
| 186 | +``` |
| 187 | + |
| 188 | +**After** |
| 189 | +```py |
| 190 | + def predict( |
| 191 | + self, |
| 192 | + inputs, |
| 193 | + *, |
| 194 | + model=None, |
| 195 | + model_name=None, |
| 196 | + model_display_name=None, |
| 197 | + feature_importance=False, |
| 198 | + project=None, |
| 199 | + region=None, |
| 200 | + **kwargs, |
| 201 | + ): |
| 202 | +``` |
| 203 | + |
| 204 | +**kwargs passed to methods must be either (1) kwargs on the underlying method (`retry`, `timeout`, or `metadata`) or (2) attributes of the request object. |
| 205 | + |
| 206 | +The following call is valid because `filter` is an attribute of `automl_v1beta1.ListDatasetsRequest`. |
| 207 | + |
| 208 | +```py |
| 209 | +from google.cloud import automl_v1beta1 as automl |
| 210 | + |
| 211 | +client = automl.TablesClient(project=project_id, region=compute_region) |
| 212 | + |
| 213 | +# List all the datasets available in the region by applying filter. |
| 214 | +response = client.list_datasets(filter=filter) |
| 215 | +``` |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | +## Enums and types |
| 220 | + |
| 221 | + |
| 222 | +> **WARNING**: Breaking change |
| 223 | +
|
| 224 | +The submodule `enums` and `types` have been removed. |
| 225 | + |
| 226 | +**Before:** |
| 227 | +```py |
| 228 | + |
| 229 | +from google.cloud import automl |
| 230 | + |
| 231 | +gcs_source = automl.types.GcsSource(input_uris=["gs://YOUR_BUCKET_ID/path/to/your/input/csv_or_jsonl"]) |
| 232 | +deployment_state = automl.enums.Model.DeploymentState.DEPLOYED |
| 233 | +``` |
| 234 | + |
| 235 | + |
| 236 | +**After:** |
| 237 | +```py |
| 238 | +from google.cloud import automl |
| 239 | + |
| 240 | +gcs_source = automl.GcsSource(input_uris=["gs://YOUR_BUCKET_ID/path/to/your/input/csv_or_jsonl"]) |
| 241 | +deployment_state = automl.Model.DeploymentState.DEPLOYED |
| 242 | +``` |
| 243 | + |
| 244 | + |
| 245 | +## Resource Path Helper Methods |
| 246 | + |
| 247 | +The following resource name helpers have been removed. Please construct the strings manually. |
| 248 | + |
| 249 | +```py |
| 250 | +from google.cloud import automl |
| 251 | + |
| 252 | +project = "my-project" |
| 253 | +location = "us-central1" |
| 254 | +dataset = "my-dataset" |
| 255 | +model = "my-model" |
| 256 | +annotation_spec = "test-annotation" |
| 257 | +model_evaluation = "test-evaluation" |
| 258 | + |
| 259 | +# AutoMlClient |
| 260 | +annotation_spec_path = f"projects/{project}/locations/{location}/datasets/{dataset}/annotationSpecs/{annotation_spec}" |
| 261 | +location_path = f"projects/{project}/locations/{location}" |
| 262 | +model_evaluation_path = f"projects/{project}/locations/{location}/models/{model}/modelEvaluations/{model_evaluation}", |
| 263 | + |
| 264 | +# PredictionServiceClient |
| 265 | +model_path = f"projects/{project}/locations/{location}/models/{model}" |
| 266 | +# alternatively you can use `model_path` from AutoMlClient |
| 267 | +model_path = automl.AutoMlClient.model_path(project_id, location, model_id) |
| 268 | + |
| 269 | +``` |
0 commit comments