修复 Codex Responses 非流式请求被上游拒绝 - #5031
Conversation
WalkthroughThe 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. ChangesCodex Streaming Enforcement
Sequence DiagramsequenceDiagram
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
codex渠道类型不考虑做任何对用户行为的自动矫正操作,用户需要在对该渠道充分了解的情况下使用,没有基础技能储备不建议使用。 |
Important
📝 变更描述 / 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=true和store=false,避免透传模式绕过 Codex adaptor 的转换逻辑。该修复与 #3303 的方向不同:#3303 主要是在渠道测试失败后尝试用流式重试;本 PR 则是在 Codex Responses relay 路径中保证请求和内部响应处理状态始终符合 Codex 上游要求,因此同时覆盖普通调用、渠道测试以及请求体透传场景。
/v1/responses/compact路径保持原有行为,不强制改为流式。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、理解偏差或预期不一致直接归类为 bug。📸 运行证明 / Proof of Work
已在服务器环境运行并通过相关单元测试:
go test ./relay/channel/codex ./relay测试结果:
已完成 Docker 镜像构建验证:
docker build -t new-api:codex-stream-turnstile .构建结果:成功。
Summary by CodeRabbit
Release Notes
Bug Fixes
Tests