fix: omit empty tools array from API requests#2517
Conversation
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
|
@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. |
|
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. |
| const tools = request.config?.tools | ||
| ? await this.converter.convertGeminiToolsToAnthropic(request.config.tools) | ||
| : undefined; | ||
| const tools = |
There was a problem hiding this comment.
[Critical] The Anthropic path still allows an empty converted tool list to flow into the request. In buildRequest(), the new guard only checks that request.config.tools.length > 0 before calling convertGeminiToolsToAnthropic(), but that converter can legally return [] after filtering out invalid declarations (for example, functions missing name or description). In that case this code still sends tools: [], which is the same validation problem this PR is fixing in the OpenAI/logging paths.
| const tools = | |
| const tools = | |
| request.config?.tools && request.config.tools.length > 0 | |
| ? await this.converter.convertGeminiToolsToAnthropic( | |
| request.config.tools, | |
| ) | |
| : undefined; | |
| const sampling = this.buildSamplingParameters(request); | |
| const thinking = this.buildThinkingConfig(request); | |
| const outputConfig = this.buildOutputConfig(); | |
| return { | |
| model: this.contentGeneratorConfig.model, | |
| system, | |
| messages, | |
| ...(tools && tools.length > 0 ? { tools } : {}), | |
| ...sampling, | |
| ...(thinking ? { thinking } : {}), | |
| ...(outputConfig ? { output_config: outputConfig } : {}), | |
| }; |
— gpt-5.4 via Qwen Code /review
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 thetoolsfield when the array is non-empty.Dive Deeper
Root Cause
Three content generators (
OpenAI pipeline,Anthropic generator,logging generator) unconditionally assign thetoolsfield after conversion, even when the result is an empty array:An empty
request.config.toolsarray is truthy in JavaScript, so the guard passes. The conversion then returns[], which gets sent astools: []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:
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.tspackages/core/src/core/anthropicContentGenerator/anthropicContentGenerator.tspackages/core/src/core/loggingContentGenerator/loggingContentGenerator.tsReviewer Test Plan
npx vitest run packages/core/src/core/openaiContentGenerator/— 280 tests passnpx vitest run packages/core/src/core/anthropicContentGenerator/— 44 tests passTesting Matrix
Linked issues / bugs
Fixes #2054