sagemaker.core.deserializers.base#

Implements base methods for deserializing data returned from an inference endpoint.

Classes

BaseDeserializer()

Abstract base class for creation of new deserializers.

BytesDeserializer([accept])

Deserialize a stream of bytes into a bytes object.

CSVDeserializer([encoding, accept])

Deserialize a stream of bytes into a list of lists.

JSONDeserializer([accept])

Deserialize JSON data from an inference endpoint into a Python object.

JSONLinesDeserializer([accept])

Deserialize JSON lines data from an inference endpoint.

NumpyDeserializer([dtype, accept, allow_pickle])

Deserialize a stream of data in .npy, .npz or UTF-8 CSV/JSON format to a numpy array.

PandasDeserializer([accept])

Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe.

RecordDeserializer([accept])

Deserialize RecordIO Protobuf data from an inference endpoint.

SimpleBaseDeserializer([accept])

Abstract base class for creation of new deserializers.

StreamDeserializer([accept])

Directly return the data and content-type received from an inference endpoint.

StringDeserializer([encoding, accept])

Deserialize data from an inference endpoint into a decoded string.

TorchTensorDeserializer([accept])

Deserialize stream to torch.Tensor.

class sagemaker.core.deserializers.base.BaseDeserializer[source]#

Bases: ABC

Abstract base class for creation of new deserializers.

Provides a skeleton for customization requiring the overriding of the method deserialize and the class attribute ACCEPT.

abstract property ACCEPT#

The content types that are expected from the inference endpoint.

abstract deserialize(stream, content_type)[source]#

Deserialize data received from an inference endpoint.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The data deserialized into an object.

Return type:

object

class sagemaker.core.deserializers.base.BytesDeserializer(accept='*/*')[source]#

Bases: SimpleBaseDeserializer

Deserialize a stream of bytes into a bytes object.

deserialize(stream, content_type)[source]#

Read a stream of bytes returned from an inference endpoint.

Parameters:
  • stream (botocore.response.StreamingBody) – A stream of bytes.

  • content_type (str) – The MIME type of the data.

Returns:

The bytes object read from the stream.

Return type:

bytes

class sagemaker.core.deserializers.base.CSVDeserializer(encoding='utf-8', accept='text/csv')[source]#

Bases: SimpleBaseDeserializer

Deserialize a stream of bytes into a list of lists.

Consider using :class:~`sagemaker.deserializers.NumpyDeserializer` or :class:~`sagemaker.deserializers.PandasDeserializer` instead, if you’d like to convert text/csv responses directly into other data types.

deserialize(stream, content_type)[source]#

Deserialize data from an inference endpoint into a list of lists.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The data deserialized into a list of lists representing the

contents of a CSV file.

Return type:

list

class sagemaker.core.deserializers.base.JSONDeserializer(accept='application/json')[source]#

Bases: SimpleBaseDeserializer

Deserialize JSON data from an inference endpoint into a Python object.

deserialize(stream, content_type)[source]#

Deserialize JSON data from an inference endpoint into a Python object.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The JSON-formatted data deserialized into a Python object.

Return type:

object

class sagemaker.core.deserializers.base.JSONLinesDeserializer(accept='application/jsonlines')[source]#

Bases: SimpleBaseDeserializer

Deserialize JSON lines data from an inference endpoint.

deserialize(stream, content_type)[source]#

Deserialize JSON lines data from an inference endpoint.

See https://docs.python.org/3/library/json.html#py-to-json-table to understand how JSON values are converted to Python objects.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

A list of JSON serializable objects.

Return type:

list

class sagemaker.core.deserializers.base.NumpyDeserializer(dtype=None, accept='application/x-npy', allow_pickle=False)[source]#

Bases: SimpleBaseDeserializer

Deserialize a stream of data in .npy, .npz or UTF-8 CSV/JSON format to a numpy array.

Note that when using application/x-npz archive format, the result will usually be a dictionary-like object containing multiple arrays (as per numpy.load()) - instead of a single array.

deserialize(stream, content_type)[source]#

Deserialize data from an inference endpoint into a NumPy array.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The data deserialized into a NumPy array.

Return type:

numpy.ndarray

class sagemaker.core.deserializers.base.PandasDeserializer(accept=('text/csv', 'application/json'))[source]#

Bases: SimpleBaseDeserializer

Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe.

deserialize(stream, content_type)[source]#

Deserialize CSV or JSON data from an inference endpoint into a pandas dataframe.

If the data is JSON, the data should be formatted in the ‘columns’ orient. See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_json.html

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The data deserialized into a pandas DataFrame.

Return type:

pandas.DataFrame

class sagemaker.core.deserializers.base.RecordDeserializer(accept='application/x-recordio-protobuf')[source]#

Bases: SimpleBaseDeserializer

Deserialize RecordIO Protobuf data from an inference endpoint.

deserialize(data, content_type)[source]#

Deserialize RecordIO Protobuf data from an inference endpoint.

Parameters:
  • data (object) – The protobuf message to deserialize.

  • content_type (str) – The MIME type of the data.

Returns:

A list of records.

Return type:

list

class sagemaker.core.deserializers.base.SimpleBaseDeserializer(accept='*/*')[source]#

Bases: BaseDeserializer

Abstract base class for creation of new deserializers.

This class extends the API of :class:~`sagemaker.deserializers.BaseDeserializer` with more user-friendly options for setting the ACCEPT content type header, in situations where it can be provided at init and freely updated.

property ACCEPT#

The tuple of possible content types that are expected from the inference endpoint.

class sagemaker.core.deserializers.base.StreamDeserializer(accept='*/*')[source]#

Bases: SimpleBaseDeserializer

Directly return the data and content-type received from an inference endpoint.

It is the user’s responsibility to close the data stream once they’re done reading it.

deserialize(stream, content_type)[source]#

Returns a stream of the response body and the MIME type of the data.

Parameters:
  • stream (botocore.response.StreamingBody) – A stream of bytes.

  • content_type (str) – The MIME type of the data.

Returns:

A two-tuple containing the stream and content-type.

Return type:

tuple

class sagemaker.core.deserializers.base.StringDeserializer(encoding='UTF-8', accept='application/json')[source]#

Bases: SimpleBaseDeserializer

Deserialize data from an inference endpoint into a decoded string.

deserialize(stream, content_type)[source]#

Deserialize data from an inference endpoint into a decoded string.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The data deserialized into a decoded string.

Return type:

str

class sagemaker.core.deserializers.base.TorchTensorDeserializer(accept='tensor/pt')[source]#

Bases: SimpleBaseDeserializer

Deserialize stream to torch.Tensor.

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

The data deserialized into a torch Tensor.

Return type:

torch.Tensor

deserialize(stream, content_type='tensor/pt')[source]#

Deserialize streamed data to TorchTensor

See https://pytorch.org/docs/stable/generated/torch.from_numpy.html

Parameters:
  • stream (botocore.response.StreamingBody) – Data to be deserialized.

  • content_type (str) – The MIME type of the data.

Returns:

A list of TorchTensor serializable objects.

Return type:

list