Type validation failed: Value: {"type":"content_block_start","index":0,"content_...

2026年04月04日 12:06 processing

错误信息

Opencode failed to send message with error: Type validation failed: Value: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","input":{}}}. Error message: [ { "expected": "string", "code": "invalid_type", "path": [ "content_block", "id" ], "message": "Invalid input: expected string, received undefined" }, { "expected": "string", "code": "invalid_type", "path": [ "content_block", "name" ], "message": "Invalid input: expected string, received undefined" } ]

AI 解决方案

##报错分析报告

1.错误翻译


原文:Invalid input: expected string, received undefined
译文:无效输入:期望字符串类型,但收到undefined

2.原因分析


  • 根本原因content_block对象缺少必需的idname字符串属性,触发类型验证失败

  • 发生位置:发送的Valuecontent_block对象({"type":"tool_use","input":{}})未包含id/name字段
  • 3.解决方案


    // 修复后的Value结构(补充id和name字符串属性)
    {
      "type": "content_block_start",
      "index": 0,
      "content_block": {
        "type": "tool_use",
        "id": "tool_123",       // 新增:字符串类型ID
        "name": "example_tool", // 新增:字符串类型名称
        "input": {}
      }
    }
    

    4. 预防措施


  • 最佳实践1:发送前用JSON Schema或TypeScript接口验证对象结构,确保必填字段存在

  • 工具推荐:使用[Zod](https://zod.dev/)(JS/TS)或[Pydantic](https://docs.pydantic.dev/)(Python)做运行时类型校验
  • ---
    请确认修复后是否解决报错,若有其他错误信息可继续提供。