fix(ai): omit tools field instead of sending empty array (#3650)

DashScope / Aliyun Qwen (OpenAI-compatible) rejects `tools: []`
with HTTP 400 `"[] is too short - 'tools'"`. Five providers used
a truthy check (`if (context.tools)`) that treated an empty array
as "send tools", so `pi --no-tools` produced `tools: []` in the
request body. Matching the Google provider's pattern, we now
guard on `context.tools.length > 0`:

- openai-completions.ts
- openai-responses.ts
- openai-codex-responses.ts
- azure-openai-responses.ts
- anthropic.ts

The openai-completions fallback that emits `tools: []` when the
conversation has tool history (required by LiteLLM / Anthropic
proxies) is preserved via the existing `else if (hasToolHistory)`
branch.

closes #3649

Co-authored-by: 槐聚 <huaiju@zbyte-inc.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
Co-authored-by: Mario Zechner <badlogicgames@gmail.com>
This commit is contained in:
HQidea
2026-04-24 20:24:31 +08:00
committed by GitHub
parent 6160892626
commit 3e0ee69b5e
7 changed files with 149 additions and 5 deletions

View File

@@ -877,7 +877,7 @@ function buildParams(
params.temperature = options.temperature;
}
if (context.tools) {
if (context.tools && context.tools.length > 0) {
params.tools = convertTools(
context.tools,
isOAuthToken,

View File

@@ -236,7 +236,7 @@ function buildParams(
params.temperature = options?.temperature;
}
if (context.tools) {
if (context.tools && context.tools.length > 0) {
params.tools = convertResponsesTools(context.tools);
}

View File

@@ -340,7 +340,7 @@ function buildRequestBody(
body.service_tier = options.serviceTier;
}
if (context.tools) {
if (context.tools && context.tools.length > 0) {
body.tools = convertResponsesTools(context.tools, { strict: null });
}

View File

@@ -504,7 +504,7 @@ function buildParams(
params.temperature = options.temperature;
}
if (context.tools) {
if (context.tools && context.tools.length > 0) {
params.tools = convertTools(context.tools, compat);
if (compat.zaiToolStream) {
(params as any).tool_stream = true;

View File

@@ -230,7 +230,7 @@ function buildParams(model: Model<"openai-responses">, context: Context, options
params.service_tier = options.serviceTier;
}
if (context.tools) {
if (context.tools && context.tools.length > 0) {
params.tools = convertResponsesTools(context.tools);
}