Skip to content

fix: omit empty tools array from API requests#2517

Open
Br1an67 wants to merge 1 commit intoQwenLM:mainfrom
Br1an67:fix/empty-tools-array
Open

fix: omit empty tools array from API requests#2517
Br1an67 wants to merge 1 commit intoQwenLM:mainfrom
Br1an67:fix/empty-tools-array

Conversation

@Br1an67
Copy link
Copy Markdown
Contributor

@Br1an67 Br1an67 commented Mar 20, 2026

TLDR

Sending tools: [] to OpenAI-compatible APIs causes validation errors like "[] is too short - 'tools'". This happens when no tools are available (e.g., certain model configurations or modes that disable tools). The fix guards all three content generator code paths to only include the tools field when the array is non-empty.

Dive Deeper

Root Cause

Three content generators (OpenAI pipeline, Anthropic generator, logging generator) unconditionally assign the tools field after conversion, even when the result is an empty array:

// Before — sends tools: [] to the API
if (request.config?.tools) {
  baseRequest.tools = await this.converter.convertGeminiToolsToOpenAI(request.config.tools);
}

An empty request.config.tools array is truthy in JavaScript, so the guard passes. The conversion then returns [], which gets sent as tools: [] in the request body. OpenAI's API (and compatible endpoints) rejects this with a validation error.

Fix

Added length checks at both the input and output stages:

// After — only sends tools when non-empty
if (request.config?.tools && request.config.tools.length > 0) {
  const tools = await this.converter.convertGeminiToolsToOpenAI(request.config.tools);
  if (tools.length > 0) {
    baseRequest.tools = tools;
  }
}

The outer check avoids unnecessary conversion of empty input. The inner check handles edge cases where tools have no valid function declarations after conversion.

Files Changed

  • packages/core/src/core/openaiContentGenerator/pipeline.ts
  • packages/core/src/core/anthropicContentGenerator/anthropicContentGenerator.ts
  • packages/core/src/core/loggingContentGenerator/loggingContentGenerator.ts

Reviewer Test Plan

  1. npx vitest run packages/core/src/core/openaiContentGenerator/ — 280 tests pass
  2. npx vitest run packages/core/src/core/anthropicContentGenerator/ — 44 tests pass

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

Fixes #2054

When no tools are available, the converted tools array can be empty.
Sending `tools: []` causes API validation errors like
'[] is too short - tools' on OpenAI-compatible endpoints.

Guard all three code paths (OpenAI pipeline, Anthropic generator,
logging generator) to only include the tools field when the array
is non-empty.

Fixes QwenLM#2054
@tanzhenxin
Copy link
Copy Markdown
Collaborator

@Br1an67 Thanks for your contributions to Qwen Code! But we are really appreciate if you can take time to do manual testing before submitting PR, I mean it is kind of the bottleneck work of current "Vibe Coding" world.

A screenshot after fix would be very great. We will take it as a proof of work to guide us to allocate resources for the PR review.

@Br1an67
Copy link
Copy Markdown
Contributor Author

Br1an67 commented Mar 22, 2026

Thanks for the feedback @tanzhenxin — completely agree. I'll set up a local dev environment and add screenshots showing the before/after behavior. Will update this PR shortly.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xcode26 qwen ai 模型接入后,使用 报错[] is too short - 'tools'

2 participants