fix: omit empty tools array from API requests#2517
Open
Br1an67 wants to merge 1 commit intoQwenLM:mainfrom
Open
fix: omit empty tools array from API requests#2517Br1an67 wants to merge 1 commit intoQwenLM:mainfrom
Br1an67 wants to merge 1 commit intoQwenLM:mainfrom
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
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. |
Contributor
Author
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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