Skip to content

修复 Codex Responses 非流式请求被上游拒绝 - #5031

Closed
JunXiaoRuo wants to merge 1 commit into
QuantumNous:mainfrom
JunXiaoRuo:codex/fix-codex-streaming-only
Closed

修复 Codex Responses 非流式请求被上游拒绝#5031
JunXiaoRuo wants to merge 1 commit into
QuantumNous:mainfrom
JunXiaoRuo:codex/fix-codex-streaming-only

Conversation

@JunXiaoRuo

@JunXiaoRuo JunXiaoRuo commented May 21, 2026

Copy link
Copy Markdown
Contributor

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

修复 Codex 渠道在 Responses 普通接口下,入口请求未开启流式时被上游拒绝的问题。

Codex 后端的 /backend-api/codex/responses 要求请求必须使用流式模式。此前当调用方没有显式传入 stream=true,或渠道测试默认使用非流式请求时,new-api 会把非流式请求继续转发给 Codex 上游,并且内部也会按非流式响应处理,最终触发上游返回 400 Stream must be set to true

本次改动在 Codex Responses 请求转换阶段统一强制设置 stream=true,同时同步更新 RelayInfo 和 gin context 中的流式状态,使后续响应处理按 SSE 流式逻辑执行。对于开启请求体透传的场景,也会在转发前修正请求体中的 stream=truestore=false,避免透传模式绕过 Codex adaptor 的转换逻辑。

该修复与 #3303 的方向不同:#3303 主要是在渠道测试失败后尝试用流式重试;本 PR 则是在 Codex Responses relay 路径中保证请求和内部响应处理状态始终符合 Codex 上游要求,因此同时覆盖普通调用、渠道测试以及请求体透传场景。

/v1/responses/compact 路径保持原有行为,不强制改为流式。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、理解偏差或预期不一致直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

已在服务器环境运行并通过相关单元测试:

go test ./relay/channel/codex ./relay

测试结果:

ok  	github.com/QuantumNous/new-api/relay/channel/codex
ok  	github.com/QuantumNous/new-api/relay

已完成 Docker 镜像构建验证:

docker build -t new-api:codex-stream-turnstile .

构建结果:成功。

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Codex API responses now consistently enforce streaming mode to ensure proper response handling across all relay configurations.
  • Tests

    • Added tests verifying streaming behavior enforcement for Codex responses and request body normalization.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The pull request adds streaming enforcement for Codex API responses across two pipeline stages: during OpenAI request conversion in the adaptor layer, and during passthrough body normalization in the responses handler. Request stream state, relay streaming flags, and Gin context keys are synchronized to ensure Codex responses are treated as streaming.

Changes

Codex Streaming Enforcement

Layer / File(s) Summary
Adaptor-side streaming state synchronization
relay/channel/codex/adaptor.go, relay/channel/codex/adaptor_test.go
A new forceCodexResponsesStream helper synchronizes streaming state across request.Stream, info.IsStream, and the Gin context key during ConvertOpenAIResponsesRequest. Tests verify that RelayModeResponses forces streaming state while RelayModeResponsesCompact does not, and confirm req.Store is set to JSON false.
Passthrough body streaming normalization
relay/responses_handler.go, relay/responses_handler_test.go
ResponsesHelper detects the Codex + responses relay-mode combination and calls normalizeCodexResponsesPassthroughBody to rebuild the outgoing request body, forcing stream=true and store=false while updating info.IsStream and the Gin context stream flag. A test verifies the normalized output and side effects reflect forced streaming state.

Sequence Diagram

sequenceDiagram
  participant Client
  participant ConvertOpenAIResponsesRequest
  participant forceCodexResponsesStream
  participant ResponsesHelper
  participant normalizeCodexResponsesPassthroughBody
  participant CodexAPI

  Client->>ConvertOpenAIResponsesRequest: OpenAI responses request<br/>(stream=false initially)
  ConvertOpenAIResponsesRequest->>forceCodexResponsesStream: call helper
  forceCodexResponsesStream->>forceCodexResponsesStream: request.Stream = true
  forceCodexResponsesStream->>forceCodexResponsesStream: info.IsStream = true
  forceCodexResponsesStream->>forceCodexResponsesStream: gin.Context[ContextKeyIsStream] = true
  ConvertOpenAIResponsesRequest-->>Client: normalized request (stream=true)
  
  Client->>ResponsesHelper: passthrough request<br/>(Codex + responses mode)
  ResponsesHelper->>normalizeCodexResponsesPassthroughBody: rebuild body
  normalizeCodexResponsesPassthroughBody->>normalizeCodexResponsesPassthroughBody: stream = true<br/>store = false
  normalizeCodexResponsesPassthroughBody->>normalizeCodexResponsesPassthroughBody: info.IsStream = true<br/>gin.Context[ContextKeyIsStream] = true
  normalizeCodexResponsesPassthroughBody-->>ResponsesHelper: normalized JSON body
  ResponsesHelper->>CodexAPI: forward with stream=true
  CodexAPI-->>ResponsesHelper: streaming response
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • QuantumNous/new-api#4325: Both PRs change Codex handling to enforce streaming by forcing stream=true in request normalization and updating Codex automatic channel tests to run/validate in stream mode.

Suggested reviewers

  • creamlike1024

Poem

🐰 A Codex stream they sought to force,
With channel tests on rocky course,
Now adaptor, handler both align,
Streaming true in perfect line. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR implements Codex-specific streaming enforcement in adaptor conversion and passthrough request handling, directly addressing issue #3298's requirement to fix hardcoded non-streaming behavior causing 400 errors on streaming-required channels.
Out of Scope Changes check ✅ Passed All changes are within scope: three files modified focus exclusively on Codex Responses streaming enforcement and its testing, with no unrelated modifications to other channel types or system components.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title describes fixing Codex Responses non-streaming requests being rejected, which directly aligns with the main changes that force streaming behavior in Codex response handling.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@JunXiaoRuo JunXiaoRuo changed the title 修复 Codex Responses 测试非流式导致的 400 错误 修复 Codex Responses 非流式请求被上游拒绝 May 21, 2026
@seefs001

Copy link
Copy Markdown
Collaborator

codex渠道类型不考虑做任何对用户行为的自动矫正操作,用户需要在对该渠道充分了解的情况下使用,没有基础技能储备不建议使用。

@seefs001 seefs001 closed this May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants