Skip to content

fix: strip tool_choice from payload when tools array is empty - #47947

Closed
WormW wants to merge 4 commits into
openclaw:mainfrom
WormW:fix/strip-tool-choice-without-tools
Closed

WormW wants to merge 4 commits into
openclaw:mainfrom
WormW:fix/strip-tool-choice-without-tools

Conversation

@WormW

@WormW WormW commented Mar 16, 2026

Copy link
Copy Markdown

Summary

Strict OpenAI-compatible providers (e.g. vLLM, scnet) reject requests containing tool_choice without a corresponding tools array with HTTP 400. OpenClaw unconditionally injected tool_choice: "auto" even when tools was correctly omitted, violating the OpenAI API spec.

Two code paths are fixed:

  • WebSocket path (openai-ws-stream.ts): Guard tool_choice injection in extraParams with tools.length > 0
  • HTTP path (extra-params.ts): Add a final onPayload wrapper (createToolChoiceGuardWrapper) that strips tool_choice and parallel_tool_calls from any openai-completions/openai-responses payload that lacks a non-empty tools array

Root cause

// openai-ws-stream.ts - tools correctly guarded:
tools: tools.length > 0 ? tools : undefined,

// But tool_choice was unconditionally injected:
if (streamOpts?.toolChoice !== undefined) {
  extraParams.tool_choice = streamOpts.toolChoice; // ← BUG
}

This produced payloads like {"model":"...","messages":[...],"tool_choice":"auto"} (no tools field), which strict providers reject.

Reproduction

# Fails: tool_choice without tools → 400 Format Error
curl -X POST "$BASE_URL/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{"model":"SomeModel","messages":[{"role":"user","content":"hi"}],"tool_choice":"auto"}'

# Works: no tool_choice
curl -X POST "$BASE_URL/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{"model":"SomeModel","messages":[{"role":"user","content":"hi"}]}'

Test plan

  • Updated existing WS stream test to verify tool_choice is omitted when tools are empty
  • Added new WS stream test verifying tool_choice IS forwarded when tools are present
  • Added HTTP path tests for tool_choice stripping (absent tools, empty tools array, non-empty tools)
  • Added test verifying parallel_tool_calls is also stripped when tools absent
  • Updated existing parallel_tool_calls and Moonshot tool_choice tests to include tools in payloads (since the guard now strips them when tools are absent)
  • All 133 tests pass (openai-ws-stream.test.ts + pi-embedded-runner-extraparams.test.ts)

Fixes #44110.

Strict OpenAI-compatible providers (e.g. vLLM, scnet) reject requests
containing `tool_choice` without a corresponding `tools` array with
HTTP 400.  OpenClaw unconditionally injected `tool_choice` even when
`tools` was correctly omitted, violating the OpenAI API spec.

Two code paths are fixed:

1. WebSocket path (openai-ws-stream.ts): Guard `tool_choice` injection
   in `extraParams` with `tools.length > 0`.

2. HTTP path (extra-params.ts): Add a final `onPayload` wrapper
   (`createToolChoiceGuardWrapper`) that strips `tool_choice` and
   `parallel_tool_calls` from any openai-completions/openai-responses
   payload that lacks a non-empty `tools` array.

Fixes openclaw#44110.
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: M labels Mar 16, 2026
@greptile-apps

greptile-apps Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a spec violation where tool_choice (and parallel_tool_calls) were sent in OpenAI-compatible payloads even when the tools array was absent or empty. Strict providers like vLLM and scnet correctly reject such malformed requests with HTTP 400.

The fix covers two code paths:

  • WS path (openai-ws-stream.ts): a simple && tools.length > 0 guard on the tool_choice injection
  • HTTP path (extra-params.ts): a new createToolChoiceGuardWrapper applied as the final onPayload wrapper in applyExtraParamsToAgent, which strips both tool_choice and parallel_tool_calls from any openai-completions/openai-responses payload that lacks a non-empty tools array

The wrapper ordering is correct: createToolChoiceGuardWrapper is applied after createParallelToolCallsWrapper, so the guard's onPayload fires after parallel_tool_calls has been injected, allowing it to be stripped when there are no tools. The approach is consistent with the existing onPayload-chaining pattern already used by createParallelToolCallsWrapper and createThinkingWrapper.

Test coverage is thorough — both paths are tested with absent tools, empty tools array, and non-empty tools array, and existing tests that rely on tool_choice behavior were correctly updated to include a tools array in their payloads.

Confidence Score: 5/5

  • This PR is safe to merge — it is a targeted, well-tested bug fix with no behavioural change for payloads that already include tools.
  • The fix is minimal and surgical: a one-line guard on the WS path and a final safety-net wrapper on the HTTP path. The wrapper ordering guarantees the guard fires after all injection wrappers. Existing tests were updated correctly, and new tests cover absent tools, empty tools array, non-empty tools array, and parallel_tool_calls stripping. No regressions are expected for providers that do not reject orphaned tool_choice fields (e.g. OpenAI itself), since the guard only removes fields that would otherwise cause HTTP 400 on strict providers.
  • No files require special attention.

Last reviewed commit: 740b40d

WormW and others added 3 commits March 16, 2026 14:27
Use `parameters` instead of `inputSchema` to match the Tool<TSchema>
type, and cast via `as unknown` to satisfy strict type checking.
kiranvk-2011 added a commit to kiranvk-2011/openclaw that referenced this pull request Apr 27, 2026
When active-memory plugin invokes sub-agents with toolsAllow, the tools
get filtered to [] in the embedded runner context. Strict OpenAI-compatible
providers (DashScope/GLM, Kimi, vLLM) reject tools: [] with HTTP 400.

Adds wrapStreamFnStripEmptyTools() as a payload interceptor that:
- Strips tools entirely when array is empty
- Strips tool_choice when tools is absent

Consolidates openclaw#59898, openclaw#66710, openclaw#47947 into a single guard.
Fixes openclaw#53174.
@clawsweeper

clawsweeper Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Thanks for the context here. I swept through the related work, and this is now duplicate or superseded.

This PR should close as superseded by the newer open #70790, which explicitly consolidates this PR's empty-tools/tool_choice fix with related embedded-runner empty-tools work. Current main does not yet implement the full requested guard, and the WebSocket path has since been refactored, so the best path is to carry useful tests from this PR into the canonical follow-up rather than keep this stale partial PR open.

Best possible solution:

Close this stale partial PR as superseded by #70790, then review or revise the canonical PR against current main and carry over this PR's useful WebSocket and HTTP regression tests where they still apply.

What I checked:

  • Canonical successor PR: The provided GitHub context for fix(agents): strip empty tools/tool_choice from embedded runner payloads #70790 says it is an open PR that consolidates fix(agents): handle explicit empty tool lists in system prompt #59898, fix: forward toolsAllow through embedded runner call chain #66710, and this PR into a single payload interceptor for empty tools arrays and orphaned tool_choice, and it explicitly lists this PR in its root-cause chain.
  • Current WebSocket path has moved: Current main builds WebSocket response.create payloads in buildOpenAIWebSocketResponseCreatePayload; it still copies options.toolChoice into tool_choice whenever present, while tools is only included when the converted tools array is non-empty. This means the old one-line hunk in openai-ws-stream.ts is stale against the current code shape. (src/agents/openai-ws-request.ts:152, 2ccdbc7dd93b)
  • Current WebSocket tests still show the old behavior: The shared contextStub uses tools: [], and the existing test still expects tool_choice to be sent with that empty-tools context. That confirms current main has not absorbed this PR's intended regression coverage. (src/agents/openai-ws-stream.test.ts:1892, 2ccdbc7dd93b)
  • HTTP embedded path still lacks the final guard: Current main has createParallelToolCallsWrapper injecting parallel_tool_calls, but searches found no createToolChoiceGuardWrapper or equivalent general guard that strips orphaned tool_choice/parallel_tool_calls when tools are absent or empty. (src/agents/pi-embedded-runner/extra-params.ts:375, 2ccdbc7dd93b)
  • Security review: The PR diff in the provided context only touches agent payload handling and colocated tests under src/agents; it does not modify CI workflows, dependency sources, lockfiles, install/build/release scripts, package publishing metadata, secrets handling, permissions, or downloaded artifacts.

So I’m closing this here and keeping the remaining discussion on the canonical linked item.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 2ccdbc7dd93b.

@clawsweeper clawsweeper Bot closed this Apr 28, 2026
kiranvk-2011 added a commit to kiranvk-2011/openclaw that referenced this pull request May 4, 2026
When active-memory plugin invokes sub-agents with toolsAllow, the tools
get filtered to [] in the embedded runner context. Strict OpenAI-compatible
providers (DashScope/GLM, Kimi, vLLM) reject tools: [] with HTTP 400.

Adds wrapStreamFnStripEmptyTools() as a payload interceptor that:
- Strips tools entirely when array is empty
- Strips tool_choice when tools is absent

Consolidates openclaw#59898, openclaw#66710, openclaw#47947 into a single guard.
Fixes openclaw#53174.
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: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: tool_choice: "auto" sent even when tools.allow: [] is configured

1 participant