Skip to content

fix: 修复 Chat Completions 兼容层误将 response.failed 返回为成功响应#2972

Open
northya wants to merge 1 commit into
Wei-Shaw:mainfrom
northya:fix/chat-completions-response-failed
Open

fix: 修复 Chat Completions 兼容层误将 response.failed 返回为成功响应#2972
northya wants to merge 1 commit into
Wei-Shaw:mainfrom
northya:fix/chat-completions-response-failed

Conversation

@northya
Copy link
Copy Markdown

@northya northya commented Jun 2, 2026

背景

/v1/chat/completions 通过 OpenAI Responses 兼容路径转发时,上游 Responses SSE 可能返回终止事件 response.failed,例如输入超过模型上下文窗口时。

修复前,Chat Completions 兼容转换逻辑会把 response.failed 当作普通终止事件处理,并转换成一个成功的 Chat Completion 响应:

  • HTTP 200
  • 空 assistant message
  • finish_reason: "stop"

这会导致客户端把上游失败误判为正常的空响应,并且和 /v1/responses 接口的行为不一致;/v1/responses 会把同类上游失败作为错误返回。

修复方式

本次修改在 Chat Completions 兼容路径中显式识别 response.failed,避免它继续进入 Responses 到 Chat Completions 的成功响应转换逻辑。

具体包括:

  • 非流式 /v1/chat/completions:当 buffered Responses SSE 的最终响应状态为 failed 时,返回 UpstreamFailoverError,不再写出 HTTP 200。
  • 流式 /v1/chat/completions:当上游返回 response.failed 时,在 pending compatibility chunks flush 到客户端之前中止成功响应转换。
  • 复用现有 OpenAI stream failover error 逻辑,使 ops upstream error 记录和 502 错误响应行为与现有 Responses passthrough 路径保持一致。

测试

新增了两个回归测试,分别覆盖:

  • 非流式 Chat Completions 收到上游 response.failed
  • 流式 Chat Completions 在客户端输出前收到上游 response.failed

已验证:

go test -tags=unit -run 'TestForwardAsChatCompletions_(BufferedResponseFailedTriggersFailover|StreamResponseFailedTriggersFailoverBeforeFlush)' ./internal/service
go test -tags=unit -run 'TestForwardAsChatCompletions' ./internal/service
go test -tags=unit ./internal/service

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

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

@northya northya force-pushed the fix/chat-completions-response-failed branch from f67f3e6 to 2e212d1 Compare June 2, 2026 06:00
@northya
Copy link
Copy Markdown
Author

northya commented Jun 2, 2026

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

github-actions Bot added a commit that referenced this pull request Jun 2, 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.

1 participant