Skip to content

fix(apicompat): map ToolSearch to native tool_search for Codex OAuth - #8804

Merged
Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
Anankke:fix/messages-bridge-native-tool-search
Nov 20, 2026
Merged

Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
Anankke:fix/messages-bridge-native-tool-search

Conversation

@Anankke

@Anankke Anankke commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Refs #8802

On Codex OAuth accounts whose model reports supports_search_tool, ToolSearch maps to the native Responses tool_search with execution: client and deferred tools stay out of tools. tools is identical before and after a reveal, so the cached prefix survives.

main this PR
ToolSearch call and result in history function call and output, (empty) for a tool_reference result tool_search_call and tool_search_output with the referenced definitions
Result text, including is_error function output separate input_text part in the next user message, tool_search_output has no field for it
tool_choice naming ToolSearch or a deferred tool function mapping request as on main, the Responses tool_choice has no value that forces tool_search
Reserved names such as python aliased in tools and function_call also in tool_search_output definitions, on the /v1/responses HTTP and WebSocket passthrough too
count_tokens every tool unchanged, so it counts more than messages sends

Other bridge accounts and requests that don't declare ToolSearch are unchanged. So is Grok, since sub2api already lowers tool_search to a function for xAI.

Shapes follow openai/codex ToolSpec::ToolSearch, ResponseItem::ToolSearchCall and ResponseItem::ToolSearchOutput. Loaded tools are plain functions with defer_loading, as Codex sent them before it wrapped them in the functions namespace.

On a Codex OAuth account with gpt-6-astra at this head, streamed and not: a ToolSearch tool_use, then after a tool_result holding only a tool_reference a tool_use for the revealed tool. An is_error result with text returns 200. A revealed tool named python comes back as python, also on /v1/responses over HTTP and WebSocket.

The Messages to Responses bridge turned every ToolSearch tool_result made of tool_reference blocks into "(empty)" and sent every defer_loading tool as a top-level function.
On ChatGPT/Codex accounts whose model reports supports_search_tool (Grok excluded, xAI has no tool_search) ToolSearch now maps to the native client-executed tool_search, history becomes tool_search_call and tool_search_output carrying the revealed definitions, and an upstream tool_search_call comes back as a ToolSearch tool_use on both the streaming and buffered paths.
Deferred tools stay out of the top-level tools so the prompt cache prefix is the same before and after a reveal.
Requests that do not declare ToolSearch are left untouched.
@r266-tech

Copy link
Copy Markdown
Contributor

AI-assisted local review of 5e322694e72ef39ffb59ec7b5920e206239eb895: the existing ToolSearch tests pass, but I reproduced two request constraints lost when the declarations move to the native representation.

  1. A forced ToolSearch becomes auto. Add "tool_choice":{"type":"tool","name":"ToolSearch"} to the request in the new service test. The actual outbound request has a native tool_search declaration but "tool_choice":"auto", so it no longer requires the requested search. AnthropicToResponses first produces a forced function choice; ApplyNativeToolSearch replaces that function declaration, and normalizeCodexToolChoice then falls back to auto because the function name is absent. Could the forced selection be translated together with the declaration? Forced Bash, auto/any, API Key, and a Codex model without search support retain their selection in the controls.

  2. A revealed reserved function name disagrees with its replay. Rename the deferred WebFetch tool and its tool_reference to python, then append a normal tool_use/tool_result for that revealed function. At the real ForwardAsAnthropic upstream boundary I get:

input[type=tool_search_output].tools[].name = "python"
input[type=function_call].name             = "python__sub2api"

Python has the same mismatch. The existing reserved-name collector traverses top-level tools and additional_tools, but not tool_search_output.tools: it aliases the replayed call while leaving the newly moved definition untouched. Could revealed definitions participate in the same alias/collision/reverse-name handling as the other declarations? An ordinary Echo name, API Key, and unsupported-model controls keep definitions and calls consistent.

Validation on Go 1.27.4/macOS arm64: 3 failing leaf scenarios and 8 passing controls through ForwardAsAnthropic; keeping this head and overlaying only the previous main's openai_gateway_messages.go makes all 11 scenarios pass. This PR's 10 existing focused ToolSearch tests also pass. These are synthetic request/SSE checks; I did not call a live provider or run the full suite for this review.

A revealed tool was declared in tool_search_output under its own name while its replayed function_call used the Codex reserved-name alias, so the two disagreed for python. Reserved-name collection now walks tool_search_output tools like additional_tools. The collector is shared with the /v1/responses passthrough entries, so they now alias tool_search_output definitions too.
The Responses API has no tool_choice value that forces tool_search or a tool that only tool_search_output declares, so a request forcing ToolSearch or a deferred tool keeps the plain function mapping and its body is unchanged.
tool_search_output has no field for the result text, so text and errors from a ToolSearch result ride in a separate input_text part of the user message after the tool outputs, ahead of the user's own parts.
@Anankke

Anankke commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

3f1a7c6 collects reserved names from tool_search_output tools too. A request whose tool_choice names ToolSearch or a deferred tool is sent as on main, since the Responses tool_choice cannot force tool_search.

@r266-tech

Copy link
Copy Markdown
Contributor

Thanks — I rechecked 3f1a7c6d9d1906989a19aba330d477b8e72b80c1 through ForwardAsAnthropic. All 11 original scenarios now pass: forced ToolSearch retains its function selection, and the revealed python / Python definitions match their replayed calls. The added forced-deferred-tool regressions also pass.

I additionally checked six streamed/buffered cases where the revealed definition is the only occurrence of the function name, with no previous function call to seed the reverse map. python, Python, and the Echo control all return their original names. A revealed python conflicting with a top-level python__sub2api is rejected without partial mutation.

The full internal/pkg/apicompat test package and the focused service ToolSearch/alias/response-name tests pass on Go 1.27.4/macOS arm64. This resolves both findings in my earlier comment. AI-assisted local checks with synthetic HTTP/SSE; no live provider or full-backend validation.

@Wei-Shaw
Wei-Shaw merged commit 135aff8 into Wei-Shaw:main Nov 20, 2026
9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants