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.SimpleBaseDeserializer(accept='*/*')

Bases: sagemaker.deserializers.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.

Initialize a SimpleBaseDeserializer instance.

Parameters

accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: “/”).

property ACCEPT

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

class sagemaker.deserializers.StringDeserializer(encoding='UTF-8', accept='application/json')

Bases: sagemaker.deserializers.SimpleBaseDeserializer

Deserialize data from an inference endpoint into a decoded string.

Initialize a StringDeserializer instance.

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

  • accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: “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(accept='*/*')

Bases: sagemaker.deserializers.SimpleBaseDeserializer

Deserialize a stream of bytes into a bytes object.

Initialize a SimpleBaseDeserializer instance.

Parameters

accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: “/”).

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', accept='text/csv')

Bases: sagemaker.deserializers.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.

Initialize a CSVDeserializer instance.

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

  • accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: “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(accept='*/*')

Bases: sagemaker.deserializers.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.

Initialize a SimpleBaseDeserializer instance.

Parameters

accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: “/”).

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

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

Initialize a NumpyDeserializer instance.

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

  • accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) 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

class sagemaker.deserializers.JSONDeserializer(accept='application/json')

Bases: sagemaker.deserializers.SimpleBaseDeserializer

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

Initialize a JSONDeserializer instance.

Parameters

accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: “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(accept='text/csv', 'application/json')

Bases: sagemaker.deserializers.SimpleBaseDeserializer

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

Initialize a PandasDeserializer instance.

Parameters

accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: (“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(accept='application/jsonlines')

Bases: sagemaker.deserializers.SimpleBaseDeserializer

Deserialize JSON lines data from an inference endpoint.

Initialize a JSONLinesDeserializer instance.

Parameters

accept (union[str, tuple[str]]) – The MIME type (or tuple of allowable MIME types) that is expected from the inference endpoint (default: (“text/csv”,”application/json”)).

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