Skip to content

[Bug] Claude Messages 缺少 server_tool_use.id 时,Responses 转换 panic、断流且失败仍计费 #7548

Closed as not planned

Description

@geeknonerd

环境

  • New API:v1.0.0-rc.40(当前 main 的同一转换路径未包含 id 兜底)
  • 渠道类型:Anthropic / Claude Messages,上游为 OpenCode Go(https://opencode.ai/zen/go)
  • 模型:kimi-k3
  • 客户端:Codex CLI 0.156.1(Responses wire,/v1/responses)

现象

Codex 反复显示 Search results for query: 与 Reconnecting... 1/5,最终报:

stream disconnected before completion: hosted-tool stream call is missing an id

New API 日志:

[ERR] data handler goroutine panic: runtime error: invalid memory address or nil pointer dereference
[ERR] stream ended: reason=panic end_error="handler panic: runtime error: invalid memory address or nil pointer dereference"

该请求 completion_tokens=0,但仍产生了扣费;客户端因断流进入重连循环。

最小复现

通过 New API 的 Anthropic 类型渠道请求 Responses 入口:

curl -N http://<new-api-host>:3001/v1/responses \
  -H "Authorization: Bearer <REDACTED>" \
  -H 'Content-Type: application/json' \
  -H 'session-id: repro-hosted-id' \
  -d '{"model":"kimi-k3","input":"你好","max_output_tokens":64,"stream":true,"tools":[{"type":"web_search"}]}'

HTTP 200,但流内包含:

event: error
data: {"type":"error","code":"server_error","message":"hosted-tool stream call is missing an id"}

日志同时出现上述 data handler goroutine panic。

上游响应证据

直连上游 Anthropic Messages(同一模型、同一 hosted tool)可以复现字段缺失:

curl -N https://opencode.ai/zen/go/v1/messages \
  -H "x-api-key: <REDACTED>" \
  -H 'anthropic-version: 2023-06-01' \
  -H 'Content-Type: application/json' \
  -H 'x-opencode-session: repro-hosted-id' \
  -d '{"model":"kimi-k3","max_tokens":256,"stream":true,"messages":[{"role":"user","content":"你好"}],"tools":[{"type":"web_search_20250305","name":"web_search"}]}'

上游流中的 content_block_start 块:

{"type":"content_block_start","index":1,"content_block":{"type":"server_tool_use","name":"web_search"}}
{"type":"content_block_start","index":2,"content_block":{"type":"web_search_tool_result", ...}}
  • server_tool_use 缺少 id
  • web_search_tool_result 缺少 tool_use_id

Anthropic 官方规范要求 server_tool_use 携带 id,并由 web_search_tool_result.tool_use_id 关联(见官方 Web search tool 文档示例)。因此上游 OpenCode Go 的该响应本身不符合 Anthropic Messages 协议;同一模型在明确要求搜索的请求中会返回正常的 srvtoolu_... id,行为不一致。

期望行为

作为协议转换网关,New API 可以对不合规的上游响应容错:

  1. server_tool_use.id 缺失时生成稳定的合成 id(例如 srvtoolu_<uuid> 或基于 content block index 的 id),并在后续结果块缺少 tool_use_id 时按 block index 关联;
  2. 无法恢复时应发送终态错误事件,而不是在 data handler 中 panic;
  3. completion_tokens=0 的失败流不应产生扣费,或应回滚预扣费。

源码线索

  • relaykit/relayconvert/internal/claude_messages/to_oai_responses_hosted_stream.go:pending.id = block.Id,在 content_block_stop 时调用 StartHostedTool,没有为空 id 生成兜底值。
  • relaykit/relayconvert/internal/oai_chat/to_oai_responses_stream_resp.go:StartHostedTool 在 start.ID == "" 时直接返回 hosted-tool stream call is missing an id。
  • 日志中另有 data handler goroutine panic: nil pointer dereference,说明该异常路径还存在未防护的空指针。

建议修复

  1. 在 Claude hosted stream bridge 中维护 index -> call id 映射;id 缺失时生成合成 id,结果块缺 tool_use_id 时复用该映射。
  2. 为 server_tool_use 缺 id、web_search_tool_result 缺 tool_use_id 增加单元测试,覆盖流式与非流式路径。
  3. 修复 data handler 的 nil dereference,并确保失败路径有终态事件。
  4. 核对失败流的计费/退费逻辑。

责任边界说明

触发根因在上游 OpenCode Go 返回缺少协议必填字段的块,这部分已另向对应上游反馈方向记录;但 New API 作为转换层出现 panic、流无法终止且失败仍计费,属于可独立修复的健壮性问题,因此提交本 issue。

Activity

  1. new-api-reviewer-bot commented on Sep 23, 2026

    @new-api-reviewer-bot

    [Bot Review]

    感谢提交。当前内容没有使用 Bug Report 模板的必需结构,尤其缺少带复选框的「提交确认/Submission Checks」章节。请按模板补齐「版本/version」「问题描述/Issue Description」和「复现步骤/Steps to Reproduce」后重新提交。

  2. added
    invalid
    on Sep 23, 2026
  3. geeknonerd commented on Sep 23, 2026

    @geeknonerd
    Author

    已按 Bug Report 模板重新提交:#7549

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions