Serializers¶
Implements methods for serializing data for an inference endpoint.
-
class
sagemaker.serializers.
BaseSerializer
¶ Bases:
abc.ABC
Abstract base class for creation of new serializers.
Provides a skeleton for customization requiring the overriding of the method serialize and the class attribute CONTENT_TYPE.
-
abstract
serialize
(data)¶ Serialize data into the media type specified by CONTENT_TYPE.
-
abstract property
CONTENT_TYPE
¶ The MIME type of the data sent to the inference endpoint.
-
abstract
-
class
sagemaker.serializers.
SimpleBaseSerializer
(content_type='application/json')¶ Bases:
sagemaker.serializers.BaseSerializer
Abstract base class for creation of new serializers.
This class extends the API of :class:~`sagemaker.serializers.BaseSerializer` with more user-friendly options for setting the Content-Type header, in situations where it can be provided at init and freely updated.
Initialize a
SimpleBaseSerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending
data (default (request) – “application/json”).
-
property
CONTENT_TYPE
¶ The data MIME type set in the Content-Type header on prediction endpoint requests.
-
class
sagemaker.serializers.
CSVSerializer
(content_type='text/csv')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize data of various formats to a CSV-formatted string.
Initialize a
CSVSerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending request data (default: “text/csv”).
-
class
sagemaker.serializers.
NumpySerializer
(dtype=None, content_type='application/x-npy')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize data to a buffer using the .npy format.
Initialize a
NumpySerializer
instance.- Parameters
-
class
sagemaker.serializers.
JSONSerializer
(content_type='application/json')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize data to a JSON formatted string.
Initialize a
SimpleBaseSerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending
data (default (request) – “application/json”).
-
class
sagemaker.serializers.
IdentitySerializer
(content_type='application/octet-stream')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize data by returning data without modification.
This serializer may be useful if, for example, you’re sending raw bytes such as from an image file’s .read() method.
Initialize an
IdentitySerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending request data (default: “application/octet-stream”).
-
class
sagemaker.serializers.
JSONLinesSerializer
(content_type='application/jsonlines')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize data to a JSON Lines formatted string.
Initialize a
JSONLinesSerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending request data (default: “application/jsonlines”).
-
serialize
(data)¶ Serialize data of various formats to a JSON Lines formatted string.
-
class
sagemaker.serializers.
SparseMatrixSerializer
(content_type='application/x-npz')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize a sparse matrix to a buffer using the .npz format.
Initialize a
SparseMatrixSerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending request data (default: “application/x-npz”).
-
serialize
(data)¶ Serialize a sparse matrix to a buffer using the .npz format.
Sparse matrices can be in the
csc
,csr
,bsr
,dia
orcoo
formats.- Parameters
data (scipy.sparse.spmatrix) – The sparse matrix to serialize.
- Returns
A buffer containing the serialized sparse matrix.
- Return type
-
class
sagemaker.serializers.
LibSVMSerializer
(content_type='text/libsvm')¶ Bases:
sagemaker.serializers.SimpleBaseSerializer
Serialize data of various formats to a LibSVM-formatted string.
The data must already be in LIBSVM file format: <label> <index1>:<value1> <index2>:<value2> …
It is suitable for sparse datasets since it does not store zero-valued features.
Initialize a
LibSVMSerializer
instance.- Parameters
content_type (str) – The MIME type to signal to the inference endpoint when sending request data (default: “text/libsvm”).