-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix SSE streaming format to comply with specification #7182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for localai ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
- Replace json.Encoder with json.Marshal for explicit formatting - Use explicit \n\n for all SSE messages (instead of relying on implicit newlines) - Change %v to %s format specifier for proper string formatting - Fix error message streaming to include proper SSE format - Ensure consistency between chat.go and completion.go endpoints Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
- Handle json.Marshal errors explicitly in error response paths - Add fallback simple error message if marshal fails - Prevents sending 'data: <nil>' on marshal failures - Addresses code review feedback Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
|
@copilot try to address additionally also these points:
|
- Change FinishReason from string to *string in Choice schema - Streaming chunks now omit finish_reason (null) instead of empty string - Final chunks properly set finish_reason to "stop", "tool_calls", etc. - Remove empty content from initial streaming chunks (only send role) - Final streaming chunk sends empty delta with finish_reason - Addresses OpenAI API compliance issues causing client failures Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
All three issues have been addressed in commit deefcb0:
The streaming format now matches OpenAI's specification:
|
- Use consistent pattern: declare variable then take address - Remove inline anonymous function for better readability - Addresses code review feedback Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
core/http/endpoints/openai/chat.go
Outdated
| } | ||
| log.Error().Msgf("Stream ended with error: %v", err) | ||
|
|
||
| stopReason := "stop" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot move common finish reasons to constants
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 9c0c1ae. Created constants.go with:
FinishReasonStop = "stop"FinishReasonToolCalls = "tool_calls"FinishReasonFunctionCall = "function_call"
All string literals replaced with these constants across chat.go, completion.go, and realtime.go.
- Create constants.go with FinishReasonStop, FinishReasonToolCalls, FinishReasonFunctionCall - Replace all string literals with constants in chat.go, completion.go, realtime.go - Improves code maintainability and prevents typos Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
Fix SSE streaming format to comply with specification
Complete Solution
This PR fixes all streaming response issues causing OpenAI API client failures:
SSE Format Fixes ✅
data: {json}\n\nwith proper%sspecifierOpenAI Compliance Fixes ✅
Empty finish_reason: Changed to
*stringwithomitempty"finish_reason": "stop"Empty initial chunks: Initial message now only sends role
{"delta": {"role": "assistant", "content": ""}}{"delta": {"role": "assistant"}}Proper stream termination: Final chunk has empty delta with finish_reason
{"delta": {"content": ""}, "finish_reason": "stop"}{"delta": {}, "finish_reason": "stop"}+data: [DONE]Code Quality Improvements ✅
FinishReasonStop = "stop"FinishReasonToolCalls = "tool_calls"FinishReasonFunctionCall = "function_call"Streaming Flow (OpenAI Compliant)
Files Modified
core/http/endpoints/openai/constants.go: New file with finish reason constantscore/http/endpoints/openai/chat.go: Streaming fixes + use constantscore/http/endpoints/openai/completion.go: Streaming fixes + use constantscore/http/endpoints/openai/realtime.go: Consistency updates + use constantscore/schema/openai.go: FinishReason schema changeValidation
✅ Passes
go vetandgofmt✅ No security vulnerabilities (CodeQL)
✅ OpenAI API specification compliant
✅ Backward compatible for non-streaming responses
Fixes #7180
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.