Repository navigation
fix(btw): strip empty tools array for strict OpenAI-compatible endpoints - #59669
GitZhangChi wants to merge 1 commit into
Conversation
Greptile SummaryThis PR fixes a
Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: src/agents/btw.ts
Line: 310-318
Comment:
**No test coverage for the new `onPayload` logic**
The PR description explicitly outlines a regression test that should lock in this fix (`src/agents/btw.test.ts` — "BTW command execution with a session history containing tool calls, asserting that the outgoing payload does not contain an empty `tools` array"), but no test was added.
Every other `onPayload` mutation in the codebase (e.g. `openai-stream-wrappers.ts`, `anthropic-family-tool-payload-compat.ts`) has corresponding test assertions that the payload sent downstream matches expectations. Without a test here, the next refactor of `streamSimple`'s options handling could silently re-introduce the bug.
A minimal test in `btw.test.ts` would look like:
```ts
it("strips empty tools array from onPayload to fix Aliyun/GLM compat", async () => {
mockDoneAnswer("Some answer.");
await runSideQuestion();
const [, , options] = streamSimpleMock.mock.calls[0] ?? [];
// Simulate the upstream injection of tools: []
const payloadWithEmptyTools = { messages: [], tools: [] };
const result = options?.onPayload?.(payloadWithEmptyTools);
expect(payloadWithEmptyTools).not.toHaveProperty("tools");
// Return value should still be the (mutated) payload or truthy
expect(result ?? payloadWithEmptyTools).not.toHaveProperty("tools");
});
```
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: src/agents/btw.ts
Line: 310-318
Comment:
**`onPayload` return value diverges from established codebase pattern**
Every other `onPayload` hook in this codebase (see `openai-stream-wrappers.ts` lines 325, 366, 408; `stream-payload-utils.ts` line 17; `anthropic-family-tool-payload-compat.ts` line 179) follows this pattern:
```ts
const originalOnPayload = options?.onPayload;
onPayload: (payload) => {
// mutate payload...
return originalOnPayload?.(payload, model); // returns undefined when no prior handler
}
```
The callers that consume the return value (e.g. `openai-ws-stream.ts` line 511) all use `nextPayload ?? payload`, so returning `undefined` is the signal "use the mutated original", while returning a new object replaces the payload entirely.
The new code returns `payload` (the mutated original). While this is functionally equivalent today — because `nextPayload ?? payload` collapses to `payload` either way — it breaks the semantic contract: returning the payload object itself is typically reserved for callers that want to *replace* it. Returning `undefined` (by not returning, or via a chain) communicates "I mutated in place; use the original."
Consider aligning with the established pattern:
```suggestion
onPayload: (payload) => {
if (payload && typeof payload === "object" && "tools" in payload) {
const p = payload as { tools?: unknown[] };
if (Array.isArray(p.tools) && p.tools.length === 0) {
delete p.tools;
}
}
},
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(btw): strip empty tools array to fix..." | Re-trigger Greptile |
Fixes #53472 The /btw command was failing with `400 [] is too short - 'tools'` when using Aliyun (Qwen), GLM, and other strict OpenAI-compatible endpoints. Root cause: upstream `@mariozechner/pi-ai` injects `tools: []` when the session has tool history, but these providers reject empty tools arrays. Solution: add an `onPayload` hook in `runBtwSideQuestion` to strip the empty `tools` array before sending it to the provider. This is a general, localized fix that handles any provider rejecting empty tools arrays without affecting normal tool calls in the main session. Verified locally with qwen3.5-plus-ali.
|
Closing — the root cause was already fixed upstream in |
Summary
/btwcommand fails with400 [] is too short - 'tools'error when using Qwen, GLM, Kimi and some strict OpenAI-compatible endpoints./btw) with these models if the active session has prior tool call history.onPayloadhook insrc/agents/btw.tsto intercept the provider request and safely strip thetoolsproperty if it is an empty array.@mariozechner/pi-aibehavior, and main session tool calls remain untouched.Change Type
Scope
Linked Issue/PR
Root Cause / Regression History
@mariozechner/pi-aiinjectstools: []whenhasToolHistory(context.messages)is true (to satisfy certain Anthropic proxy requirements). However, the/btwside-channel deliberately omits tools. Whentools: []is sent to Kimi/GLM APIs, they strictly reject it with a 400 error.Regression Test Plan
src/agents/btw.test.ts/btwcommand execution with a session history containing tool calls, asserting that the outgoing payload does not contain an emptytoolsarray.User-visible / Behavior Changes
Users of Kimi, Qwen, GLM, and strict OpenAI-compatible endpoints will now be able to use
/btwsuccessfully without receiving a 400 Bad Request error.Security Impact
NoNoNoNoNoRepro + Verification
Environment
qwen3.5-plusGLM-5MiniMax-M2.5kimi-k2.5Steps
/btw what did we just do?Expected
Actual (before fix)
400 [] is too short - 'tools'.Evidence
Human Verification
runBtwSideQuestion.Review Conversations
Compatibility / Migration
YesNoNoRisks and Mitigations
tools: []when tool history is present./btw, it might fail. However, standardizing on omitting the empty array is the safer universal default, as it conforms strictly to the OpenAI API specification.