siyuan-note/siyuan · error
response stream ended before a terminal event
Error message
response stream ended before a terminal event
What it means
Recv() translates the OpenAI Responses API stream into Chat Completions chunks. The Responses stream must end with a terminal event (response.completed or response.incomplete); if the underlying stream yields io.EOF first, the library replaces EOF with this error because the converted stream never emitted a finish_reason chunk. It guards callers against treating a truncated stream as a normal end.
Solutions
- Check network path (proxies, ingress timeouts) so the full SSE body is delivered; increase idle/proxy read timeouts
- Retry the request; the stream may have been dropped transiently
- Verify the endpoint actually implements the Responses API fully (some OpenAI-compatible gateways skip response.completed)
- Reduce max_output_tokens or prompt size so generation finishes before infrastructure timeouts
Defensive patterns
Strategy: try-catch
Try / catch
chunk, err := stream.Recv()
if err != nil {
if strings.Contains(err.Error(), "response stream ended before a terminal event") {
// treat as truncated stream: discard partial answer or retry
return retryCompletion(ctx, req)
}
return err
} Prevention
- Set proxy/ingress idle timeouts well above expected generation time
- Add an application-level retry for streaming requests
- Prefer providers/gateways with full Responses API support
- Monitor finish_reason presence per completed stream
When it happens
Trigger: stream.responses.Recv() returns io.EOF before any ResponseStreamEventCompleted/Incomplete event was seen, e.g. the HTTP body ended after partial events (text deltas) without the terminal response event.
Common situations: Proxy/load-balancer cutting long streaming connections; provider closing the connection early; network drop mid-stream; non-OpenAI gateways that emulate Responses API imperfectly and omit the terminal event; server crash mid-generation.
Understand the failure class
Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.
Related errors
- response failed
- response stream failed
- AI editor stream idle timeout
- compaction summary request failed
- compaction summary stream failed
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/a76a2414bd7694a3.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/openai_completion.go:461
return stream.chat.Recv()
}
if stream.responses == nil {
return openai.ChatCompletionStreamResponse{}, io.EOF
}
if len(stream.pending) > 0 {
response := stream.pending[0]
stream.pending = stream.pending[1:]
return response, nil
}
if stream.responsesDone {
return openai.ChatCompletionStreamResponse{}, io.EOF
}
for {
event, err := stream.responses.Recv()
if err != nil {
if errors.Is(err, io.EOF) {
return openai.ChatCompletionStreamResponse{}, errors.New("response stream ended before a terminal event")
}
return openai.ChatCompletionStreamResponse{}, err
}
response := openai.ChatCompletionStreamResponse{Object: "chat.completion.chunk"}
switch event.Type {
case openai.ResponseStreamEventOutputTextDelta:
stream.responseContent.WriteString(event.Delta)
response.Choices = []openai.ChatCompletionStreamChoice{{
Index: 0,
Delta: openai.ChatCompletionStreamChoiceDelta{Content: event.Delta},
}}
return response, nil
case openai.ResponseStreamEventRefusalDelta:
stream.responseContent.WriteString(event.Delta)
response.Choices = []openai.ChatCompletionStreamChoice{{
Index: 0,
Delta: openai.ChatCompletionStreamChoiceDelta{Content: event.Delta, Refusal: event.Delta},
}}View on GitHub (pinned to 9f775e8a12)