Deserializers

Implements methods for deserializing data returned from an inference endpoint.

class sagemaker.deserializers.BaseDeserializer

Bases: abc.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 deserialize(stream, content_type)

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

abstract property ACCEPT

The content types that are expected from the inference endpoint.

class sagemaker.deserializers.StringDeserializer(encoding='UTF-8')

Bases: sagemaker.deserializers.BaseDeserializer

Deserialize data from an inference endpoint into a decoded string.

Initialize the string encoding.

Parameters

encoding (str) – The string encoding to use (default: UTF-8).

ACCEPT = ('application/json',)
deserialize(stream, content_type)

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.deserializers.BytesDeserializer

Bases: sagemaker.deserializers.BaseDeserializer

Deserialize a stream of bytes into a bytes object.

ACCEPT = ('*/*',)
deserialize(stream, content_type)

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.deserializers.CSVDeserializer(encoding='utf-8')

Bases: sagemaker.deserializers.BaseDeserializer

Deserialize a stream of bytes into a list of lists.

Initialize the string encoding.

Parameters

encoding (str) – The string encoding to use (default: “utf-8”).

ACCEPT = ('text/csv',)
deserialize(stream, content_type)

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.deserializers.StreamDeserializer

Bases: sagemaker.deserializers.BaseDeserializer

Returns 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.

ACCEPT = ('*/*',)
deserialize(stream, content_type)

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.deserializers.NumpyDeserializer(dtype=None, accept='application/x-npy', allow_pickle=True)

Bases: sagemaker.deserializers.BaseDeserializer

Deserialize a stream of data in the .npy format.

Initialize the dtype and allow_pickle arguments.

Parameters
  • dtype (str) – The dtype of the data (default: None).

  • accept (str) – The MIME type that is expected from the inference endpoint (default: “application/x-npy”).

  • allow_pickle (bool) – Allow loading pickled object arrays (default: True).

deserialize(stream, content_type)

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

property ACCEPT

The content types that are expected from the inference endpoint.

To maintain backwards compatability with legacy images, the NumpyDeserializer supports sending only one content type in the Accept header.

class sagemaker.deserializers.JSONDeserializer

Bases: sagemaker.deserializers.BaseDeserializer

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

ACCEPT = ('application/json',)
deserialize(stream, content_type)

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.deserializers.PandasDeserializer

Bases: sagemaker.deserializers.BaseDeserializer

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

ACCEPT = ('text/csv', 'application/json')
deserialize(stream, content_type)

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.deserializers.JSONLinesDeserializer

Bases: sagemaker.deserializers.BaseDeserializer

Deserialize JSON lines data from an inference endpoint.

ACCEPT = ('application/jsonlines',)
deserialize(stream, content_type)

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