sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

[Bug] Grok Responses forwards tool_choice without tools and xAI returns invalid-argument

Open xboluo18 opened this issue 4 months ago • 0 comments

Environment

  • Sub2API: v0.1.164
  • Commit: cd8bb98c44303b2c8f04c0da340447c992f0cb7d
  • Account platform/type: Grok OAuth
  • Inbound endpoint: POST /responses (normalized to /v1/responses)
  • Model: grok-4.5
  • Streaming: false

Problem

When a Responses request contains a root-level tool_choice but does not contain a tools field, Sub2API forwards the orphaned tool_choice to xAI. xAI rejects the request:

{
  "code": "invalid-argument",
  "error": "Invalid request content: A tool_choice was set on the request but no tools were specified."
}

The downstream client receives a 502 after Sub2API tries failover. The same request failed with the same upstream 400 on two different Grok OAuth accounts, so this is payload-specific rather than an account credential or entitlement issue.

Minimal reproduction

curl "$SUB2API_BASE_URL/v1/responses" \
  -H "Authorization: Bearer $SUB2API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "grok-4.5",
    "input": "hello",
    "stream": false,
    "tool_choice": "auto"
  }'

Observed upstream result:

OpenAI upstream error 400 (platform=grok type=oauth):
Invalid request content: A tool_choice was set on the request but no tools were specified.

Root cause

sanitizeGrokResponsesTools returns before inspecting tool_choice when the tools field is absent or is not an array:

func sanitizeGrokResponsesTools(body []byte) ([]byte, error) {
    tools := gjson.GetBytes(body, "tools")
    if !tools.Exists() || !tools.IsArray() {
        return body, nil
    }

    // ...

    toolChoice := gjson.GetBytes(body, "tool_choice")
    if shouldDropGrokToolChoice(toolChoice, filteredTools) {
        body, err = sjson.DeleteBytes(body, "tool_choice")
    }
}

The existing cleanup correctly removes tool_choice when tools exists but becomes empty after filtering. It does not handle the equivalent case where tools was never present.

Expected behavior

For Grok Responses requests, Sub2API should remove root-level tool_choice whenever no valid tools remain. A text-only request should then continue normally instead of reaching xAI with an invalid payload.

If a non-array tools value should be treated as malformed input rather than sanitized, Sub2API could return a direct client-facing 400; it should not forward tool_choice without usable tools and later expose the upstream failure as 502.

Suggested fix

Handle the missing-tools case before the current early return, or otherwise make the final invariant explicit: tool_choice must not remain unless at least one valid tool remains.

Suggested regression coverage:

  • no tools + tool_choice: "auto" -> drop tool_choice
  • no tools + object-form tool_choice -> drop tool_choice
  • tools: [] + tool_choice -> drop tool_choice
  • all tools filtered as unsupported + tool_choice -> drop tool_choice
  • at least one valid tool + tool_choice: "auto" -> preserve tool_choice

Workaround

Downstream clients can avoid the failure by omitting tool_choice whenever they do not send a non-empty tools array.

xboluo18 avatar Jul 24 '26 07:07 xboluo18