fix(xai): omit tool_choice when no tools are available - #20606
Closed
Ade5954 wants to merge 1 commit into
Closed
Conversation
…ransport When using xAI/Grok as provider, all requests are routed through the Codex Responses transport. If no local function tools are available (e.g. web_search toolset is filtered out), the transport still sends tool_choice:'auto' and parallel_tool_calls:true but with an empty tools array. xAI rejects this with HTTP 400 'tool_choice was set but no tools were specified' (issue NousResearch#20590). Changes: - agent/transports/codex.py: Only set tool_choice and parallel_tool_calls when _responses_tools() returns a non-empty list. When no function tools are available, omit these fields entirely so providers don't reject the request. - agent/codex_responses_adapter.py: Add defense-in-depth in _preflight_codex_api_kwargs() to strip tool_choice and parallel_tool_calls when the normalized tools array is empty.
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.
What does this PR do?
Fixes a bug where xAI/Grok providers reject requests with HTTP 400
"tool_choice was set but no tools were specified"when no local functiontools are available (e.g. the
web_searchtoolset is filtered out bycheck_web_api_key).Root cause: The
ResponsesApiTransport.build_kwargs()unconditionallysets
tool_choice: "auto"andparallel_tool_calls: truein every request,regardless of whether any function tools were resolved. When
_responses_tools(tools)returnsNone(empty input list), the outgoingbody contains
tool_choiceandparallel_tool_callsbut notoolsarray— which xAI's Responses API rejects.
Related Issue
Fixes #20590
Type of Change
Changes Made
agent/transports/codex.pytool_choice/parallel_tool_callsout of the static kwargs dictinto a conditional block that only executes when
_responses_tools()returns a non-empty list
agent/codex_responses_adapter.py_preflight_codex_api_kwargs(): after thetool_choice/parallel_tool_callspassthrough, strip both fields whenthe normalized
toolsarray is empty or absentHow to Test
tool_choicefieldwhen
toolsis empty