Skip to content

[PR] OpenAI adapter: treat empty tool_calls array as absent so text isn't dropped (#4466) - #4469

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-24_empty-toolcalls-drop
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-24_empty-toolcalls-drop

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #4466

What

The OpenAI chat-completions adapter treated any truthy delta.tool_calls as a tool-call frame. An empty array [] is truthy in JS, so gateways that emit "tool_calls": [] on every chunk of a plain text response (reproduced against Tencent CodeBuddy / copilot.tencent.com) routed all text into the tool-call absorb path and dropped it — the client got an empty stream (Model returned no valid response).

Root cause

src/loop/adapter-openai.ts:

const frameToolCalls = delta?.tool_calls as Array<Record<string, unknown>> | undefined;
if (frameToolCalls) absorbToolCallDeltas(frameToolCalls);

For tool_calls: [], frameToolCalls is [] (truthy), so the chunk is buffered into rawToolChunks for settle-time raw replay. But settleToolCalls bails early (if (pending.size === 0) return) because the empty arrays never populated pending, and round-1 text events carry no raw (which core.ts forwards nothing for). Net effect: zero client-visible bytes. Note filterRealToolFragments in the same file already guarded this correctly (!tcs || tcs.length === 0); the two checks were inconsistent.

Fix

Treat a length-0 array as absent:

const rawFrameToolCalls = delta?.tool_calls as Array<Record<string, unknown>> | undefined;
const frameToolCalls = rawFrameToolCalls?.length ? rawFrameToolCalls : undefined;

Such chunks now fall through to the normal text/reasoning path and reach the client. Real (non-empty) tool calls are untouched.

Behavior change (old → new)

  • Old: a chunk whose delta.tool_calls is a truthy value — including an empty array — was classified as a tool-call frame; its text was buffered and then dropped when no real tool call arrived → empty stream for affected gateways.
  • New: a length-0 tool_calls array is treated as absent; the chunk flows through the normal text/reasoning path and its content reaches the client. Non-empty tool_calls behave exactly as before.
  • Why: restores the intended pass-through for gateways that stamp tool_calls: [] on every chunk; without it those clients could get no model output at all through bili.

Verification

  • New tests/issue4466-empty-toolcalls.test.ts:
    • pure-text response with tool_calls: [] on every chunk → text reaches the client (was empty pre-fix);
    • minimal single-chunk repro from the issue → content passes intact;
    • negative control: a real non-empty tool call is still absorbed and replayed verbatim (name + arguments preserved).
  • Teeth check: reverting only the guard makes both empty-stream tests fail while the negative control still passes.
  • Targeted green locally: the new suite plus openai-reasoning-content, openai-toolcall-finish-reason, issue589-usage-frame (29 tests) and typecheck. Full-suite verdict deferred to CI on the pushed head.

Scope / gates

Single-module fix in src/loop/adapter-openai.ts; no kernel/ change, no config surface change, no persistence/version/wire-format change, no security surface. Full regression via remote CI.

Gateways that emit `tool_calls: []` on every chunk (e.g. Tencent CodeBuddy
against copilot.tencent.com) made frameToolCalls truthy, so all text was
routed into the tool-call absorb path; settle then bailed early (pending
stayed empty) and round-1 text without raw forwards nothing -> empty stream.

Treat a length-0 array as absent, matching the guard already present in
filterRealToolFragments in the same file.

Adds tests/issue4466-empty-toolcalls.test.ts: the pure-text and minimal-repro
streams now reach the client; a negative control confirms a real non-empty
tool call is still absorbed and replayed verbatim.
@ranxianglei ranxianglei changed the title OpenAI adapter: treat empty tool_calls array as absent so text isn't dropped (#4466) [PR] OpenAI adapter: treat empty tool_calls array as absent so text isn't dropped (#4466) Oct 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-24_empty-toolcalls-drop (e749375)

Latest build — install this exact version

npm install -g billion-context@0.0.5364-pr.4469.7988f31

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
e749375 billion-context@0.0.5364-pr.4469.7988f31 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr4469.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重:不重复。 本 PR 是 #4466 的修复 PR;按失败签名(空 tool_calls 数组 / 空流 / CodeBuddy)检索 open/closed issue 与近期 PR,无其他处理同一机制的条目,也无重复修复 PR 在途。

标签:bug / scope/wiring / p1(原有 pr 标签保留)—— 请求管道 wire 帧误分类,受影响网关完全拿不到正文(可用性停摆)故 p1;波及面是任意 OpenAI-wire 客户端而非特定宿主车道,不打客户端标签。

审核结论(独立复审)

拉取 PR head e749375ed 逐项核验:diff 仅 2 个文件(src/loop/adapter-openai.ts 7 行 + 新测试 tests/issue4466-empty-toolcalls.test.ts 68 行);分支落后当前 master 0 提交,mergeable、无冲突。

结构化八问

  1. 是什么问题:OpenAI chat-completions 流解析器把任何 truthy 的 delta.tool_calls 判为工具调用帧;空数组 [] 在 JS 里 truthy,于是「每个文本 chunk 都带 tool_calls: []」的网关(腾讯 CodeBuddy/copilot.tencent.com 实测)会让全部正文走进工具吸收路径后被丢弃,客户端拿到空流(Model returned no valid response)。
  2. 什么原因导致的:根因是同文件两处判定不一致——帧分类处(src/loop/adapter-openai.ts:532)用裸 truthy 判断,而 filterRealToolFragments(同文件 line 34)早已正确判 !tcs || tcs.length===0。因果链:空数组 → absorbToolCallDeltas([]) 不往 pending 填任何东西 → 该帧仍被判为工具帧、推入 rawToolChunks,只 yield 无 raw 的 round-1 结构化 text 事件(core.ts 对此转发零字节)→ finish 时 settleToolCalls 因 pending.size===0 提前 return,缓冲的 raw 帧永不回放 → 客户端可见字节归零。
  3. 影响范围与严重程度:受影响 = 网关在每个 chunk 发 tool_calls: [] 的全部 OpenAI chat-completions 客户端(CodeBuddy 已实测;/bili/ 改写车道与证书 MITM 车道共用此适配器)。对这些客户端是「完全拿不到模型输出」(p1);不发空数组的正常网关(OpenAI/SGLang/vLLM 等)零影响。
  4. 怎么修复的:把长度 0 的数组当不存在(rawFrameToolCalls?.length ? rawFrameToolCalls : undefined),此类 chunk 回落到正常 text/reasoning 路径送达客户端;语义对齐同文件既有守卫。非空 tool_calls 逻辑不变。
  5. 是否有回归问题:对改动路径承担的既有行为逐项清点(下表),未发现无意回归;高危模式(语义漂移/fail-fast 变吞错/阈值时序/输出格式漂移)均不涉及。
  6. 是否破坏缓存:不涉及。改动只在前端流帧分类层,请求构造、前缀缓存接缝、快照渲染、增量游标全未触碰;正常路径转发的字节不变。
  7. 是否改了设计方向:无架构转向,仅把一处误分类修正为与同文件既有守卫一致的语义。
  8. 是否引入了新的配置:无新增 env/config/CLI flag。

回归清点(改动路径上的既有行为)

既有行为 结论
真实非空 tool_call 的吸收 + settle 逐字回放(name/id/arguments 保留) 保持 —— 守卫只排除 length 0;负向对照测试钉住
无 tool_calls 的普通 text/reasoning 帧 保持 —— 未触碰
content 与非空 tool_calls 同帧的混合帧 保持 —— 仍判工具帧、缓冲待 settle 回放
#589 尾部 usage 帧(空 choices) 保持 —— 在改动代码上游处理;套件绿
#1306 name-split / nameless buffer 处理 保持 —— 在 absorb/settle 内部,未动
#1881 双字段帧 prose 过滤 保持 —— 两个分支跑同一套过滤器
非数组 truthy 值(如字符串) 无漂移 —— 字符串也有 .length:非空字符串行为与旧版一致,空字符串本为 falsy

另扫了 src/loop/ 其余适配器查同类缺陷:adapter-openai.ts 内另一处即 filterRealToolFragments(本就正确);core.ts / tag-echo-filter.ts 只做计数与过滤不做帧分类;google 适配器走 functionCall 不同 wire。无额外实例。

测试有效性与 diff 整洁度

  • 测试有牙:断言的是真实客户端可见字节(bytes.includes("HELLO ")),不是恒真断言。我独立复跑了 teeth 验证:只回退那一行守卫,两个空流测试失败、负向对照仍过——与 PR 描述完全一致,随后已恢复。
  • 负向对照防修过头(真实工具调用必须仍被吸收并逐字回放)。
  • diff 干净:2 文件 74 行,每行都与本修复相关;命名达意、注释讲为什么(OpenAI adapter drops all content when upstream sends an empty tool_calls array #4466 上下文)、风格与文件一致、无死代码、无无关改动。
  • 本地定向运行:新套件 3/3 绿;相邻 openai-reasoning-content / openai-toolcall-finish-reason / issue589-usage-frame / openai-assistant-content-harden / openai-finish-reason-dup 共 36/36 绿;typecheck 干净。全量判定以远程 CI 为准。

CI 状态(写此评论时)

静态门禁已全绿(mysql-contract / config-surface-guard / unused-exports / test-import-graph success;e2e·theorem 按门控 skip);30 个 check 中 12 个 test shard 仍在跑,暂无红项。若后续 shard 变红我会跟进补报。

行为变更披露(旧→新):旧——delta.tool_calls 只要 truthy(含空数组)即判工具帧,其正文被缓冲后丢弃 → 受影响网关拿到空流;新——长度 0 的 tool_calls 视为不存在,chunk 走正常文本路径、正文送达客户端;非空 tool_calls 完全照旧。原因:恢复这类网关的预期透传,否则它们经 bili 拿不到任何输出。

判定:审核通过。单模块修复 + 回归钉 + teeth 验证过的负向对照,未触碰 kernel/config/persistence/wire-form/security;CI 全量绿后即可合(合并按规矩由人来执行)。

@ranxianglei ranxianglei added bug Something isn't working p1 scope/wiring labels Oct 24, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

CI 终态更新(补上上一条留的尾巴):PR head e749375ed 上的全部 check 已出齐,无任何红项——

  • 最新一轮完整运行:11 个 test shard(ubuntu/windows × node 22/24 分片)全部 success;static-gates(ubuntu/windows)success;全部守卫 job(kernel-guard / pi-subagents-guard / version-guard / lock-sync / docs-sync / config-surface-guard / unused-exports / test-import-graph / lane-guard / lifecycle-ledger-guard / docs-coverage / build-artifact)success;
  • e2e / theorem / real-client 各车道按门控 skip(设计如此,需显式 env 门才跑);
  • 期间有一批 01:52:5x 的 check 显示 cancelled——那是同一 commit 上的一次瞬时重派发(整批 job 在数秒内被取消),随后被新一轮完整运行取代并全绿,不是失败信号。

最终判定维持上一条结论:审核通过,CI 全量绿已达成,可合(合并按规矩由人来执行)。

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

Labels

bug Something isn't working p1 scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAI adapter drops all content when upstream sends an empty tool_calls array

1 participant