Skip to content

fix(openai): declare web_search when OAuth history replays web_search_call - #7939

Merged
Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
IrisRainbowNeko:fix/issue-7927-web-search-history-tool
Oct 9, 2026
Merged

Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
IrisRainbowNeko:fix/issue-7927-web-search-history-tool

Conversation

@IrisRainbowNeko

@IrisRainbowNeko IrisRainbowNeko commented Oct 8, 2026 •

Copy link
Copy Markdown

问题

OpenAI OAuth 账号下,Codex 在使用过网页搜索的会话里触发上下文压缩时,流会以如下错误中断,Codex 反复重连后放弃,整个会话卡死:

{"type":"response.failed","error":{"type":"internal_error","code":"upstream_error","message":"response protection is unavailable"}}

日志表现为 openai.forward_failed ... upstream response failed: response protection is unavailable,客户端为 stream disconnected before completion: response protection is unavailable。

根因

ChatGPT Codex 上游(约 2026-10-06 起)对 input 中回放了 hosted web_search_call 历史项、但请求未声明 web_search 工具 的请求直接返回上述错误。

Codex 对自定义 provider 使用本地压缩(codex-rs/core/src/compact.rs 中 Prompt { ..Default::default() }),压缩请求携带完整历史但 tools: [];而普通轮次会带 {"type":"web_search"}。因此普通请求正常,只要历史里有搜索记录,之后的每一次压缩都会失败——与 #7927 中“压缩就报错”“去掉 web_search_call 就成功”的对照结论一致。tools 为空的 remote compaction v2(compaction_trigger)请求同样会失败。

通过本地日志代理抓取 Codex(0.159.0-alpha)真实请求,并经网关打到真实上游的复现:

请求 工具声明 历史含 web_search_call 结果
Codex 普通轮次 tools 含 web_search 是 response.completed
Codex 本地压缩请求(原样重放) tools: [] 是 response protection is unavailable
同上 + 本 PR 改写 [web_search] + tool_choice:"none" 是 response.completed
仅声明 function 工具 [function] 是 response protection is unavailable
function + web_search [function, web_search] 是 response.completed
v2 压缩(compaction_trigger),tools: [] 无 是 response protection is unavailable
v2 压缩 + 本 PR 改写 [web_search] + none 是 response.completed,输出 compaction 项
v2 压缩,Codex 默认完整工具(已含 web_search,本 PR 不改动) 完整 是 response.completed,输出 compaction 项
Responses Lite v2 压缩,无工具 无 是 response protection is unavailable
Responses Lite,顶层 tools:[web_search] 顶层 是 400 responses Lite does not support top-level tool type "web_search"
Responses Lite + 本 PR 改写(additional_tools 置于 trigger 之前) input[].additional_tools 是 response.completed,输出 compaction 项

web_search_call 的 id 换成伪造值或去掉 id 结果不变,说明触发条件是“历史有搜索项而请求未声明搜索工具”,与具体 id 无关。上表中“本 PR 改写”的请求体均由本 PR 代码实际生成后发往真实上游。

改动

新增 ensureOpenAIOAuthWebSearchToolForHistory(map 版,用于 Codex OAuth transform)/ ...Body(gjson/sjson 版,用于透传与 WS,避免全量解码),接入 OAuth 链路:

  • Codex OAuth transform(applyCodexOAuthTransformWithOptions,在 input 过滤之后执行;新增 ResponsesLite 选项,由 Forward 传入)
  • OAuth WebSocket 与 HTTP 透传共用的 normalizeOpenAIResponsesWebSocketCompatibilityBody(抽出 normalizeOpenAIResponsesCompatibilityBodyWithOptions,透传调用点传入 Compact,原函数签名与行为不变)

规则:

  • 仅当 input 含 type=web_search_call 项,且 tools 与 input[].additional_tools 均未声明 web_search* 时生效;
  • 标准 Responses:在顶层 tools 追加 {"type":"web_search","external_web_access":false}(仅缓存搜索);
  • Responses Lite:上游拒绝顶层 hosted tool,改为追加到调用方的第一个 additional_tools 项;没有时插入 {"type":"additional_tools","role":"developer","tools":[...]},若末尾是 compaction_trigger 则插在它之前,保证 v2 压缩的 trigger 仍为最后一项;
  • 若调用方原本未提供任何工具(顶层与 additional_tools 均为空),且 tool_choice 缺省 / auto / none,则固定为 "none",保证注入的工具不会被调用、请求维持“无工具”语义;调用方本身带了工具时不改 tool_choice;
  • /responses/compact 端点(transform 与透传)与 API Key 账号不受影响。

验证

  • 单测:transform / WS / 透传 compact / API Key 各链路对照;tools 缺省 / null / 已声明 / web_search_preview / additional_tools 已声明 / 字符串 input 等边界;Lite 下插入位置(trigger 之前、无 trigger 时末尾)、扩展已有 additional_tools、空 additional_tools、不污染顶层 tools;map 版与 body 版输出一致;注入工具对象不共享。
  • Forward 级测试(httpUpstreamRecorder):transform / passthrough × 标准 / Lite 四种组合,断言发往 chatgpt.com/backend-api/codex/responses 的请求体声明了 web_search(Lite 在 additional_tools)、tool_choice=none、compaction_trigger 仍为最后一项;回退接线后测试失败。
  • go test -tags=unit ./...(go1.27.0)全部通过;go build ./...、go vet、gofmt、git diff --check 通过。
  • 本地未运行 golangci-lint 与集成测试,交由 CI。

Fixes #7927

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA. ✅
Posted by the CLA Assistant Lite bot.

@IrisRainbowNeko

Copy link
Copy Markdown
Author

Thank you for your contribution! Before we can merge this PR, we need you to sign our Contributor License Agreement (CLA).

To sign, please reply with the following comment:

I have read the CLA Document and I hereby sign the CLA

You only need to sign once — it will be valid for all your future contributions to this project.

I have read the CLA Document and I hereby sign the CLA

IrisRainbowNeko seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

I have read the CLA Document and I hereby sign the CLA

@IrisRainbowNeko

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

damian2848 added a commit to damian2848/sub2api that referenced this pull request Oct 8, 2026
…_call

ChatGPT's Codex endpoint now fails the stream with "response protection is
unavailable" when input replays a hosted web_search_call item but the
request declares no web_search tool. Codex compaction (local compact with
tools:[] and remote compaction v2 with compaction_trigger) hits this after
any web search in the session, so the session keeps reconnecting.

Ported from Wei-Shaw/sub2api#7939 (IrisRainbowNeko, still open upstream):
for OAuth accounts on /responses (transform, passthrough and WebSocket),
inject {"type":"web_search"} at top level, or into input additional_tools
for Responses Lite (kept ahead of a trailing compaction_trigger), and pin
tool_choice to "none" when the caller offered no tools. /responses/compact
and API key accounts are untouched.

Fork adjustments:
- passthrough call site adapted to the fork's Copilot SDK guard
- injected tool omits external_web_access, which this fork already strips
  from native Codex requests as an unknown parameter (c034d73)
- errcheck fixes for golangci-lint v2.13

Co-Authored-By: IrisRainbowNeko <rainbow-neko@outlook.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ranxi2001 pushed a commit to ranxi2001/sub2api that referenced this pull request Oct 8, 2026
…_call

ChatGPT Codex upstream now rejects requests whose input replays hosted
web_search_call items without declaring a web_search tool, failing the
stream with "response protection is unavailable". Codex local compaction
sends the full history with tools: [], so any session that used web
search can no longer compact and gets stuck reconnecting.

For OpenAI OAuth requests (Codex transform, HTTP passthrough and WS),
when input contains web_search_call and no web_search* tool is declared,
inject {"type":"web_search","external_web_access":false} (into
additional_tools for Responses Lite, before compaction_trigger) and pin
tool_choice to "none" when the caller had no tools. The /responses/compact
endpoint and API key accounts are untouched.

Ported from Wei-Shaw#7939 (fixes Wei-Shaw#7927).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
IrisRainbowNeko and others added 3 commits October 9, 2026 11:03
…_call

ChatGPT's Codex endpoint now rejects requests whose input replays a hosted
web_search_call item unless tools declares web_search, failing the stream
with "response protection is unavailable". Codex local context compaction
sends the full history with tools:[], so every compaction after a web search
fails and the session gets stuck in a reconnect loop.

For OAuth transform, passthrough and WebSocket paths, append a cached-only
web_search tool when the history contains web_search_call and no web_search
tool is declared. When the caller offered no tools, pin tool_choice to
"none" so the injected tool cannot be invoked. The /responses/compact wire
shape is left untouched.

Fixes Wei-Shaw#7927
Responses Lite rejects hosted tools at the top level ("responses Lite does
not support top-level tool type web_search"), so Lite requests now carry the
injected web_search tool in an input additional_tools item: extend the
caller's first additional_tools item, or insert one before a trailing
compaction_trigger so the v2 compaction wire keeps the trigger last.

The HTTP passthrough path also runs the shared compatibility normalizer on
/responses/compact; thread the compact flag through so that endpoint is
left untouched, and drop the now-redundant call from the passthrough OAuth
body normalizer.
@Wei-Shaw
Wei-Shaw force-pushed the fix/issue-7927-web-search-history-tool branch from 97e4857 to 85e3110 Compare October 9, 2026 03:10
@Wei-Shaw
Wei-Shaw merged commit f2669c8 into Wei-Shaw:main Oct 9, 2026
9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 9, 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.

gpt 报错 response protection is unavailable

2 participants