Search code examples
pythonamazon-web-servicestime-seriesamazon-sagemakerdeepar

AWS DeepAR predict returns 400


I'm trying to run a DeepAR to do some estimation for a series the next 15 records based on current and previous months.

I follow the examples and I believe that everything is ok. Still I have a very cryptic error.

I put here the code:

predictor = estimator.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.large',   
    serializer=JSONSerializer(),
    deserializer=JSONDeserializer()
    )

json_request = json.dumps({
    "instances": ts,
    "configuration": {
        "num_samples": 10,
        "output_types": ["quantiles", "samples"],
        "quantiles": ['0.2', '0.5', '0.8']
    }
})

prediction = predictor.predict(json_request)

My json looks like this:

{"instances": 
    [{"start": "2024-03-01", 
      "target":[60,10,86,62,21,25,7,79,33,82,34,43,14,99,5,37,85,84,88,25,2,14,15,98,14,75,70,99,12]
      }, 
     {"start": "2024-04-01", 
      "target": [55,89,40,81,87,7,49,77,37,42,48,27,89,45,85]
      }], 
 "configuration": {"num_samples": 15, "output_types": ["quantiles", "samples"], "quantiles": ["0.2", "0.5", "0.8"]}}

But I have the following error:

---------------------------------------------------------------------------
ModelError                                Traceback (most recent call last)
Cell In[22], line 2
      1 print(type('json_request'))
----> 2 prediction = predictor.predict(json_request)
      3 print(prediction)

File c:\Users\civan\PycharmProjects\JupyterBooks\.venv\Lib\site-packages\sagemaker\base_predictor.py:212, in Predictor.predict(self, data, initial_args, target_model, target_variant, inference_id, custom_attributes, component_name)
    209 if inference_component_name:
    210     request_args["InferenceComponentName"] = inference_component_name
--> 212 response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)
    213 return self._handle_response(response)

File c:\Users\civan\PycharmProjects\JupyterBooks\.venv\Lib\site-packages\botocore\client.py:553, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
    549     raise TypeError(
    550         f"{py_operation_name}() only accepts keyword arguments."
    551     )
    552 # The "self" in this scope is referring to the BaseClient.
--> 553 return self._make_api_call(operation_name, kwargs)

File c:\Users\civan\PycharmProjects\JupyterBooks\.venv\Lib\site-packages\botocore\client.py:1009, in BaseClient._make_api_call(self, operation_name, api_params)
   1005     error_code = error_info.get("QueryErrorCode") or error_info.get(
   1006         "Code"
   1007     )
   1008     error_class = self.exceptions.from_code(error_code)
-> 1009     raise error_class(parsed_response, operation_name)
   1010 else:
   1011     return parsed_response

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary with message "Unable to evaluate payload provided".

I have tried without serializer/desearilzer and it give error also but not this one, things related to format.


Solution

  • After more digging in AWS class serialization I found the model, I put here maybe somebody will find on google search (or ChatGPT learn this :) )

    from sagemaker.serializers import IdentitySerializer
    from sagemaker.deserializers import JSONDeserializer
    
    predictor = estimator.deploy(
        initial_instance_count=1,
        instance_type='ml.m5.large',   
        serializer=IdentitySerializer(content_type="application/json"),
        deserializer=JSONDeserializer()
        )
    
    prediction = predictor.predict(json_request)