'How to return a float with 'text/csv' as "Content-Type" from SageMaker endpoint that uses custom inference code?

I am trying to return output(or predictions) from a SageMaker endpoint with 'text/csv' or 'text/csv; charset=utf-8' as "Content-Type". I have tried multiple ways, but the sagemaker always returns with 'text/html; charset=utf-8' as the "Content-Type", and I would like SageMaker to return 'text/csv' or 'text/csv; charset=utf-8'.

Here's the output_fn from my inference-code:

** my other code **
def output_fn(prediction, content_type='text/csv'):
    ** my other code **
    return output_float

above function returns number with float data-type and I got error(in cloudwatch logs) that this function should only be returning string, tuple, dict or Respoonse instance.

So, here are all the different ways I have tried to have SageMaker return my number with 'text/csv' but only receives 'text/html; charset=utf-8'

  1. return json.dumps(output_float). this sent 'text/html; charset=utf-8'.
  2. return f"{output_float}". this sent 'text/html; charset=utf-8'.
  3. return f"{output_float},". this sent 'text/html; charset=utf-8'.
  4. return f"{output_float}\n". this sent 'text/html; charset=utf-8'.
  5. return f"{output_float},\n". this sent 'text/html; charset=utf-8'.
  6. by using sagemaker.serializers.CSVSerializer like this:
    from sagemaker.serializers import CSVSerializer
    csv_serialiser = CSVSerializer(content_type='text/csv')
    
    def output_fn(prediction, content_type='text/csv'):
        ** my other code **
        return csv_serialiser.serialize(output_float)
    
    I got 'NoneType' object has no attribute 'startswith' error with this.
  7. as a tuple: return (output_float,). I haven't noted down what this did, but it sure didn't return the number with 'text/csv' as "Content-Type".
  8. made changes in my model object to return a float on calling .predict_proba on my model-object and deployed it from sagemaker studio without using any custom inference code and deployed this from SageMaker studio. but got this error after sending a request to the endpoint: 'NoneType' object has no attribute 'startswith', but at my side, when I pass proper inputs to the unpicked model and call .predict_proba, i get float as expected.
  9. by returning flask.Response like this:
    from flask import Response
    
    def output_fn(prediction, content_type='text/csv'):
        ** my other code **
        return Response(response=output_float, status=200, headers={'Content-Type':'text/csv; charset=utf-8'})
    
    but, I got some IndexError with this(I didn't notedown the traceback.)

Some other info:

  1. Model I am using is completely outside of SageMaker, not from a training job or anything like that.
  2. All of the aforementioned endpoints have been deployed entirely from aws-cli with relevant .json files, except the one in point-8 above.

How do I return number with "text/csv" as content-type from sagemaker? I need my output in "text/csv" content-type specifically for Model-Quality-Monitor. How do I do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source