fix: omit empty tools array from API requests#61
Open
BingqingLyu wants to merge 1 commit into
Open
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
This was referenced Apr 28, 2026
Owner
Author
Conflict Group 1This PR shares modified functions with 5 other PR(s): #1, #113, #114, #117, #88. These PRs should be reviewed as a batch — merging one may affect the others.
graph LR
PR61["PR #61"]
FbuildOpenAIRequestForLogging_4379["buildOpenAIRequestForLogging<br>loggingContentGenerator.ts"]
PR61 -->|modifies| FbuildOpenAIRequestForLogging_4379
PR1["PR #1"]
PR1 -->|modifies| FbuildOpenAIRequestForLogging_4379
PR113["PR #113"]
PR113 -->|modifies| FbuildOpenAIRequestForLogging_4379
PR114["PR #114"]
PR114 -->|modifies| FbuildOpenAIRequestForLogging_4379
PR117["PR #117"]
PR117 -->|modifies| FbuildOpenAIRequestForLogging_4379
PR88["PR #88"]
PR88 -->|modifies| FbuildOpenAIRequestForLogging_4379
FbuildRequest_1009["buildRequest<br>pipeline.ts"]
PR61 -->|modifies| FbuildRequest_1009
PR1 -->|modifies| FbuildRequest_1009
PR113 -->|modifies| FbuildRequest_1009
PR114 -->|modifies| FbuildRequest_1009
PR117 -->|modifies| FbuildRequest_1009
PR88 -->|modifies| FbuildRequest_1009
Posted by codegraph-ai conflict detection. |
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 QwenLM#2054