Skip to content

fix(agents): omit empty tools array in OpenAI transport params - #61147

Closed
GitZhangChi wants to merge 1 commit into
openclaw:mainfrom
GitZhangChi:fix/agents-empty-tools-array
Closed

GitZhangChi wants to merge 1 commit into
openclaw:mainfrom
GitZhangChi:fix/agents-empty-tools-array

Conversation

@GitZhangChi

@GitZhangChi GitZhangChi commented Apr 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Old code could send tools: [] in two cases: when context.tools existed but was empty, and when hasToolHistory inferred prior tool calls from message history. Strict OpenAI-compatible providers (e.g. vLLM, LocalAI) reject requests with an empty tools array, causing inference to fail even when no tools should be active.

This change only sets params.tools when context.tools.length > 0, and removes the now-unneeded hasToolHistory fallback. No changes to tool conversion, compat flags, or provider routing.

Change Type

  • Bug fix

Scope

  • Gateway / orchestration

Root Cause

Two paths could emit tools: []:

  1. context.tools was truthy but empty, so convertTools([]) produced [] and forwarded it.
  2. hasToolHistory unconditionally set params.tools = [] when prior tool calls existed in message history, even if no tools were currently registered.

Targeted regression coverage was missing for the no-active-tools path.

Regression Test Plan

  • Added regression coverage in src/agents/openai-transport-stream.test.ts
  • Verified tools is omitted when context.tools is empty
  • Verified tools is omitted when tool call history exists but no tools are currently registered

User-visible / Behavior Changes

Requests to strict OpenAI-compatible providers (e.g. vLLM, LocalAI) no longer fail due to an empty tools array when no tools are active for the current turn.

Repro + Verification

Environment

  • OS: Linux
  • Runtime: Node 22+
  • Provider: vLLM / LocalAI (strict OpenAI-compatible)

Evidence

  • Targeted regression tests added and passing
  • pnpm lint
  • pnpm check

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No

Risks and Mitigations

Omitting an empty tools field is the correct OpenAI-compatible behavior. Providers that need explicit tool disablement should use tool_choice: "none" rather than rely on tools: [].

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Apr 5, 2026
@greptile-apps

greptile-apps Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Targeted fix for strict OpenAI-compatible providers that reject requests with an empty tools array. The root cause was context.tools = [] being truthy, so the old if (context.tools) guard would call convertTools([]), return [], and set params.tools = []. The context.tools.length > 0 guard closes that path correctly, and removing hasToolHistory is safe since OpenAI does not require tools to be present when tool calls already appear in message history.

Confidence Score: 5/5

Safe to merge — the fix is correct and targeted with no blocking issues.

All findings are P2 style suggestions. The core logic is sound: the length guard correctly prevents empty tools arrays from being sent to strict providers, and the hasToolHistory removal is safe because OpenAI does not require the tools parameter to be present when tool calls appear in message history.

No files require special attention.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/openai-transport-stream.ts
Line: 1302-1306

Comment:
**Redundant inner length guard**

`convertTools` is a pure `.map()` over every entry (lines 1258–1266) and cannot filter or drop items, so `converted.length` is always equal to `context.tools.length`. Because the outer guard already ensures `context.tools.length > 0`, the inner `converted.length > 0` check is always `true` and can never fire. It may mislead readers into thinking `convertTools` can produce fewer items than its input (e.g. if it were filtering unsupported tools). Consider simplifying:

```suggestion
  if (context.tools && context.tools.length > 0) {
    params.tools = convertTools(context.tools, compat, model);
  }
```

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(agents): skip empty tools array in O..." | Re-trigger Greptile

Comment thread src/agents/openai-transport-stream.ts Resolved
@GitZhangChi GitZhangChi changed the title fix(agents): skip empty tools array in OpenAI transport params fix(agents): omit empty tools array in OpenAI transport params Apr 5, 2026
Only set params.tools when context.tools is non-empty — the old code
called convertTools([]) and forwarded tools:[] to the provider, which
strict OpenAI-compatible providers reject with a 400. Also drops the
hasToolHistory fallback that unconditionally sent tools:[] whenever
prior tool calls appeared in message history; OpenAI does not require
the field to be present in that case.

convertTools is a pure .map() so the outer length guard is sufficient.

Tests: assert tools key is absent when context.tools is empty, and when
tool call history exists but no tools are currently registered.
@GitZhangChi GitZhangChi closed this Apr 5, 2026
@GitZhangChi
GitZhangChi deleted the fix/agents-empty-tools-array branch April 5, 2026 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant