python-3.x 调用InvokeEndpoint操作时出错(ModelError):收到服务器错误(500),正在从lambda+ API网关调用终结点

1l5u6lss  于 2023-01-22  发布在  Python
关注(0)|答案(1)|浏览(57)

我的sagemaker端点在从lambda函数调用时似乎工作正常,但当我使用来自API网关rest api(在api网关上部署的rest api作为LAMBDA API INTEGRATION ENABLED的ANY方法)的相同有效负载时,它给出以下错误
从其中调用SageMaker终结点的lambda函数代码部分

@app.post("/dept_predictor")
@tracer.capture_method
def dept_predictor():
    # post_data: dict = app.current_event.json_body
    # try:
        print("Received event: " + json.dumps( app.current_event.body, indent=2))
        payload = app.current_event.body
        print(payload)
        # raise Exception('Malformed input ...')
    
        response  = runtime_client.invoke_endpoint(EndpointName=ENDPOINT_NAME, 
                                                  ContentType='application/json', 
                                                  Body=json.dumps(payload),
                                                  Accept='Accept')
        print(response)

从API网关调用lambda函数后的Lambda函数CloudWatch日志

[ERROR] ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

SageMaker端点云监视日志

2023-01-19T00:14:05.961+05:30   Loading the hash vectorizer model ...

2023-01-19T00:14:05.961+05:30   2023-01-18 18:44:05,959 ERROR - sklearn_functions - Exception on /invocations [POST]

2023-01-19T00:14:05.961+05:30   Traceback (most recent call last): File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper return fn(*args, **kwargs) File "/opt/ml/code/sklearn_functions.py", line 130, in input_fn pname = json.loads(request_body)["product_name"]

2023-01-19T00:14:05.962+05:30   TypeError: string indices must be integers

2023-01-19T00:14:05.962+05:30   During handling of the above exception, another exception occurred:

2023-01-19T00:14:05.962+05:30   Traceback (most recent call last): File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/miniconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_transformer.py", line 200, in transform self._model, request.content, request.content_type, request.accept File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_transformer.py", line 227, in _default_transform_fn data = self._input_fn(content, content_type) File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 95, in wrapper six.reraise(error_class, error_class(e), sys.exc_info()[2]) File "/miniconda3/lib/python3.7/site-packages/six.py", line 702, in reraise raise value.with_traceback(tb) File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper return fn(*args, **kwargs) File "/opt/ml/code/sklearn_functions.py", line 130, in input_fn pname = json.loads(request_body)["product_name"]

2023-01-19T00:14:05.962+05:30   10.32.0.2 - - [18/Jan/2023:18:44:05 +0000] "POST /invocations HTTP/1.1" 500 290 "-" "AHC/2.0"

2023-01-19T00:14:05.962+05:30   sagemaker_containers._errors.ClientError: string indices must be integers

我试着检查这个类似的GitHub issue还没有解决。任何线索,这是高度赞赏。谢谢!

t1qtbnec

t1qtbnec1#

解决了这个问题。当我使用API gate rest api调用时,相同的负载在没有使用lambda api代理集成和ANY方法设置为string的情况下被传输。因此,将string负载转换为dict负载解决了这个问题

payload = json.loads(payload)

相关问题