Skip to content

fix(openai): avoid empty choices stream heartbeat#430

Merged
CJackHwang merged 1 commit intoCJackHwang:mainfrom
NgoQuocViet2001:ai/openai-stream-empty-choices
May 5, 2026
Merged

fix(openai): avoid empty choices stream heartbeat#430
CJackHwang merged 1 commit intoCJackHwang:mainfrom
NgoQuocViet2001:ai/openai-stream-empty-choices

Conversation

@NgoQuocViet2001
Copy link
Copy Markdown
Contributor

💻 变更类型 | Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • 📝 docs
  • 🔨 chore

🔀 变更说明 | Description of Change

Fixes #429.

OpenAI Chat Completions streaming currently sends keep-alive traffic as both an SSE comment and a JSON chat.completion.chunk with choices: []. Some strict OpenAI-compatible clients index choices[0] for every data frame, so that heartbeat chunk can raise IndexError: list index out of range and abort an otherwise healthy stream.

This change keeps the heartbeat as a valid SSE comment only:

  • preserve : keep-alive\n\n and flush behavior so idle streams still stay warm
  • stop emitting a JSON data frame with an empty choices array
  • update the regression test to assert keep-alives do not produce JSON data frames or [DONE]

Duplicate check: searched open PRs/issues for choices / empty choices / IndexError; the currently open PRs (#422, #407, #405) cover unrelated read-cache, WebUI env, and multi-turn work.

📝 补充信息 | Additional Information

Validation run locally with portable Go 1.26.0:

  • gofmt -w internal/httpapi/openai/chat/chat_stream_runtime.go internal/httpapi/openai/chat/chat_stream_runtime_test.go
  • go test ./internal/httpapi/openai/chat -run TestChatStreamKeepAliveUsesCommentOnly -count=1
  • go test ./internal/httpapi/openai/chat -count=1
  • go test ./internal/httpapi/openai/... -count=1
  • go test ./... -count=1
  • git diff --check

@vercel
Copy link
Copy Markdown

vercel Bot commented May 5, 2026

@NgoQuocViet2001 is attempting to deploy a commit to the cjack's projects Team on Vercel.

A member of the Team first needs to authorize it.

@CJackHwang CJackHwang changed the base branch from main to dev May 5, 2026 14:22
@CJackHwang CJackHwang changed the base branch from dev to main May 5, 2026 14:23
@vercel
Copy link
Copy Markdown

vercel Bot commented May 5, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ds2api Ready Ready Preview, Comment May 5, 2026 2:24pm

@CJackHwang CJackHwang merged commit d69b065 into CJackHwang:main May 5, 2026
8 checks passed
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.

[Bug] 流式响应中返回了 choices 为空的 chunk

2 participants