Skip to content

fix: omit empty tools array from API requests#61

Open
BingqingLyu wants to merge 1 commit into
mainfrom
fork-pr-2517-fix-empty-tools-array
Open

fix: omit empty tools array from API requests#61
BingqingLyu wants to merge 1 commit into
mainfrom
fork-pr-2517-fix-empty-tools-array

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 27, 2026

Copy link
Copy Markdown
Owner

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 QwenLM#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
@BingqingLyu BingqingLyu added conflicting-group-1 Conflicting PR group 1 — review as a batch conflicting-pr Shares at least one cross-PR dependency with other PRs and removed conflicting-pr labels May 7, 2026
@BingqingLyu

BingqingLyu commented May 7, 2026

Copy link
Copy Markdown
Owner Author

Conflict Group 1

This 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.

Function File Also modified by
buildOpenAIRequestForLogging loggingContentGenerator.ts #1, #113, #114, #117, #88
buildRequest pipeline.ts #1, #113, #114, #117, #88
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
Loading

Posted by codegraph-ai conflict detection.

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

Labels

conflicting-group-1 Conflicting PR group 1 — review as a batch conflicting-pr Shares at least one cross-PR dependency with other PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants