400 {"error":{"message":"InvokeModelWithResponseStream: operation error Bedrock ...

2026年03月05日 16:31 状态: processing

🚨 错误信息

API Error: 400 {"error":{"message":"InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: e6c5e413-de60-4258-9135-0b4b644f20bd, ValidationException: ***.***.content.2: `tool_use` ids must be unique (request id: 20260305162932294599765XuehLXUD)","type":"<nil>"},"type":"error"}

🤖 AI解决方案

报错分析报告

1.错误翻译


``
原文:
tool_use ids must be unique
译文:
tool_use的id必须唯一
`

2. 原因分析


  • 根本原因:请求中tool_use工具的id字段存在重复值,违反Bedrock Runtime的唯一性约束。

  • 具体位置:报错指向请求内容的content.2部分(对应..content.2),该位置的tool_use id与其他块重复。
  • 3.解决方案


    确保每个
    tool_use块的id全局唯一,以下是修复示例(以Python/JavaScript为例):

    python

    Python示例:用UUID生成唯一id


    import uuid

    tool_use_blocks = [
    {
    "type": "tool_use",
    "id": str(uuid.uuid4()), # 唯一id(如"f81d4fae-7dec-11d0-a765-00a0c91e6bf6")
    "name": "your_tool_name",
    "input": {"key": "value"}
    },
    # 其他tool_use块同理,每个id均用uuid.uuid4()生成
    ]

    javascript
    // JavaScript示例:用crypto.randomUUID()生成唯一id
    const toolUseBlocks = [
    {
    type: "tool_use",
    id: crypto.randomUUID(), // 唯一id(如"1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed")
    name: "your_tool_name",
    input: { key: "value" }
    },
    // 其他块同理
    ];

    4. 预防措施


  • 最佳实践1:生成tool_use块时,强制使用唯一标识符(优先选UUID、时间戳+随机数组合)。

  • 最佳实践2:提交请求前,用集合校验id唯一性(如Python的set(ids)、JS的new Set(ids)`),重复则抛错。

  • 推荐工具:用Postman/Insomnia的预请求脚本自动生成唯一id,或用ESLint规则检查重复id。
  • ---
    请验证修复后id的唯一性,即可解决该400错误。