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

Bedrock Agent无法解析Node.js Lambda响应,报DependencyFailedException求助

问题

我有一个Bedrock Agent,其动作组已连接到Node.js Lambda并定义了API Schema。尽管已确保Lambda响应符合AWS官方文档规范,但调用端点时仍出现以下错误:

ERROR  DependencyFailedException: The server encountered an error processing the Lambda response. Check the Lambda response and retry the request

Lambda返回内容如下:

{
    "messageVersion": "1.0",
    "response": {
        "actionGroup": "downstream-actions",
        "apiPath": "/hello",
        "httpMethod": "GET",
        "httpStatusCode": 200,
        "responseBody": {
            "application/json": {
                "body": "{\"message\":\"Hello, world!\"}"
            }
        }
    }
}

该Lambda基于Node.js开发,我尝试将整个对象序列化后返回,但仍得到相同错误。

补充的API Schema:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Downstream Action API",
    "version": "1.0.0",
    "description": "API for invoking downstream actions",
  },
  "paths": {
    "/hello": {
      "get": {
        "summary": "Get a hello world message",
        "description": "Simple endpoint that returns a hello world message",
        "operationId": "getHello",
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "message": {
                      "type": "string",
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
}
解决方案
  • 修正responseBody的body格式:当前返回的body是序列化后的JSON字符串,但Bedrock Agent要求直接传入JSON对象,无需提前序列化。修改后的responseBody应如下:
    "responseBody": {
        "application/json": {
            "body": {"message":"Hello, world!"}
        }
    }
    
  • 修复API Schema的语法错误:Schema中存在两处多余的尾逗号(info.description末尾、properties.message末尾),JSON不允许这种格式,会导致Bedrock解析失败。修正后的Schema:
    {
      "openapi": "3.0.0",
      "info": {
        "title": "Downstream Action API",
        "version": "1.0.0",
        "description": "API for invoking downstream actions"
      },
      "paths": {
        "/hello": {
          "get": {
            "summary": "Get a hello world message",
            "description": "Simple endpoint that returns a hello world message",
            "operationId": "getHello",
            "responses": {
              "200": {
                "description": "Successful response",
                "content": {
                  "application/json": {
                    "schema": {
                      "type": "object",
                      "properties": {
                        "message": {
                          "type": "string"
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    
  • 调整Node.js Lambda返回逻辑:Lambda会自动将返回的对象序列化为JSON,无需手动调用JSON.stringify()。直接返回对象即可避免双重序列化问题:
    exports.handler = async (event) => {
      return {
          "messageVersion": "1.0",
          "response": {
              "actionGroup": "downstream-actions",
              "apiPath": "/hello",
              "httpMethod": "GET",
              "httpStatusCode": 200,
              "responseBody": {
                  "application/json": {
                      "body": {"message":"Hello, world!"}
                  }
              }
          }
      };
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 12:47:08