You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Lambda调用SageMaker触发ModelError错误的排查求助

Lambda调用SageMaker端点触发ModelError问题排查

错误信息

调用Lambda请求SageMaker端点时返回400客户端错误,具体错误响应如下:

{
  "errorMessage": "An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from primary and could not load the entire response body. See CloudWatch日志组/aws/sagemaker/Endpoints/jira-endpoint获取详细信息.",
  "errorType": "ModelError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 19, in lambda_handler\n    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,\n",
    "  File \"/var/runtime/botocore/client.py\", line 565, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 1021, in _make_api_call\n    raise error_class(parsed_response, operation_name)\n"
  ]
}

当前Lambda代码

import os, io, boto3, json, csv, base64

ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= boto3.client('runtime.sagemaker')

def lambda_handler(event, context):
    project = event['project']
    # project = base64.b64decode(event['project'])
    # timeline = base64.b64decode(event['timeline'])
    
    # payload = """{}.
    #     Given the project description above, sugges a project tasklist to achieve the project goals.
    #     The project timeline is {} weeks.
    # """.format(project, timeline)

        
    payload = project
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       Body=project, ContentType="text/csv")

    result = json.loads(response["Body"].read().decode())
    print(result)
    return {
        "statusCode": 200,
        "headers": { "content-type": "application/json"},
        "body":  result
    }

问题分析与解决方案

核心问题

错误400表明请求格式不符合SageMaker端点的预期,当前代码存在两个明显问题:

  • 请求ContentType设置为text/csv,但预构建模型通常期望JSON格式输入
  • 请求Body直接传入原始字符串,未按模型要求的结构封装

解决步骤

  1. 查看CloudWatch日志:进入指定的SageMaker端点日志组,获取模型返回的具体错误详情,明确输入格式要求
  2. 匹配模型输入规范:
    大多数SageMaker预构建LLM模型(如JumpStart模型)期望JSON格式输入,示例结构:
    {"inputs": "你的提示文本内容"}
    
    此时ContentType需设置为application/json
  3. 调整Lambda代码:
    修改请求格式,确保Body符合模型要求,示例代码如下:
    import os, boto3, json
    
    ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
    runtime= boto3.client('runtime.sagemaker')
    
    def lambda_handler(event, context):
        project = event['project']
        # 构建符合模型要求的JSON payload,可根据需求补充提示词
        payload = json.dumps({
            "inputs": f"{project}. Given the project description above, suggest a project tasklist to achieve the project goals."
        })
        response = runtime.invoke_endpoint(
            EndpointName=ENDPOINT_NAME,
            Body=payload,
            ContentType="application/json"
        )
        result = json.loads(response["Body"].read().decode())
        print(result)
        return {
            "statusCode": 200,
            "headers": {"content-type": "application/json"},
            "body": json.dumps(result)  # Lambda返回的body需为字符串类型
        }
    
  4. 验证输入内容:确保event中的project字段为有效文本,若前端传入的是base64编码内容,需取消注释解码代码

内容的提问来源于stack exchange,提问作者Dru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 21:12:41