prediction_io_cleanup¶
- verta.deployment.prediction_io_cleanup(func)¶
Decorator for casting the argument and return values for
predict()
into Python built-in types.For interoperability, a deployed model will receive and return Python’s built-in types—such as lists rather than NumPy arrays. There may be inconsistencies as you develop your model locally if your
predict()
code is written to expect and/or output a third-party type; this decorator will attempt to cast such values into a Python built-in type, replicatingDeployedModel.predict()
’s behavior.New in version 0.13.17.
Examples
Before:
class Model(object): def predict(self, data): return data.mean() data = np.array([0, 1, 2]) model.predict(data) # succeeds; predict() locally receives NumPy array # 1.0 deployed_model.predict(data) # fails; predict() in deployment receives list # HTTPError: 400 Client Error: Traceback (most recent call last): # File "<stdin>", line 3, in predict # AttributeError: 'list' object has no attribute 'mean' # for url: https://app.verta.ai/api/v1/predict/01234567-0123-0123-0123-012345678901
After:
class Model(object): @prediction_io_cleanup def predict(self, data): # anticipate `data` being list return sum(data) / float(len(data)) data = np.array([1, 2, 3]) # consistent behavior locally and in deployment model.predict(data) # 1.0 deployed_model.predict(data) # 1.0